Autofac 使用构造函数

Autofac using Constructor

我首先使用带有 entityFramework 代码的工作单元模式。现在我想使用 Autofac 来注册 UnitOfWork、Repositories 和 My dbContext。

这是我的 UnitOfWork 代码:

public class UnitOfWork : IUnitOfWork
{
    private readonly DbContext _context;

    public UnitOfWork(DbContext context)
    {
        _context = context;

        Contact = new ContractRepository(context);
    }

    public void Dispose()
    {
        _context.Dispose();
        GC.SuppressFinalize(_context);
    }

    public IContactRepository Contact { get; private set; }

    public int Complete()
    {
        return _context.SaveChanges();
    }
}

这是我的存储库:

public class Repository<Entity> : IRepository<Entity> where Entity : class
{
    protected readonly DbContext _noteBookContext;
    public Repository(DbContext noteBookContext)
    {
        _noteBookContext = noteBookContext;
    }
    public void Add(Entity entity)
    {
        _noteBookContext.Set<Entity>().Add(entity);
    }
}

这是我的存储库之一:

public  class ContractRepository: Repository<Contact>,IContactRepository 
{
    public ContractRepository(DbContext noteBookContext) : base(noteBookContext)
    {

    }

    public DbContext NotebookContext
    {
        get
        {
            return _noteBookContext;
        }
    }
}

这是我的数据库上下文 class:

public class NoteBookContext:DbContext
{
    public NoteBookContext(string connectionstring):base(connectionstring)
    {

    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ContactConfig());
        modelBuilder.Configurations.Add(new PhoneConfig());
        modelBuilder.Configurations.Add(new PhoneTypeConfig());
        modelBuilder.Configurations.Add(new GroupConfig());
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<Contact> Contacts { get; set; }
    public DbSet<Phone> Phones { get; set; }

    public DbSet<Group> Groups { get; set; }

    public DbSet<PhoneType> PhoneTypes { get; set; }
}

现在我想用构造函数注册 UnitOfWork(像这样的构造函数:)

var uow = new UnitOfWork(new NotebookdbContext("connectionstring"));

请注意,NoteBookContext 是我的 entity framework 模型。

我写了注册但是我得到了错误:

var builder = new ContainerBuilder();
builder.RegisterType<NoteBookContext>()
       .As<DbContext>();
builder.RegisterType<UnitOfWork>()
       .UsingConstructor(typeof(DbContext))
       .As<IUnitOfWork>();

builder.RegisterGeneric(typeof(Repository<>))
       .As(typeof(IRepository<>))
       .InstancePerLifetimeScope();

Container container = builder.Build();

这是我的错误:

An unhandled exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll Additional information: None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'DataLayer.NoteBookContext' can be invoked with the available services and parameters:

Cannot resolve parameter 'System.String connectionstring' of constructor 'Void .ctor(System.String)'.

编辑 2:

在 Cyril Durand 的回答帮助下,我写了以下注册配置:

 var builder = new ContainerBuilder();
        builder.RegisterType<ConnectionStringProvider>().As<IConnectionStringProvider>();
        builder.RegisterType<NoteBookContext>().As<DbContext>().WithParameter((pi, c) => pi.Name == "connectionstring",
                                                                              (pi, c) => c.Resolve<IConnectionStringProvider>().ConnectionString);
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().WithParameter(ResolvedParameter.ForNamed<DbContext>("connectionstring"));
        builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();

在我的代码中:

using (var scope = DependencyInjection.Container.BeginLifetimeScope())
        {
            var ConnectionString = scope.Resolve<IConnectionStringProvider>();
            ConnectionString.ConnectionString = "Context";

            var uw = scope.Resolve<IUnitOfWork>();
            var a =uw.Contact.GetAll();

        }

但我又遇到了错误:

An unhandled exception of type 'Autofac.Core.DependencyResolutionException' occurred in Autofac.dll

Additional information: An exception was thrown while invoking the constructor 'Void .ctor(System.String)' on type 'NoteBookContext'.

大家能帮帮我吗?

不看错误很难判断。但是你不需要使用 UsingConstructor.

//Make DbContext per request, if your app is web app (which has http request).
builder.RegisterType<NoteBookContext>()
       .As<DbContext>().WithParameter("connectionstring","ConnectionStringValue").InstancePerLifetimeScope();
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();

builder.RegisterGeneric(typeof(Repository<>))
       .As(typeof(IRepository<>))
       .InstancePerLifetimeScope();

Container = builder.Build();

错误信息:

An unhandled exception of type Autofac.Core.DependencyResolutionException occurred in Autofac.dll Additional information: None of the constructors found with Autofac.Core.Activators.Reflection.DefaultConstructorFinder on type DataLayer.NoteBookContext can be invoked with the available services and parameters:

Cannot resolve parameter System.String connectionstring of constructor Void .ctor(System.String).

告诉您 Autofac 无法创建 NoteBookContext 因为它无法解析类型为 String 的名为 connectionstring 的参数.

您的 NoteBookContext 实现需要一个连接字符串,Autofac 不告诉它就无法知道。注册 NoteBookContext 时,您必须指定连接字符串:

builder.RegisterType<NoteBookContext>()
       .As<DbContext>()
       .WithParameter("connectionstring", "XXX");

另一种具有动态分辨率和 IConnectionStringProvider 接口的解决方案:

public interface IConnectionStringProvider
{
    public String ConnectionString { get; }
}

并注册:

builder.RegisterType<ConnectionStringProvider>()
       .As<IConnectionStringProvider>()
       .InstancePerLifetimeScope(); 
builder.RegisterType<NoteBookContext>()
       .As<DbContext>()
       .WithParameter((pi, c) => pi.Name == "connectionstring", 
                      (pi, c) => c.Resolve<IConnectionStringProvider>().ConnectionString)
       .InstancePerLifetimeScope();

您想将连接字符串传递给您的上下文有什么原因吗?为您的工作单元创建一个接口并执行如下操作:

public class NoteBookContext:DbContext
{
    //Change connectionstring below with the name of your connection string in web.config
    public NoteBookContext():base("name=connectionstring")
    {

    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new ContactConfig());
        modelBuilder.Configurations.Add(new PhoneConfig());
        modelBuilder.Configurations.Add(new PhoneTypeConfig());
        modelBuilder.Configurations.Add(new GroupConfig());
        base.OnModelCreating(modelBuilder);
    }
    public DbSet<Contact> Contacts { get; set; }
    public DbSet<Phone> Phones { get; set; }

    public DbSet<Group> Groups { get; set; }

    public DbSet<PhoneType> PhoneTypes { get; set; }
}

然后像这样注册:

var builder = new ContainerBuilder();
builder.RegisterType<NoteBookContext>()
       .As<DbContext>()
       .InstancePerLifetimeScope();

builder.RegisterType<UnitOfWork>()
       .As<IUnitOfWork>()
       .InstancePerLifetimeScope();

builder.RegisterGeneric(typeof(Repository<>))
       .As(typeof(IRepository<>))
       .InstancePerLifetimeScope();

Container container = builder.Build();