Esempio n. 1
0
        public void TestMethod1()
        {
            var dbContext = new DanwuDbContext();
            var context = new EntityFrameworkRepositoryContext();
            var unitOfWork = new EntityFrameworkUnitOfWork(dbContext);
            IUserRepository userRepository = new UserRepository(context);
            userRepository.Create(new User()
            {
                UserName = "******",
                NickName = "坏坏男孩",
                RealName = "吴丹",
                PhoneNum = "18916765826",
                Email = "*****@*****.**",
                Status = UserStatus.Enabled,
                PassWord = "******",
                RegisterTime = DateTime.Now,
                LastLogonTime = DateTime.Now
            });
            userRepository.Create(new User()
            {
                UserName = "******",
                NickName = "坏坏男孩1",
                RealName = "吴丹1",
                PhoneNum = "18916765000",
                Email = "*****@*****.**",
                Status = UserStatus.Disabled,
                PassWord = "******",
                RegisterTime = DateTime.Now,
                LastLogonTime = DateTime.Now
            });

            var result = context.Commit();
            //var result = unitOfWork.Commit();
        }
Esempio n. 2
0
        public void TestMethod1()
        {
            var             dbContext      = new DanwuDbContext();
            var             context        = new EntityFrameworkRepositoryContext();
            var             unitOfWork     = new EntityFrameworkUnitOfWork(dbContext);
            IUserRepository userRepository = new UserRepository(context);

            userRepository.Create(new User()
            {
                UserName      = "******",
                NickName      = "坏坏男孩",
                RealName      = "吴丹",
                PhoneNum      = "18916765826",
                Email         = "*****@*****.**",
                Status        = UserStatus.Enabled,
                PassWord      = "******",
                RegisterTime  = DateTime.Now,
                LastLogonTime = DateTime.Now
            });
            userRepository.Create(new User()
            {
                UserName      = "******",
                NickName      = "坏坏男孩1",
                RealName      = "吴丹1",
                PhoneNum      = "18916765000",
                Email         = "*****@*****.**",
                Status        = UserStatus.Disabled,
                PassWord      = "******",
                RegisterTime  = DateTime.Now,
                LastLogonTime = DateTime.Now
            });

            var result = context.Commit();
            //var result = unitOfWork.Commit();
        }
        public void Paging2Test()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(1000);
                foreach (var customer in customers)
                {
                    repository.Add(customer);
                }
                repositoryContext.Commit();

                var selectedCustomers = customers.Where(x => x.Id > 20000000).OrderByDescending(x => x.Id);
                var idList            = selectedCustomers.Select(x => x.Id).Skip(15).Take(15).ToList();
                var totalRecords      = selectedCustomers.Count();
                var totalPages        = (totalRecords + 14) / 15;

                var pagedResult = repository.FindAll(c => c.Id > 20000000,
                                                     new SortSpecification <int, Customer> {
                    { x => x.Id, SortOrder.Descending }
                }, 2, 15);

                Assert.Equal(totalRecords, pagedResult.TotalRecords);
                Assert.Equal(totalPages, pagedResult.TotalPages);
                Assert.Equal(2, pagedResult.PageNumber);
                Assert.Equal(15, pagedResult.PageSize);
                Assert.True(CompareIds(idList, pagedResult.As <int, Customer>().Select(x => x.Id).ToList()));
            }
        }
        public void PagingTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(21);
                foreach (var customer in customers)
                {
                    repository.Add(customer);
                }
                repositoryContext.Commit();

                var idList = customers.OrderBy(x => x.Email).Select(x => x.Id).Skip(5).Take(5).ToList();

                var pagedResult = repository.FindAll(new SortSpecification <int, Customer> {
                    { x => x.Email, SortOrder.Ascending }
                }, 2, 5);

                Assert.Equal(21, pagedResult.TotalRecords);
                Assert.Equal(5, pagedResult.TotalPages);
                Assert.Equal(2, pagedResult.PageNumber);
                Assert.Equal(5, pagedResult.PageSize);
                Assert.True(CompareIds(idList, pagedResult.As <int, Customer>().Select(x => x.Id).ToList()));
            }
        }
        public void FindAllWithSortTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(500);

                foreach (var cust in customers)
                {
                    repository.Add(cust);
                }
                repositoryContext.Commit();

                var sorted = customers.OrderBy(x => x.Email).OrderByDescending(x => x.Name).ToList();

                var result = repository.FindAll(x => true, new SortSpecification <int, Customer> {
                    { _ => _.Email, SortOrder.Ascending }, { _ => _.Name, SortOrder.Descending }
                }).ToList();

                bool match = true;
                var  total = sorted.Count();
                for (var i = 0; i < total; i++)
                {
                    if (sorted[i].Email != result[i].Email || sorted[i].Name != result[i].Name)
                    {
                        match = false;
                        break;
                    }
                }

                Assert.True(match);
            }
        }
        public void RemoveByKeyTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var customer   = Customer.CreateOne(1, "Sunny Chen", "*****@*****.**");
                var repository = repositoryContext.GetRepository <int, Customer>();
                repository.Add(customer);
                repositoryContext.Commit();

                repository.RemoveByKey(1);
                repositoryContext.Commit();

                var customersCount = repository.FindAll().Count();

                Assert.Equal(0, customersCount);
            }
        }
        public void UpdateByKeyTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var customer   = Customer.CreateOne(1, "Sunny Chen", "*****@*****.**");
                var repository = repositoryContext.GetRepository <int, Customer>();
                repository.Add(customer);
                repositoryContext.Commit();

                customer.Email = "*****@*****.**";
                repository.UpdateByKey(1, customer);
                repositoryContext.Commit();

                var updatedCustomer = repository.FindByKey(1);

                Assert.Equal("*****@*****.**", updatedCustomer.Email);
            }
        }
        public void Save2AggregateRootsTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var customers  = Customer.CreateMany(2);
                var repository = repositoryContext.GetRepository <int, Customer>();
                customers.ToList().ForEach(customer => repository.Add(customer));
                repositoryContext.Commit();

                var customersCount = repository.FindAll().Count();

                Assert.Equal(2, customersCount);
            }
        }
        public void FindAllTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(1000);
                foreach (var cust in customers)
                {
                    repository.Add(cust);
                }
                repositoryContext.Commit();

                var result = repository.FindAll();

                Assert.Equal(1000, result.Count());
            }
        }
        public void FindAllWithPredicateTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customers  = Customer.CreateMany(500);

                foreach (var cust in customers)
                {
                    repository.Add(cust);
                }
                repositoryContext.Commit();

                var subset = customers.Where(x => x.Id > 1000000);

                var result = repository.FindAll(x => x.Id > 1000000);

                Assert.Equal(subset.Count(), result.Count());
            }
        }
        public void AddNavigationPropertyTest()
        {
            using (var repositoryContext = new EntityFrameworkRepositoryContext(new SalesContext()))
            {
                var repository = repositoryContext.GetRepository <int, Customer>();
                var customer   = Customer.CreateOne();
                var id         = customer.Id;
                customer.Addresses = new List <Address> {
                    new Address {
                        Country = "China", State = "SH", City = "Shanghai", Street = "SF", ZipCode = "200000", Id = 1
                    }
                };
                repository.Add(customer);

                repositoryContext.Commit();

                var retrieved = repository.FindByKey(id);

                Assert.Single(retrieved.Addresses);
            }
        }
