【问题标题】:how to create autofac factory for dependency resolution如何为依赖解析创建autofac工厂
【发布时间】:2013-07-04 13:50:58
【问题描述】:

我正在使用 IDbcontext 访问我的数据库

   public sealed class DbContext : IDbContext
{
    private bool disposed;
    private SqlConnection connection;

    public DbContext(string connectionString)
    {
        connection = new SqlConnection(connectionString);
    }

    public IDbConnection Connection
    {
        get
        {
            if (disposed) throw new ObjectDisposedException(GetType().Name);

            return connection;
        }
    }

    public IDbTransaction CreateOpenedTransaction()
    {
        if (connection.State != ConnectionState.Open)
            Connection.Open();
        return Connection.BeginTransaction();
    }

    public IEnumerable<T> ExecuteProcedure<T>(string procedure, dynamic param = null, IDbTransaction transaction = null)
    {
        if (connection.State == ConnectionState.Closed)
        {
            connection.Open();
        }

        return Dapper.SqlMapper.Query<T>(connection, procedure, param, transaction,
            commandType: CommandType.StoredProcedure);
    }

    public int ExecuteProcedure(string procedure, dynamic param = null, IDbTransaction transaction = null)
    {
        if (connection.State == ConnectionState.Closed)
        {
            connection.Open();
        }

        return Dapper.SqlMapper.Execute(connection, procedure, param, transaction,
            commandType: CommandType.StoredProcedure);
    }

    public void Dispose()
    {
        Debug.WriteLine("** Disposing DbContext");

        if (disposed) return;

        if (connection != null)
        {
            connection.Dispose();
            connection = null;
        }

        disposed = true;
    }
}

我有 2 个数据库“first”和“second”

在拳头的决议o依赖,我使用

 builder.Register<IDbContext>(c =>
          new DbContext(ConfigurationManager.ConnectionStrings["first"].ConnectionString))
          .InstancePerDependency();

所以我需要补充:

builder.Register<IDbContext>(c =>
              new DbContext(ConfigurationManager.ConnectionStrings["second"].ConnectionString))

          .InstancePerDependency();

我需要为 Dbcontext 创建一个工厂并将 NamedResolve 用于 autofac,我该怎么做??

【问题讨论】:

    标签: c# design-patterns ioc-container autofac factory


    【解决方案1】:
    public class DbContextFactory
    {
        private ILifetimeScope m_RootLifetimeScope;
    
        public DbContextFactory(ILifetimeScope rootLifetimeScope)
        {
            m_RootLifetimeScope = rootLifetimeScope;
        }
    
        public IDbContext CreateDbContext()
        {
            if (logic for selection first dbcontext)
            {
                return m_RootLifetimeScope.ResolveNamed<IDbContext>("first");
            }
            else if (logic for selection second dbcontext)
            {
                return m_RootLifetimeScope.ResolveNamed<IDbContext>("second");
            }
            else 
            {
                throw new NotSupportedException();
            }
        }
    }
    
    //registration
    
    builder.RegisterType<DbContextFactory>().SingleInstance();
    
    //using
    
    var factory = yourContainer.Resolve<DbContextFactory>();
    var context = factory.CreateDbContext();
    

    【讨论】:

      猜你喜欢
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      相关资源
      最近更新 更多