Esempio n. 1
0
        public void LinqTestOne()
        {
            IList<Address> addresses;

            // Regular EF call without AOP:
            using(var db = new AdventureWorksLT2008R2Entities())
            {
                db.Configuration.LazyLoadingEnabled = false;
                addresses = db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses).ToList();
            }
            Assert.AreEqual(2, addresses.Count);

            // Now same with LINQ-friendly AOP shortcuts:

            // Example 1: where AOP creates instance of AdventureWorksLT2008R2Entities, runs the DAL method,
            // and disposes AdventureWorksLT2008R2Entities instance - all in one shot.
            addresses = AwDal.List(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));
            Assert.AreEqual(2, addresses.Count);

            // Example 2: with simple AOP proxied call for existing instance of DbContext.
            using(var db = new AdventureWorksLT2008R2Entities())
            {
                db.Configuration.LazyLoadingEnabled = false;
                addresses = db.GetDbProxy(TestAspects).List(dbx => dbx.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));
            }
            Assert.AreEqual(2, addresses.Count);

            var address = AwDal.Single(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));
            Assert.IsNotNull(address);

            long adrressCount = AwDal.Count(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses));
            Assert.AreEqual(2, adrressCount);

            adrressCount = AwDal.Count(db => db.QueryCustomerAddressesByCustomerID(CustomerIdWithManyAddresses), new QueryModifiers().AddSortCriteria("AddressID").AddPaging(0, 1));
            Assert.AreEqual(1, adrressCount);
        }
Esempio n. 2
0
        public void TestExecuteCommand()
        {
            Customer mrAndrewCencini;
            const string originalEmail = "*****@*****.**";
            const string anotherEmail = "*****@*****.**";
            string replacementEmail;

            using(var dbc = new AdventureWorksLT2008R2Entities())
            {
                dbc.Configuration.LazyLoadingEnabled = false;

                mrAndrewCencini = dbc.GetDbProxy(TestAspects).Single(db => db.QueryCustomerByID(163));

                replacementEmail = mrAndrewCencini.EmailAddress == originalEmail ? anotherEmail : originalEmail;
                mrAndrewCencini.EmailAddress = replacementEmail;

                int recordsTouched = dbc.GetDbProxy(TestAspects).ExecuteCommand(db => db.ToStringEx(""));
                Assert.AreEqual(1, recordsTouched);
                mrAndrewCencini = dbc.GetDbProxy(TestAspects).Single(db => db.QueryCustomerByID(163));
            }

            Assert.AreEqual(mrAndrewCencini.EmailAddress, replacementEmail);

            //Customer mrAndrewCencini = new Customer { CustomerID = 163 };
            //int retVal = AwDal.ExecuteCommand(db => db.DeleteEntity(mrAndrewCencini));
            //Assert.AreEqual(1, retVal);
        }