2013-07-04 4 views
0

Я использую IDbcontext в моей базе данных Accesкак создать autofac фабрику для разрешения зависимостей

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; 
    } 
} 

У меня есть две базы данных «первый» и «второй»

В резолюции о depenancy кулак, я использую

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, как я могу это сделать ??

ответ

3
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(); 
Смежные вопросы