private static async Task TestGetWithThreeCompositePrimaryKeyAsync(IUnitOfWorkFactory uowFactory)
        {
            var repo = new Service <CustomerWithThreeCompositePrimaryKey, int, string, int>(uowFactory);

            var key1      = 1;
            var key2      = "2";
            var key3      = 3;
            var randomKey = 4;

            var fetchStrategy = new FetchQueryStrategy <CustomerWithThreeCompositePrimaryKey>();

            var entity = new CustomerWithThreeCompositePrimaryKey {
                Id1 = key1, Id2 = key2, Id3 = key3, Name = "Random Name"
            };

            Assert.Null(await repo.GetAsync(key1, key2, key3));
            Assert.Null(await repo.GetAsync(key1, key2, key3, fetchStrategy));

            repo.Create(entity);

            Assert.Null(await repo.GetAsync(key1, key2, randomKey));
            Assert.Null(await repo.GetAsync(key1, key2, randomKey, fetchStrategy));
            Assert.NotNull(await repo.GetAsync(key1, key2, key3));
            Assert.NotNull(await repo.GetAsync(key1, key2, key3, fetchStrategy));
        }
Exemple #2
0
        private static async Task TestGetWithIdAsync(IServiceFactory serviceFactory)
        {
            var service = serviceFactory.Create <Customer>();

            int key = 1;

            var fetchStrategy = new FetchQueryStrategy <Customer>()
                                .Fetch(x => x.Address);

            var entity = new Customer {
                Id = key, Name = "Random Name"
            };

            Assert.Null(await service.GetAsync(key));
            Assert.Null(await service.GetAsync(key, fetchStrategy));
            Assert.Null(await service.GetAsync(key, x => x.Address));
            Assert.Null(await service.GetAsync(key, "Address"));

            await service.CreateAsync(entity);

            Assert.NotNull(await service.GetAsync(key));
            Assert.NotNull(await service.GetAsync(key, fetchStrategy));
            Assert.NotNull(await service.GetAsync(key, x => x.Address));
            Assert.NotNull(await service.GetAsync(key, "Address"));
        }
        public void GetWithId()
        {
            int key = 1;
            Expression <Func <Customer, bool> > predicate = x => true;
            var fetchStrategy = new FetchQueryStrategy <Customer>();
            Expression <Func <Customer, string> > selector = x => x.Name;

            var mock = new Mock <IService <Customer> >();

            mock.Setup(x => x.Get(It.IsAny <int>()));
            mock.Setup(x => x.Get(It.IsAny <int>(), It.IsAny <IFetchQueryStrategy <Customer> >()));
            mock.Setup(x => x.Get(It.IsAny <int>(), It.IsAny <string[]>()));
            mock.Setup(x => x.Get(It.IsAny <int>(), It.IsAny <Expression <Func <Customer, object> >[]>()));

            var readOnlyService = new ReadOnlyServiceWrapper <Customer, int>(mock.Object);

            readOnlyService.Get(key);
            readOnlyService.Get(key, fetchStrategy);
            readOnlyService.Get(key, string.Empty);
            readOnlyService.Get(key, (Expression <Func <Customer, object> >[])null);

            mock.Verify(x => x.Get(key), Times.Once);
            mock.Verify(x => x.Get(key, fetchStrategy), Times.Once);
            mock.Verify(x => x.Get(key, string.Empty), Times.Once);
            mock.Verify(x => x.Get(key, (Expression <Func <Customer, object> >[])null), Times.Once);
        }
        /// <summary>
        /// Returns the fetch strategy, or a default valued if the sequence is empty.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="conventions">The configurable conventions.</param>
        /// <returns>The fetching strategy.</returns>
        public static IFetchQueryStrategy <T> DefaultIfFetchStrategyEmpty <T>([CanBeNull] this IQueryOptions <T> source, [NotNull] IRepositoryConventions conventions)
        {
            Guard.NotNull(conventions, nameof(conventions));

            return(source?.FetchStrategy != null && source.FetchStrategy.PropertyPaths.Any()
                ? source.FetchStrategy
                : FetchQueryStrategy <T> .Default(conventions));
        }
Exemple #5
0
        public void FetchToString()
        {
            var strategy = new FetchQueryStrategy <Customer>()
                           .Fetch("Address1")
                           .Fetch("Phone")
                           .Fetch("Phone.Customer");

            Assert.Equal("FetchQueryStrategy<Customer>: [ Paths = Address1, Phone, Phone.Customer ]", strategy.ToString());
        }
