Пример #1
0
        public static IDbConnection CreateConnection(DataBaseType repositoryType, string connectionStringName)
        {
            if (string.IsNullOrWhiteSpace(connectionStringName))
            {
                throw new Exception($"数据库连接名称不能为空,参数connectionStringName");
            }
            if (!repositoryType.IsInDefined())
            {
                throw new Exception("请指定数据库类型!");
            }

            string connectionString = GetConnectionString(connectionStringName);

            IDbConnection connection = null;

            if (repositoryType == DataBaseType.SqlServer)
            {
                connection = new SqlConnection(connectionString);
            }
            else if (repositoryType == DataBaseType.PostgreSQL)
            {
                connection = new NpgsqlConnection(connectionString);
            }

            connection.Open();

            return(connection);
        }
Пример #2
0
        public static void RegisterRepositoryAndSvc(this ContainerBuilder builder, DataBaseType dataBaseType, Assembly repoAssembly, Assembly unitSvcAssembly, Assembly integrateSvcAssembly)
        {
            if (!dataBaseType.IsInDefined())
            {
                throw new Exception($"数据库类型dataBaseType:{dataBaseType}不合法");
            }
            if (repoAssembly == null)
            {
                throw new Exception($"repoAssembly程序集不能为空");
            }
            if (unitSvcAssembly == null)
            {
                throw new Exception($"unitSvcAssembly程序集不能为空");
            }
            if (integrateSvcAssembly == null)
            {
                throw new Exception($"integrateSvcAssembly程序集不能为空");
            }

            //注册范型仓储
            if (dataBaseType == DataBaseType.SqlServer)
            {
                builder.RegisterGeneric(typeof(SqlServerRepository <>)).As(typeof(IRepository <>)).PropertiesAutowired().InstancePerLifetimeScope();
            }
            else if (dataBaseType == DataBaseType.PostgreSQL)
            {
                builder.RegisterGeneric(typeof(PgSqlRepository <>)).As(typeof(IRepository <>)).PropertiesAutowired().InstancePerLifetimeScope();
            }

            var allTypes = AppDomain.CurrentDomain.GetAllTypes();

            foreach (Type type in allTypes)
            {
                var  attr = type.GetCustomAttribute <AutoRegisterAttribute>();
                bool isPromissoryAutoRegType = attr != null;//是约定自动注册

                //约定是一个类,且是约定自动注册
                if (type.IsClass && isPromissoryAutoRegType)
                {
                    Type interfaceType = !string.IsNullOrWhiteSpace(attr.InterfaceName) ?
                                         type.GetInterface(attr.InterfaceName) :
                                         type.GetInterface($"I{type.Name}");

                    if (interfaceType != null)
                    {
                        if (!string.IsNullOrWhiteSpace(attr.RegKey))
                        {
                            builder.RegisterType(type).As(interfaceType).Named(attr.RegKey, interfaceType).PropertiesAutowired().SingleInstance();
                        }
                        else
                        {
                            builder.RegisterType(type).As(interfaceType).PropertiesAutowired().SingleInstance();
                        }
                    }
                    else
                    {
                        if (!string.IsNullOrWhiteSpace(attr.RegKey))
                        {
                            builder.RegisterType(type).As(type).Named(attr.RegKey, type).PropertiesAutowired().SingleInstance();
                        }
                        else
                        {
                            builder.RegisterType(type).PropertiesAutowired().SingleInstance();
                        }
                    }
                }

                bool isSubClass = typeof(System.Web.Http.ApiController).IsAssignableFrom(type);
                isSubClass |= typeof(System.Web.Mvc.ControllerBase).IsAssignableFrom(type);

                if (isSubClass && !type.Assembly.FullName.Contains("System.Web"))
                {
                    builder.RegisterType(type).PropertiesAutowired();
                }
            }
        }