当前位置: 首页>>代码示例>>C#>>正文


C# InMemoryRepository.GetAll方法代码示例

本文整理汇总了C#中InMemoryRepository.GetAll方法的典型用法代码示例。如果您正苦于以下问题:C# InMemoryRepository.GetAll方法的具体用法?C# InMemoryRepository.GetAll怎么用?C# InMemoryRepository.GetAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在InMemoryRepository的用法示例。


在下文中一共展示了InMemoryRepository.GetAll方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Repository_Handles_Sorting

        public void Repository_Handles_Sorting()
        {
            var repo = new InMemoryRepository<Order>();
            repo.Add(OrdersToLoad());

            // there are 2 ways to handle sorting, there is an Expression based way
            //  and a "magic string" based approach.
            // Why the 2 approaches?
            //  For convenience really.  In a Web based applicaiton sometimes it is easier to
            //  post back a string that represents the properrty that you want to sort on.

            // First, the Expression way
            var descendingOrders = repo.GetAll(new SortingOptions<Order, DateTime>(x => x.OrderDate, isDescending: true));
            descendingOrders.First().OrderId.ShouldEqual(1);

            var ascendingOrders = repo.GetAll(new SortingOptions<Order, DateTime>(x => x.OrderDate, isDescending: false));
            ascendingOrders.First().OrderId.ShouldEqual(2);

            // You can also combine sortings and selectors (See HowToUseGetSelectors for more info)
            var descendingNames = repo.GetAll(x => x.Name, new SortingOptions<Order, DateTime>(x => x.OrderDate, isDescending: true));
            descendingNames.First().ShouldEqual("Order 1");

            // The Magic String approach to sorting
            //  you can see that you don't need the second generic type (the property type to sort on), just the name of the property
            ascendingOrders = repo.GetAll(new SortingOptions<Order>("OrderDate", isDescending: false));
            ascendingOrders.First().OrderId.ShouldEqual(2);

            // using sorting with FindAll
            var minDate = DateTime.Now.AddDays(-7);
            var ordersWithinAWeek = repo.FindAll(x => x.OrderDate > minDate, new SortingOptions<Order, double>(x => x.Total, true));
            ordersWithinAWeek.Count().ShouldEqual(2);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:32,代码来源:HowToUsePagingAndSorting.cs

示例2: Batch_Can_Be_Rolled_Back

        public void Batch_Can_Be_Rolled_Back()
        {
            var repo = new InMemoryRepository<Order, int>();

            using (var batch = repo.BeginBatch())
            {
                batch.Add(new Order { Name = "Order 1" });
                batch.Add(new Order { Name = "Order 2" });
                repo.GetAll().Count().ShouldEqual(0);
                batch.Rollback();
                batch.Commit();
            }

            repo.GetAll().Count().ShouldEqual(0);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:15,代码来源:HowToBatch.cs

示例3: Add_Operations_Can_Be_Batched

        public void Add_Operations_Can_Be_Batched()
        {
            var repo = new InMemoryRepository<Order, int>();

            using (var batch = repo.BeginBatch())
            {
                batch.Add(new Order { Name = "Order 1" });
                batch.Add(new Order { Name = "Order 2" });
                repo.GetAll().Count().ShouldEqual(0);

                batch.Commit();
            }

            repo.GetAll().Count().ShouldEqual(2);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:15,代码来源:HowToBatch.cs

示例4: Repository_Supports_Selectors

        public void Repository_Supports_Selectors()
        {
            var repo = new InMemoryRepository<Order>();

            // let's add a couple of orders to work with
            repo.Add(new Order()
                         {
                             Name = "Order 1",
                             Total = 120.00,
                             OrderDate = new DateTime(2013, 4, 26)
                         });

            repo.Add(new Order()
                         {
                             Name = "Order 2",
                             Total = 80.00,
                             OrderDate = new DateTime(2013, 4, 24)
                         });

            // normal Get method
            var order = repo.Get(1);
            order.OrderId.ShouldEqual(1);

            // in this case we only need the order name
            var orderName = repo.Get(1, x => x.Name);
            orderName.ShouldEqual("Order 1");

            // we can also bring back an anonymous type if needed
            var anonymousType = repo.Get(1, x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 });
            anonymousType.IsExpensiveOrder.ShouldBeTrue();

            // or we can map it to a specific type we have defined like a ViewModel
            var viewModel = repo.Get(1, x => new OrderViewModel() {Name = x.Name, IsExpensiveOrder = x.Total > 100.0});
            viewModel.IsExpensiveOrder.ShouldBeTrue();

            // We have the same options with the GetAll, Find and FindAll as well
            orderName = repo.Find(x => x.OrderId == 2, x => x.Name);
            orderName.ShouldEqual("Order 2");

            // we can also bring back an anonymous type if needed
            var anonymousTypes = repo.GetAll(x => new { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 }).ToList();
            anonymousTypes.Count.ShouldEqual(2);
            anonymousTypes.First().Name.ShouldEqual("Order 1");
            anonymousTypes.First().IsExpensiveOrder.ShouldBeTrue();

            anonymousTypes.Last().Name.ShouldEqual("Order 2");
            anonymousTypes.Last().IsExpensiveOrder.ShouldBeFalse();

            // or we can map it to a specific type we have defined like a ViewModel
            var viewModels = repo.FindAll(x => x.OrderId < 5, x => new OrderViewModel() { Name = x.Name, IsExpensiveOrder = x.Total > 100.0 }).ToList();
            viewModels.Count.ShouldEqual(2);
            viewModels.First().Name.ShouldEqual("Order 1");
            viewModels.First().IsExpensiveOrder.ShouldBeTrue();

            viewModels.Last().Name.ShouldEqual("Order 2");
            viewModels.Last().IsExpensiveOrder.ShouldBeFalse();
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:57,代码来源:HowToUseGetSelectors.cs

示例5: Repository_Should_BeginBatch

        public void Repository_Should_BeginBatch()
        {
            var repository = new InMemoryRepository<Contact, Int32>();

            using (var batch = repository.BeginBatch())
            {
                batch.Add(new Contact { Name = "Test User 1" });

                var result = repository.GetAll();
                result.Count().ShouldEqual(0); // shouldn't have really been added yet

                batch.Add(new Contact { Name = "Test User 2" });

                result = repository.GetAll();
                result.Count().ShouldEqual(0); // shouldn't have really been added yet

                batch.Commit();
            }

            repository.GetAll().Count().ShouldEqual(2);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:21,代码来源:BatchSpike.cs

示例6: Main

        static void Main(string[] args)
        {
            var repo = new InMemoryRepository<Foo>();

            repo.Insert(new Foo());
            repo.Insert(new Foo());
            repo.Insert(new Foo());

            var entities = repo.GetAll();

            Console.WriteLine(entities.Count());
        }
开发者ID:righettig,项目名称:GenericRepository,代码行数:12,代码来源:Program.cs

示例7: Repository_Handles_Pagination

        public void Repository_Handles_Pagination()
        {
            var repo = new InMemoryRepository<Order, int>();
            repo.Add(OrdersToLoad());

            // with PagingOptions you give it the pageNumber, number per page, and the sorting options
            //      since the sorting options are the same as described above, I will just do the Expression based approach here
            var pagingOptions = new PagingOptions<Order, DateTime>(1, 2, x => x.OrderDate, isDescending: true);
            var pageOneOrders = repo.GetAll(pagingOptions).ToList();

            pageOneOrders.Count.ShouldEqual(2);
            pageOneOrders.First().OrderId.ShouldEqual(1);
            pagingOptions.TotalItems.ShouldEqual(3);

            // now we can get the second page of results
            pagingOptions.PageNumber = 2;

            var pageTwoOrders = repo.GetAll(pagingOptions).ToList();

            pageTwoOrders.Count.ShouldEqual(1);
            pageTwoOrders.First().OrderId.ShouldEqual(2);
            pagingOptions.TotalItems.ShouldEqual(3);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:23,代码来源:HowToUsePagingAndSorting.cs

示例8: ExecuteGetAll_With_Selector_Should_Use_Cache_After_First_Call

        public void ExecuteGetAll_With_Selector_Should_Use_Cache_After_First_Call()
        {
            var repos = new InMemoryRepository<Contact>(new StandardCachingStrategy<Contact>());

            repos.Add(new Contact { Name = "Test1"});
            repos.Add(new Contact { Name = "Test2"});

            var items = repos.GetAll(x => x.Name);
            repos.CacheUsed.ShouldBeFalse();
            items.Count().ShouldEqual(2);

            items = repos.GetAll(x => x.Name);
            repos.CacheUsed.ShouldBeTrue();
            items.Count().ShouldEqual(2);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:15,代码来源:InMemoryCachingTests.cs

示例9: ExecuteGetAll_With_Paging_Should_Save_TotalItems_In_Cache

        public void ExecuteGetAll_With_Paging_Should_Save_TotalItems_In_Cache()
        {
            var repos = new InMemoryRepository<Contact>(new StandardCachingStrategy<Contact>());

            repos.Add(new Contact { ContactId = 1, Name = "Test1" });
            repos.Add(new Contact { ContactId = 2, Name = "Test2" });
            repos.Add(new Contact { ContactId = 3, Name = "Test3" });
            repos.Add(new Contact { ContactId = 4, Name = "Test4" });

            var pagingOptions = new PagingOptions<Contact>(1, 1, "Name");

            var items = repos.GetAll(x => x.Name, pagingOptions);
            repos.CacheUsed.ShouldBeFalse();
            items.Count().ShouldEqual(1);
            pagingOptions.TotalItems.ShouldEqual(4);

            // reset paging options so the TotalItems is default
            pagingOptions = new PagingOptions<Contact>(1, 1, "Name");

            items = repos.GetAll(x => x.Name, pagingOptions);
            repos.CacheUsed.ShouldBeTrue();
            items.Count().ShouldEqual(1);
            pagingOptions.TotalItems.ShouldEqual(4);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:24,代码来源:InMemoryCachingTests.cs

示例10: Update_Operations_Can_Be_Batched

        public void Update_Operations_Can_Be_Batched()
        {
            var repo = new InMemoryRepository<Order, int>();
            repo.Add(new Order { Name = "Order 1" });
            repo.Add(new Order { Name = "Order 2" });
            repo.GetAll().Count().ShouldEqual(2);

            using (var batch = repo.BeginBatch())
            {
                foreach (var order in repo.GetAll())
                {
                    order.Name = "Updated";
                    batch.Update(order);
                }

                repo.GetAll().Count().ShouldEqual(2);
                repo.GetAll().Count(x => x.Name.StartsWith("Update")).ShouldEqual(0);

                batch.Commit();
            }

            repo.GetAll().Count().ShouldEqual(2);
            repo.GetAll().Count(x => x.Name.StartsWith("Update")).ShouldEqual(2);
        }
开发者ID:mgmccarthy,项目名称:SharpRepository,代码行数:24,代码来源:HowToBatch.cs


注:本文中的InMemoryRepository.GetAll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。