Exemple #6
0
        public void FetchPropertiesByName()
        {
            var strategy = new FetchQueryStrategy <Customer>()
                           .Fetch("Address1")
                           .Fetch("Phone")
                           .Fetch("Phone.Customer");

            Assert.Contains("Address1", strategy.PropertyPaths);
            Assert.Contains("Phone", strategy.PropertyPaths);
            Assert.Contains("Phone.Customer", strategy.PropertyPaths);
        }
Exemple #7
0
        public void FetchPropertiesByExpression()
        {
            var strategy = new FetchQueryStrategy <Customer>()
                           .Fetch(x => x.Address1)
                           .Fetch(x => x.Phone)
                           .Fetch(x => x.Phone.Customer);

            Assert.Contains("Address1", strategy.PropertyPaths);
            Assert.Contains("Phone", strategy.PropertyPaths);
            Assert.Contains("Phone.Customer", strategy.PropertyPaths);
        }
        /// <summary>
        /// Converts the specified paths to <see cref="IFetchQueryStrategy{T}" />.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <param name="paths">A collection of lambda expressions representing the paths to include.</param>
        /// <returns>The new fetch strategy options instance.</returns>
        public static IFetchQueryStrategy <T> ToFetchQueryStrategy <T>([NotNull] this Expression <Func <T, object> >[] paths)
        {
            Guard.NotNull(paths, nameof(paths));

            var fetchStrategy = new FetchQueryStrategy <T>();

            if (paths.Length > 0)
            {
                foreach (var path in paths)
                {
                    fetchStrategy.Fetch(path);
                }
            }

            return(fetchStrategy);
        }
Exemple #9
0
        public void FindWithId()
        {
            int key = 1;
            Expression <Func <Customer, bool> > predicate = x => true;
            var fetchStrategy = new FetchQueryStrategy <Customer>();
            Expression <Func <Customer, string> > selector = x => x.Name;

            var mock = new Mock <IRepository <Customer> >();

            mock.Setup(x => x.Find(It.IsAny <int>()));
            mock.Setup(x => x.Find(It.IsAny <int>(), It.IsAny <IFetchQueryStrategy <Customer> >()));

            var readOnlyRepo = new ReadOnlyRepositoryWrapper <Customer, int>(mock.Object);

            readOnlyRepo.Find(key);
            readOnlyRepo.Find(key, fetchStrategy);

            mock.Verify(x => x.Find(key), Times.Once);
            mock.Verify(x => x.Find(key, fetchStrategy), Times.Once);
        }
Exemple #10
0
        private static async Task TestGetWithTwoCompositePrimaryKeyAsync(IServiceFactory serviceFactory)
        {
            var repo = serviceFactory.Create <CustomerWithTwoCompositePrimaryKey, int, string>();

            var key1 = 1;
            var key2 = "2";

            var fetchStrategy = new FetchQueryStrategy <CustomerWithTwoCompositePrimaryKey>();

            var entity = new CustomerWithTwoCompositePrimaryKey {
                Id1 = key1, Id2 = key2, Name = "Random Name"
            };

            Assert.Null(await repo.GetAsync(key1, key2));
            Assert.Null(await repo.GetAsync(key1, key2, fetchStrategy));

            await repo.CreateAsync(entity);

            Assert.NotNull(await repo.GetAsync(key1, key2));
            Assert.NotNull(await repo.GetAsync(key1, key2, fetchStrategy));
        }
