Пример #1
0
        private void RegisterGenericRepositories()
        {
            var dbContextTypes =
                _typeFinder.Find(type =>
                                 type.IsPublic &&
                                 !type.IsAbstract &&
                                 type.IsClass &&
                                 typeof(EddoDbContext).IsAssignableFrom(type)
                                 );

            if (dbContextTypes.IsNullOrEmpty())
            {
                Logger.Warn("没有发现类EddoDbContext");
                return;
            }

            foreach (var dbContextType in dbContextTypes)
            {
                EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext(dbContextType, IocManager);
            }
            using (var dbContextMatcher = IocManager.ResolveAsDisposable <IDbContextTypeMatcher>())
            {
                dbContextMatcher.Object.Populate(dbContextTypes);
            }
        }
        public EntityFrameworkGenericRepositoryRegistrar1_Tests()
        {
            var fakeMainDbContextProvider   = NSubstitute.Substitute.For <IDbContextProvider <MyMainDbContext <int, long>, int, long> >();
            var fakeModuleDbContextProvider = NSubstitute.Substitute.For <IDbContextProvider <MyModuleDbContext <int, long>, int, long> >();

            LocalIocManager.IocContainer.Register(
                Component.For <ITypeFinder>().ImplementedBy <TypeFinder>(),
                Component.For <IDbContextProvider <MyMainDbContext <int, long>, int, long> >().UsingFactoryMethod(() => fakeMainDbContextProvider),
                Component.For <IDbContextProvider <MyModuleDbContext <int, long>, int, long> >().UsingFactoryMethod(() => fakeModuleDbContextProvider)
                );

            var typeFinder = LocalIocManager.Resolve <ITypeFinder>();
            var finder     = typeFinder as TypeFinder;

            finder.AssemblyFinder = new Repositories.AssembleFinder();


            var entities = finder.Find(
                x => (
                    typeof(IEntity).IsAssignableFrom(x) ||
                    x.IsInheritsOrImplements(typeof(IEntity <>))
                    )
                );

            //EntityFrameworkGenericRepositoryRegistrar.RegisterGenericRepositories<int, long>(finder, LocalIocManager);
            //EntityFrameworkGenericRepositoryRegistrar.RegisterGenericRepositories<int, long>(typeof(MyMainDbContext), LocalIocManager);

            EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext <int, long>(typeof(MyModuleDbContext <int, long>), LocalIocManager);
            EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext <int, long>(typeof(MyMainDbContext <int, long>), LocalIocManager);
        }
Пример #3
0
        public EntityFrameworkGenericRepositoryRegistrar_Tests()
        {
            var fakeMainDbContextProvider   = NSubstitute.Substitute.For <IDbContextProvider <MyMainDbContext> >();
            var fakeModuleDbContextProvider = NSubstitute.Substitute.For <IDbContextProvider <MyModuleDbContext> >();

            LocalIocManager.IocContainer.Register(
                Component.For <IDbContextProvider <MyMainDbContext> >().UsingFactoryMethod(() => fakeMainDbContextProvider),
                Component.For <IDbContextProvider <MyModuleDbContext> >().UsingFactoryMethod(() => fakeModuleDbContextProvider)
                );

            EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext(typeof(MyModuleDbContext), LocalIocManager);
            EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext(typeof(MyMainDbContext), LocalIocManager);
        }
Пример #4
0
        private void RegisterGenericRepositories()
        {
            var dbContextTypes = (
                from assembly in AssemblyFinder.GetAllAssemblies()
                from type in assembly.GetTypes()
                where type.IsPublic && !type.IsAbstract && type.IsClass && typeof(AbpDbContext).IsAssignableFrom(type)
                select type
                ).ToList();

            foreach (var dbContextType in dbContextTypes)
            {
                EntityFrameworkGenericRepositoryRegistrar.RegisterDbContext(dbContextType, IocManager);
            }
        }
        public void Should_Resolve_Generic_Repositories()
        {
            EntityFrameworkGenericRepositoryRegistrar.RegisterDbContext(typeof(MyDbContext), LocalIocManager);

            var entity1Repository = LocalIocManager.Resolve <IRepository <MyEntity1> >();

            entity1Repository.ShouldNotBe(null);

            var entity1RepositoryWithPk = LocalIocManager.Resolve <IRepository <MyEntity1, int> >();

            entity1RepositoryWithPk.ShouldNotBe(null);

            var entity2Repository = LocalIocManager.Resolve <IRepository <MyEntity2, long> >();

            entity2Repository.ShouldNotBe(null);
        }
