Esempio n. 1
0
        public void MongoDBRepositoryTests_RetrieveByOrSpecificationTest()
        {
            Customer[] customers = new Customer[] 
            { 
                new Customer{ ID=Helper.AggregateRootId1, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust1", LastName="cust1", Password="******", Username="******"},
                new Customer{ ID=Helper.AggregateRootId2, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="aaa", LastName="bbb", Password="******", Username="******"},
                new Customer{ ID=Helper.AggregateRootId3, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust3", LastName="cust3", Password="******", Username="******"}
            };

            IRepository<Customer> repository = ServiceLocator.Instance.GetService<IRepository<Customer>>();
            foreach (var cust in customers)
                repository.Add(cust);
            repository.Context.Commit();

            ISpecification<Customer> spec = Specification<Customer>.Eval(p => p.FirstName.StartsWith("cust")).Or(Specification<Customer>.Eval(p => p.FirstName == "aaa"));
            var c = repository.FindAll(spec);
            repository.Context.Dispose();
            Assert.IsNotNull(c);
            Assert.AreEqual(3, c.Count());
        }
        public void MyTestInitialize()
        {
            NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
            config.Properties["connection.driver_class"] = "NHibernate.Driver.SqlClientDriver";
            config.Properties["connection.provider"] = "NHibernate.Connection.DriverConnectionProvider";
            config.Properties["connection.connection_string"] = Helper.ClassicTestDB_SQLExpress_ConnectionString;
            config.Properties["dialect"] = "NHibernate.Dialect.MsSql2008Dialect";
            config.Properties["proxyfactory.factory_class"] = "NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu";
            config.AddAssembly(typeof(Customer).Assembly);
            NHibernate.Tool.hbm2ddl.SchemaExport schemaExport = new NHibernate.Tool.hbm2ddl.SchemaExport(config);
            schemaExport.Execute(false, true, false);

            customer = new Customer
            {
                //Id = Guid.NewGuid(),
                Birth = DateTime.Now,
                Email = "*****@*****.**",
                FirstName = "dax",
                LastName = "net",
                Password = "******",
                Username = "******"
            };
        }
        public void NHibernateRepositoryTests_ChangeCustomerNameTest()
        {
            Customer customer = new Customer
            {
                Birth = DateTime.Now.AddYears(-20),
                Email = "*****@*****.**",
                FirstName = "dax",
                LastName = "net",
                Password = "******",
                Username = "******"
            };

            IRepository<Customer> customerRepository = ServiceLocator.Instance.GetService<IRepository<Customer>>();
            customerRepository.Add(customer);
            customerRepository.Context.Commit();
            ISpecification<Customer> spec = Specification<Customer>.Eval(p => p.Username.Equals("daxnet"));

            var customer2 = customerRepository.Find(spec);
            Assert.AreEqual(customer.Username, customer2.Username);

            customer2.FirstName = "qingyang";
            customer2.LastName = "chen";
            customerRepository.Update(customer2);
            customerRepository.Context.Commit();


            var customer3 = customerRepository.Find(spec);
            customerRepository.Context.Dispose();
            Assert.AreEqual("qingyang", customer3.FirstName);
            Assert.AreEqual("chen", customer3.LastName);
        }
        public void NHibernateRepositoryTests_RetrieveAllAggregateRootBySpecificationAndSortingTest()
        {
            // construct the aggregate root array.
            Customer[] customers = new Customer[] 
            { 
                customer,
                new Customer{ ID = Helper.AggregateRootId1, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust1", LastName="cust1", Password="******", Username="******"},
                new Customer{ ID = Helper.AggregateRootId2, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust2", LastName="cust2", Password="******", Username="******"},
                new Customer{ ID = Helper.AggregateRootId3, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust3", LastName="cust3", Password="******", Username="******"}
            };

            IRepository<Customer> customerRepository = ServiceLocator.Instance.GetService<IRepository<Customer>>();
            foreach (var cust in customers)
            {
                customerRepository.Add(cust);
            }
            customerRepository.Context.Commit();

            ISpecification<Customer> spec = Specification<Customer>.Eval(p => p.FirstName.StartsWith("cust"));
            var query = customerRepository.FindAll(spec, c => c.Email, Storage.SortOrder.Descending);
            var count = query.Count();
            var custs = query.First();
            customerRepository.Context.Dispose();
            Assert.IsNotNull(custs);
            Assert.AreEqual<int>(3, count);
            Assert.AreEqual<string>("*****@*****.**", custs.Email);
        }
        public void NHibernateRepositoryTests_RetrieveAggregateRootBySpecificationTest()
        {
            // construct the aggregate root array.
            Customer[] customers = new Customer[] 
            { 
                customer,
                new Customer{ ID=Helper.AggregateRootId1, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust1", LastName="cust1", Password="******", Username="******"},
                new Customer{ ID=Helper.AggregateRootId2, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust2", LastName="cust2", Password="******", Username="******"},
                new Customer{ ID=Helper.AggregateRootId3, Birth=DateTime.Now.AddYears(-23), Email="*****@*****.**", FirstName="cust3", LastName="cust3", Password="******", Username="******"}
            };

            IRepository<Customer> customerRepository = ServiceLocator.Instance.GetService<IRepository<Customer>>();
            foreach (var cust in customers)
            {
                customerRepository.Add(cust);
            }
            customerRepository.Context.Commit();

            ISpecification<Customer> spec = Specification<Customer>.Eval(p => p.FirstName.Equals("cust2"));
            var c = customerRepository.Find(spec);
            customerRepository.Context.Dispose();
            Assert.IsNotNull(c);
            Assert.IsNotNull(c.ID);
        }