注册Autofac泛型类型 [英] Register Generic Type with Autofac

查看:608
本文介绍了注册Autofac泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UnitOfWork类,它实现IUnitOfWork。我尝试注册与autofac:

  VAR建设者=新ContainerBuilder(); 
建设者
.RegisterGeneric(typeof运算(的UnitOfWork<库<>,>))
。至于(typeof运算(IUnitOfWork))
.InstancePerDependency();



实施



 公共类的UnitOfWork< T,O> :IUnitOfWork 
,其中T:库< O>
其中O:BaseEntity
{
}

公共接口IUnitOfWork:IDisposable的
{
无效的SaveChanges();
}



给出了一个错误的类型期待



但另一个项目这一个作品:

 公共类资源库< T> ; :GenericRepository< T> 
,其中T:BaseEntity
{
公共库(IDbContext上下文)
:基地(上下文){}
}

公共抽象类GenericRepository< T>
:IRepository<吨>中的IQueryable< T>其中T:BaseEntity
{
}

建设者
.RegisterGeneric(typeof运算(库<>))
。至于(typeof运算(IRepository<> ;))
.InstancePerHttpRequest();


解决方案

您的不能有部分打开类(例如,使用的UnitOfWork<库<>,> 您已经指定 T 而不是 0 的typeof ,以尝试:

  VAR建设者=新ContainerBuilder(); 
建设者
.RegisterGeneric(typeof运算(的UnitOfWork<,>))
。至于(typeof运算(IUnitOfWork))
.InstancePerDependency();



其中T:库< O> 通用的限制将采取的第一个参数应该是照顾库<>



但是,因为它需要一个通用的接口不会 RegisterGeneric 工作,所以需要创建一个 IUnitOfWork< T,O> ...



但你的模型是非常奇怪的。为什么你的的UnitOfWork 需要库<> 类型参数



而不是有它作为一个类型参数,你可以得到的库<> 的UnitOfWork< E> 构造器:

 公共类的UnitOfWork< E> :IUnitOfWork< E>其中E:BaseEntity 
{
私人只读存储库< E>库;

公众的UnitOfWork(库< E>库)
{
this.repository =库;
}

// ..其它方法

}

其中, IUnitOfWork< E>

 公开接口IUnitOfWork< E> :IDisposable接口,其中E:BaseEntity 
{
无效的SaveChanges();
}



而Autofac注册:

  VAR建设者=新ContainerBuilder(); 
建设者
.RegisterGeneric(typeof运算(库<>))。AsSelf();
建设者
.RegisterGeneric(typeof运算(的UnitOfWork<>))
。至于(typeof运算(IUnitOfWork<>))
.InstancePerDependency();
VAR容器= builder.Build();

//样品使用
变种U = container.Resolve< IUnitOfWork<&myEntity所GT;>();


I have UnitofWork class and it implement IUnitOfWork. I try to register that with autofac :

var builder = new ContainerBuilder();
builder
    .RegisterGeneric(typeof(UnitOfWork<Repository<>,>))
    .As(typeof(IUnitOfWork))
    .InstancePerDependency();

Implementation is:

public class UnitOfWork<T, O> : IUnitOfWork
    where T : Repository<O>
    where O : BaseEntity
{
}

public interface IUnitOfWork : IDisposable
{
    void SaveChanges();
}

Gives an error "Type expected"

but this one work on another project:

public class Repository<T> : GenericRepository<T> 
    where T : BaseEntity
{
    public Repository(IDbContext context)
        : base(context) { }   
}

public abstract class GenericRepository<T> 
    : IRepository<T>, IQueryable<T> where T : BaseEntity
{
}

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

解决方案

You cannot have partially opened classes (e.g. with UnitOfWork<Repository<>,> you have specified T but not O) inside a typeof, try it with:

var builder = new ContainerBuilder();
builder
    .RegisterGeneric(typeof(UnitOfWork<,>))
    .As(typeof(IUnitOfWork))
    .InstancePerDependency();

The where T : Repository<O> generic constraint will take care of that the first argument should be an Repository<>

But it won't work with RegisterGeneric because it requires a generic interface so need to create a IUnitOfWork<T,O>

But your model is very strange. Why does your UnitOfWork need a Repository<> type argument?

Instead of having it as a type argument you can get an Repository<> in your UnitOfWork<E> constructor:

public class UnitOfWork<E> : IUnitOfWork<E> where E : BaseEntity
{
    private readonly Repository<E> repository;

    public UnitOfWork(Repository<E> repository)
    {
        this.repository = repository;
    }

    //.. other methods

}

Where IUnitOfWork<E>

public interface IUnitOfWork<E> : IDisposable where E : BaseEntity
{
    void SaveChanges();
}

And the Autofac registration:

var builder = new ContainerBuilder();
builder
    .RegisterGeneric(typeof(Repository<>)).AsSelf();
builder
    .RegisterGeneric(typeof(UnitOfWork<>))
    .As(typeof(IUnitOfWork<>))
    .InstancePerDependency();
var container = builder.Build();

// sample usage
var u = container.Resolve<IUnitOfWork<MyEntity>>();

这篇关于注册Autofac泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