Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Uri uri = new Uri("http://localhost:8083/ExamDataService");
            ExamCodeFirstContext ctx = new ExamCodeFirstContext(uri);

            bool tryAgain = true;

            do
            {
                IEnumerable <Customer> customers = ctx.Customers.ToList();
                Console.WriteLine(string.Format("Retrieved {0} customers from the WCF Data Service", customers.Count()));

                // Try replacing Id with id and see how WCF throws an excellent exception!
                DataServiceQuery <Customer> customerQuery = ctx.Customers.AddQueryOption("$filter", "Id gt 1");
                customers = customerQuery.ToList();

                Console.WriteLine(string.Format("Retrieved {0} customers from the WCF Data Service using DataServiceQuery", customers.Count()));


                Console.WriteLine("Re-run? (Y/N)");
                if (Console.ReadLine() != "Y")
                {
                    tryAgain = false;
                }
            }while (tryAgain);

            Console.WriteLine("Press <Enter> to stop the client.");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
        public IEnumerable <Customer> Get()
        {
            IEnumerable <Customer> customers = default(IEnumerable <Customer>);

            // *************************************************************************************************************************
            // The ADO.NET way
            // *************************************************************************************************************************
            using (IReadRepository <Customer> customerRepository = new CustomerRepository(nameof(ExamCodeFirstContext)))
            {
                customers = customerRepository.Get();
            }

            // *************************************************************************************************************************
            // The Entity Framework way
            // *************************************************************************************************************************
            using (IRepository <Customer> customerRepository = new EfRepository <Customer>(new ExamCodeFirstContext()))
            {
                customers = customerRepository.Get();
            }

            // *************************************************************************************************************************
            // The Model First Way
            // *************************************************************************************************************************
            using (IRepository <Core.Model_First.Customer> customerRepository = new EfRepository <Core.Model_First.Customer>(new Core.Model_First.ExamModelFirstContext()))
            {
                IEnumerable <Core.Model_First.Customer> customersFromModelFirst = customerRepository.Get();
            }

            // *************************************************************************************************************************
            // The DI way
            // *************************************************************************************************************************
            customers = this.CustomerRepository.Get();

            // *************************************************************************************************************************
            // The good old way of the ObjectContext
            // *************************************************************************************************************************
            if (this.CustomerRepository is EfRepository <Customer> )
            {
                // Use explicit conversion to get the DbContext from the repository without exposing the DbContext property as public
                ExamCodeFirstContext ctx = (ExamCodeFirstContext)(EfRepository <Customer>) this.CustomerRepository;

                IQueryable <Customer> customersDeferred = ctx.Customers.Where(x => x.Id > 1);
                customersDeferred.Load();
                customersDeferred.Where(x => x.FirstName == "Donald");

                IQueryable <Customer> customersDeferred2 = ctx.Customers.Where(x => x.Id > 1);
                customersDeferred2.Where(x => x.FirstName == "Donald");

                // Use the adapter to get to the old ObjectContext type
                IObjectContextAdapter adapter       = (IObjectContextAdapter)ctx;
                ObjectContext         objectContext = adapter.ObjectContext;

                // Create same query as the others with the ObjectContext directly
                ObjectQuery <Customer> customersQuery = objectContext.CreateObjectSet <Customer>();

                // Get the trace string of the objcect query
                string query = customersQuery.ToTraceString();

                customers = customersQuery;
            }


            return(customers.ToList());
        }