Esempio n. 12
0
        public async Task CreateOrUpdateProduct(ProductDto dto)
        {
            var entity = dto.To <Product>();

            _productRepository.AddOrUpdate(entity);

            var tagEntities = new List <Tag>();

            if (dto.Tags.IsNotNullOrEmpty())
            {
                var tags = dto.Tags.Split(new char[] { ',' });
                foreach (var tag in tags)
                {
                    var tagEntity = _tagRepository.Find(CmsSpecifications.TagsWithNameAndType(tag, TagType.Public));
                    if (tagEntity == null)
                    {
                        tagEntity = new Tag()
                        {
                            Id = Guid.NewGuid(), TagType = TagType.Public, Name = tag
                        };
                        _tagRepository.Add(tagEntity);
                        tagEntities.Add(tagEntity);
                    }
                }
            }

            var associatedTags    = _productTagRepository.FindAll(ErpSpecifications.TagsWithProductId(dto.Id)).ToList();
            var tagsToBeDeleted   = associatedTags.Where(x => tagEntities.Any(tag => tag.Id == x.TargetId) == false).Select(x => x.Id).ToList();
            var tagsNeedToBeAdded = tagEntities.Where(x => associatedTags.Any(a => a.TargetId == x.Id) == false).ToList();

            if (tagsToBeDeleted.Any())
            {
                _productTagRepository.RemoveAll(DomainObjectSpecifications.IdIn <ProductTag>(tagsToBeDeleted));
            }
            if (tagsNeedToBeAdded.Any())
            {
                foreach (var tagId in tagsNeedToBeAdded)
                {
                    var association = new ProductTag
                    {
                        Id       = Guid.NewGuid(),
                        SourceId = dto.Id,
                        TargetId = tagId.Id
                    };
                    _productTagRepository.Add(association);
                }
            }

            //Product Images
            var images            = dto.Images?.Select(x => Guid.Parse(x.Id)).ToList() ?? new List <Guid>();
            var associatedImages  = _productImageRepository.FindAll(ErpSpecifications.ImageWithProductId(dto.Id)).ToList();
            var imagesToBeDeleted = associatedImages.Where(x => images.Any(image => image == x.TargetId) == false).Select(x => x.Id).ToList();
            var imagesToBeAdded   = images.Where(x => associatedImages.Any(a => a.TargetId == x) == false).ToList();

            if (imagesToBeDeleted.Any())
            {
                _productImageRepository.RemoveAll(DomainObjectSpecifications.IdIn <ProductImage>(imagesToBeDeleted));
            }
            if (imagesToBeAdded.Any())
            {
                foreach (var item in imagesToBeAdded)
                {
                    var image       = dto.Images.First(x => x.Id == item.ToString());
                    var attachement = new Attachment
                    {
                        Id      = item,
                        Name    = image.Name,
                        RbsInfo = image.Url
                    };
                    _attachmentRepository.AddOrUpdate(attachement);

                    var association = new ProductImage
                    {
                        Id       = Guid.NewGuid(),
                        SourceId = dto.Id,
                        TargetId = item
                    };
                    _productImageRepository.Add(association);
                }
            }
            _repositoryContext.Commit();
        }