Ejemplo n.º 1
0
        public static void AddRepositoryFromAssembly(this IServiceCollection services, Action <AssemblyAutoRegisterOptions> setupOptions)
        {
            var options = new AssemblyAutoRegisterOptions
            {
                Filter = (type) => { return(type.IsInterface); }
            };

            setupOptions(options);
            ScopeTemplateParser templateParser = new ScopeTemplateParser(options.ScopeTemplate);
            var             assembly           = Assembly.Load(options.AssemblyString);
            var             allTypes           = assembly.GetTypes().Where(options.Filter);
            ISmartSqlMapper sqlMapper          = null;

            foreach (var type in allTypes)
            {
                services.AddSingleton(type, sp =>
                {
                    if (sqlMapper == null && options.GetSmartSql != null)
                    {
                        sqlMapper = options.GetSmartSql(sp);
                    }
                    sqlMapper   = sqlMapper ?? sp.GetRequiredService <ISmartSqlMapper>();
                    var factory = sp.GetRequiredService <IRepositoryFactory>();
                    var scope   = string.Empty;
                    if (!String.IsNullOrEmpty(options.ScopeTemplate))
                    {
                        scope = templateParser.Parse(type.Name);
                    }
                    return(factory.CreateInstance(type, sqlMapper, scope));
                });
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 注入仓储结构 By 程序集
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="setupOptions"></param>
        /// <returns></returns>
        public static SmartSqlDIBuilder AddRepositoryFromAssembly(this SmartSqlDIBuilder builder, Action <AssemblyAutoRegisterOptions> setupOptions)
        {
            builder.AddRepositoryFactory();
            var options = new AssemblyAutoRegisterOptions
            {
                Filter = (type) => type.IsInterface
            };

            setupOptions(options);
            ScopeTemplateParser templateParser = new ScopeTemplateParser(options.ScopeTemplate);
            var allTypes = TypeScan.Scan(options);

            foreach (var type in allTypes)
            {
                builder.Services.AddSingleton(type, sp =>
                {
                    ISqlMapper sqlMapper = sp.GetRequiredService <ISqlMapper>();;
                    if (!String.IsNullOrEmpty(options.SmartSqlAlias))
                    {
                        sqlMapper = sp.EnsureSmartSql(options.SmartSqlAlias).SqlMapper;
                    }
                    var factory = sp.GetRequiredService <IRepositoryFactory>();
                    var scope   = string.Empty;
                    if (!String.IsNullOrEmpty(options.ScopeTemplate))
                    {
                        scope = templateParser.Parse(type.Name);
                    }
                    return(factory.CreateInstance(type, sqlMapper, scope));
                });
            }
            return(builder);
        }