Example #1
0
        /// <summary>
        /// 初始化依赖注入
        /// </summary>
        /// <param name="config">框架配置信息</param>
        public void Initialize(OSharpConfig config)
        {
            //注册数据上下文
            Type[] dbContexTypes = config.DataConfig.ContextConfigs.Where(m => m.Enabled).Select(m => m.ContextType).ToArray();
            RegisterDbContextTypes(dbContexTypes, typeof(IUnitOfWork));

            //注册数据仓储
            Type repositoryType = GetRepositoryType();

            if (!typeof(IRepository <,>).IsGenericAssignableFrom(repositoryType))
            {
                throw new InvalidOperationException(Resources.IocInitializerBase_TypeNotIRepositoryType.FormatWith(repositoryType.FullName));
            }
            RegisterRepositoryType(repositoryType, typeof(IRepository <,>));

            //注册即时生命周期的映射
            Type[] dependencyTypes = TransientTypeFinder.FindAll();
            RegisterDependencyTypes <ITransientDependency>(dependencyTypes);

            //注册局部生命周期的映射
            dependencyTypes = LifetimeScopeTypeFinder.FindAll();
            RegisterDependencyTypes <ILifetimeScopeDependency>(dependencyTypes);

            //注册单例生命周期的映射
            dependencyTypes = SingletonTypeFinder.FindAll();
            RegisterDependencyTypes <ISingletonDependency>(dependencyTypes);

            //注册自定义映射
            RegisterCustomTypes();

            //设置各个框架的DependencyResolver
            Assembly[] assemblies = AssemblyFinder.FindAll();
            SetResolver(assemblies);
        }
Example #2
0
        /// <summary>
        /// 初始化依赖注入
        /// </summary>
        /// <param name="config">框架配置信息</param>
        public void Initialize()
        {
            Type[] dbContexTypes = { typeof(DefaultDbContext) }; //config.DataConfig.ContextConfigs.Where(m => m.Enabled).Select(m => m.ContextType).ToArray();
            RegisterDbContextTypes(dbContexTypes, typeof(IUnitOfWork));

            RegisterRepositoryType(typeof(Repository <,>), typeof(IRepository <,>));

            Type[] dependencyTypes = TransientTypeFinder.FindAll();
            RegisterDependencyTypes <ITransientDependency>(dependencyTypes);

            dependencyTypes = LifetimeScopeTypeFinder.FindAll();
            RegisterDependencyTypes <ILifetimeScopeDependency>(dependencyTypes);

            dependencyTypes = SingletonTypeFinder.FindAll();
            RegisterDependencyTypes <ISingletonDependency>(dependencyTypes);

            RegisterCustomTypes();

            Assembly[] assemblies = AssemblyFinder.FindAll();
            SetResolver(assemblies);
        }