Exemple #11
0
        private static void TestGetWithThreeCompositePrimaryKey(IServiceFactory serviceFactory)
        {
            var repo = serviceFactory.Create <CustomerWithThreeCompositePrimaryKey, int, string, int>();

            var key1 = 1;
            var key2 = "2";
            var key3 = 3;

            var fetchStrategy = new FetchQueryStrategy <CustomerWithThreeCompositePrimaryKey>();

            var entity = new CustomerWithThreeCompositePrimaryKey {
                Id1 = key1, Id2 = key2, Id3 = key3, Name = "Random Name"
            };

            Assert.Null(repo.Get(key1, key2, key3));
            Assert.Null(repo.Get(key1, key2, key3, fetchStrategy));

            repo.Create(entity);

            Assert.NotNull(repo.Get(key1, key2, key3));
            Assert.NotNull(repo.Get(key1, key2, key3, fetchStrategy));
        }
        private static async Task TestGetWithIdAsync(IUnitOfWorkFactory uowFactory)
        {
            var service = new Service <Customer>(uowFactory);

            int          key  = 1;
            const string name = "Random Name";

            var fetchStrategy = new FetchQueryStrategy <Customer>();

            fetchStrategy.Fetch(x => x.Address);

            var entity = new Customer {
                Id = key, Name = name
            };

            Assert.Null(await service.GetAsync(key));
            Assert.Null(await service.GetAsync(key, fetchStrategy));

            await service.CreateAsync(entity);

            Assert.NotNull(await service.GetAsync(key));
            Assert.NotNull(await service.GetAsync(key, fetchStrategy));
        }
        public async Task GetWithIdAsync()
        {
            int key = 1;
            Expression <Func <Customer, bool> > predicate = x => true;
            var fetchStrategy = new FetchQueryStrategy <Customer>();
            Expression <Func <Customer, string> > selector = x => x.Name;

            var mock = new Mock <IService <Customer> >();

            mock.Setup(x => x.GetAsync(It.IsAny <int>(), It.IsAny <CancellationToken>()));
            mock.Setup(x => x.GetAsync(It.IsAny <int>(), It.IsAny <IFetchQueryStrategy <Customer> >(), It.IsAny <CancellationToken>()));
            mock.Setup(x => x.GetAsync(It.IsAny <int>(), It.IsAny <string[]>(), It.IsAny <CancellationToken>()));
            mock.Setup(x => x.GetAsync(It.IsAny <int>(), It.IsAny <string[]>()));
            mock.Setup(x => x.GetAsync(It.IsAny <int>(), It.IsAny <Expression <Func <Customer, object> >[]>(), It.IsAny <CancellationToken>()));
            mock.Setup(x => x.GetAsync(It.IsAny <int>(), It.IsAny <Expression <Func <Customer, object> >[]>()));

            var readOnlyService = new ReadOnlyServiceWrapper <Customer, int>(mock.Object);

            await readOnlyService.GetAsync(key);

            await readOnlyService.GetAsync(key, fetchStrategy);

            await readOnlyService.GetAsync(key, new[] { string.Empty }, default(CancellationToken));

            await readOnlyService.GetAsync(key, string.Empty);

            await readOnlyService.GetAsync(key, (Expression <Func <Customer, object> >[]) null);

            await readOnlyService.GetAsync(key, (Expression <Func <Customer, object> >[]) null, default(CancellationToken));

            mock.Verify(x => x.GetAsync(key, default(CancellationToken)), Times.Once);
            mock.Verify(x => x.GetAsync(key, fetchStrategy, default(CancellationToken)), Times.Once);
            mock.Verify(x => x.GetAsync(key, new[] { string.Empty }, default(CancellationToken)), Times.Once);
            mock.Verify(x => x.GetAsync(key, string.Empty), Times.Once);
            mock.Verify(x => x.GetAsync(key, (Expression <Func <Customer, object> >[])null), Times.Once);
            mock.Verify(x => x.GetAsync(key, (Expression <Func <Customer, object> >[])null, default(CancellationToken)), Times.Once);
        }
        private static void TestGetWithTwoCompositePrimaryKey(IUnitOfWorkFactory uowFactory)
        {
            var repo = new Service <CustomerWithTwoCompositePrimaryKey, int, string>(uowFactory);

            var key1      = 1;
            var key2      = "2";
            var randomKey = "3";

            var fetchStrategy = new FetchQueryStrategy <CustomerWithTwoCompositePrimaryKey>();

            var entity = new CustomerWithTwoCompositePrimaryKey {
                Id1 = key1, Id2 = key2, Name = "Random Name"
            };

            Assert.Null(repo.Get(key1, key2));
            Assert.Null(repo.Get(key1, key2, fetchStrategy));

            repo.Create(entity);

            Assert.Null(repo.Get(key1, randomKey));
            Assert.Null(repo.Get(key1, randomKey, fetchStrategy));
            Assert.NotNull(repo.Get(key1, key2));
            Assert.NotNull(repo.Get(key1, key2, fetchStrategy));
        }