Пример #6
0
        private void RegisterGenericRepositories()
        {
            //TODO: Refactor this code. Also, it's similar to DefaultModuleFinder.FindAll

            var allAssemblies = AssemblyFinder.GetAllAssemblies().Distinct();

            foreach (var assembly in allAssemblies)
            {
                Type[] types;

                try
                {
                    types = assembly.GetTypes();
                }
                catch (ReflectionTypeLoadException ex)
                {
                    types = ex.Types;
                }

                if (types.IsNullOrEmpty())
                {
                    continue;
                }

                var dbContextTypes = (
                    from type in types
                    where
                    type.IsPublic && !type.IsAbstract && type.IsClass &&
                    typeof(AbpDbContext).IsAssignableFrom(type)
                    select type
                    ).ToArray();

                if (dbContextTypes.IsNullOrEmpty())
                {
                    continue;
                }

                foreach (var dbContextType in dbContextTypes)
                {
                    EntityFrameworkGenericRepositoryRegistrar.RegisterDbContext(dbContextType, IocManager);
                }
            }
        }
Пример #7
0
        public void Should_Resolve_Generic_Repositories()
        {
            var fakeDbContextProvider = NSubstitute.Substitute.For <IDbContextProvider <MyDbContext <int, long>, int, long> >();

            LocalIocManager.IocContainer.Register(
                Component.For <ITypeFinder>().ImplementedBy <TypeFinder>(),
                Component.For <IDbContextProvider <MyDbContext <int, long>, int, long> >().UsingFactoryMethod(() => fakeDbContextProvider)
                );

            var typeFinder = LocalIocManager.Resolve <ITypeFinder>();
            var finder     = typeFinder as TypeFinder;

            finder.AssemblyFinder = new AssembleFinder();


            var entities = finder.Find(
                x => (
                    typeof(IEntity).IsAssignableFrom(x) ||
                    x.IsInheritsOrImplements(typeof(IEntity <>))
                    )
                );

            //EntityFrameworkGenericRepositoryRegistrar.RegisterGenericRepositories<int, long>(finder,
            //    LocalIocManager);

            var dbContextType = typeof(MyDbContext <int, long>);

            EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext <int, long>(dbContextType, LocalIocManager);

            var entity1Repository = LocalIocManager.Resolve <IRepository <MyEntity1> >();

            entity1Repository.ShouldNotBe(null);

            var entity1RepositoryWithPk = LocalIocManager.Resolve <IRepository <MyEntity1, int> >();

            entity1RepositoryWithPk.ShouldNotBe(null);

            var entity2Repository = LocalIocManager.Resolve <IRepository <MyEntity2, long> >();

            entity2Repository.ShouldNotBe(null);
        }
Пример #8
0
        private void RegisterGenericRepositories()
        {
            var dbContextTypes =
                _typeFinder.Find(type =>
                                 type.IsPublic &&
                                 !type.IsAbstract &&
                                 type.IsClass &&
                                 typeof(AbpDbContext).IsAssignableFrom(type)
                                 );

            if (dbContextTypes.IsNullOrEmpty())
            {
                Logger.Warn("No class found derived from AbpDbContext.");
                return;
            }

            foreach (var dbContextType in dbContextTypes)
            {
                EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext(dbContextType, IocManager);
            }
        }
Пример #9
0
        public void Should_Resolve_Generic_Repositories()
        {
            var fakeDbContextProvider = NSubstitute.Substitute.For <IDbContextProvider <MyDbContext> >();

            LocalIocManager.IocContainer.Register(
                Component.For <IDbContextProvider <MyDbContext> >().UsingFactoryMethod(() => fakeDbContextProvider)
                );

            EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext(typeof(MyDbContext), LocalIocManager);

            var entity1Repository = LocalIocManager.Resolve <IRepository <MyEntity1, int> >();

            entity1Repository.ShouldNotBe(null);

            var entity1RepositoryWithPk = LocalIocManager.Resolve <IRepository <MyEntity1, int> >();

            entity1RepositoryWithPk.ShouldNotBe(null);

            var entity2Repository = LocalIocManager.Resolve <IRepository <MyEntity2, long> >();

            entity2Repository.ShouldNotBe(null);
        }
Пример #10
0
        private void RegisterGenericRepositories <TTenantId, TUserId>()
            where TTenantId : struct
            where TUserId : struct
        {
            var dbContextTypes =
                _typeFinder.Find(type =>
                                 type.IsPublic &&
                                 !type.IsAbstract &&
                                 type.IsClass &&
                                 type.IsInheritsOrImplements(typeof(AbpDbContext <,>)));

            if (dbContextTypes.IsNullOrEmpty())
            {
                Logger.Warn("No class found derived from AbpDbContext.");
                return;
            }

            foreach (var dbContextType in dbContextTypes)
            {
                EntityFrameworkGenericRepositoryRegistrar.RegisterForDbContext <TTenantId, TUserId>(dbContextType, IocManager);
            }
        }