Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何为依赖项解析创建autofac工厂_C#_Design Patterns_Ioc Container_Autofac_Factory - Fatal编程技术网

C# 如何为依赖项解析创建autofac工厂

C# 如何为依赖项解析创建autofac工厂,c#,design-patterns,ioc-container,autofac,factory,C#,Design Patterns,Ioc Container,Autofac,Factory,我正在使用IDbcontext访问我的数据库 public sealed class DbContext : IDbContext { private bool disposed; private SqlConnection connection; public DbContext(string connectionString) { connection = new SqlConnection(connectionString);

我正在使用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;
    }
}
公共密封类DbContext:IDbContext
{
私人住宅;
专用SqlConnection;
公共数据库上下文(字符串连接字符串)
{
连接=新的SqlConnection(connectionString);
}
公共连接
{
得到
{
如果(disposed)抛出新的ObjectDisposedException(GetType().Name);
回路连接;
}
}
公共IDbTransaction CreateOpenedTransaction()
{
if(connection.State!=ConnectionState.Open)
Connection.Open();
返回连接。BeginTransaction();
}
公共IEnumerable ExecuteProcess(字符串过程,动态参数=null,IDbTransaction=null)
{
if(connection.State==ConnectionState.Closed)
{
connection.Open();
}
返回Dapper.SqlMapper.Query(连接、过程、参数、事务、,
commandType:commandType.StoredProcess);
}
public int ExecuteProcedure(字符串过程,动态参数=null,IDbTransaction=null)
{
if(connection.State==ConnectionState.Closed)
{
connection.Open();
}
返回Dapper.SqlMapper.Execute(连接、过程、参数、事务、,
commandType:commandType.StoredProcess);
}
公共空间处置()
{
Debug.WriteLine(“**DbContext”);
如果(处置)返回;
if(连接!=null)
{
connection.Dispose();
连接=空;
}
这是真的;
}
}
我有两个数据库“第一个”和“第二个”

在解决fist的依赖性时,我使用

 builder.Register<IDbContext>(c =>
          new DbContext(ConfigurationManager.ConnectionStrings["first"].ConnectionString))
          .InstancePerDependency();
builder.Register(c=>
新的DbContext(ConfigurationManager.ConnectionString[“first”].ConnectionString))
.InstancePerDependence();
因此,我需要补充:

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

          .InstancePerDependency();
builder.Register(c=>
新的DbContext(ConfigurationManager.ConnectionString[“第二个”].ConnectionString))
.InstancePerDependence();
我需要为Dbcontext创建一个工厂,并将NamedResolve用于autofac,如何才能做到这一点???

公共类DbContextFactory
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();
{ 专用ILifetimeScope m_RootLifetimeScope; 公共DbContextFactory(ILifetimeScope rootLifetimeScope) { m_RootLifetimeScope=RootLifetimeScope; } 公共IDbContext CreateDbContext() { if(选择第一个dbcontext的逻辑) { 返回m_RootLifetimeScope.ResolveNamed(“第一”); } else if(选择第二个dbcontext的逻辑) { 返回m_RootLifetimeScope.ResolveNamed(“第二个”); } 其他的 { 抛出新的NotSupportedException(); } } } //登记 builder.RegisterType().SingleInstance(); //使用 var factory=yourContainer.Resolve(); var context=factory.CreateDbContext();
公共类DbContextFactory
{
专用ILifetimeScope m_RootLifetimeScope;
公共DbContextFactory(ILifetimeScope rootLifetimeScope)
{
m_RootLifetimeScope=RootLifetimeScope;
}
公共IDbContext CreateDbContext()
{
if(选择第一个dbcontext的逻辑)
{
返回m_RootLifetimeScope.ResolveNamed(“第一”);
}
else if(选择第二个dbcontext的逻辑)
{
返回m_RootLifetimeScope.ResolveNamed(“第二个”);
}
其他的
{
抛出新的NotSupportedException();
}
}
}
//登记
builder.RegisterType().SingleInstance();
//使用
var factory=yourContainer.Resolve();
var context=factory.CreateDbContext();