Autofac-构建前解决 [英] Autofac - resolve before build
问题描述
使用Unity依赖项可以在构建容器之前解决. Autofac也可能吗?
下面的代码演示了我的情况-我需要解析ICacheRepository
才能更新"单例CacheHelper
.
With Unity dependency can be resolved before the container is build. Is that possible with Autofac as well?
Below code demonstrates my scenario - I'd need to resolve the ICacheRepository
in order to "new up" the singleton CacheHelper
.
在Unity中,只需使用container.Resolve<ICacheRepository>()
代替???即可完成.在Autofac中怎么办?
In Unity that would be simply done with container.Resolve<ICacheRepository>()
in the place of ???. What about in Autofac?
var builder = new ContainerBuilder();
builder.RegisterType<CacheRepository>().As<ICacheRepository>();
var cacheHelper = new CacheHelper(???);
builder.RegisterInstance(cacheHelper).As<CacheHelper>();
CacheHelper
类在CacheRepository
上具有构造函数依赖项.
Where CacheHelper
class has a constructor dependency on CacheRepository
.
public class CacheHelper
{
private readonly ICacheRepository _repository;
public CacheHelper(ICacheRepository repository)
{
_repository = repository;
}
}
推荐答案
在构建过程中,您不必解析组件. Autofac 能够解决对象图依赖关系.在您的情况下,CacheHelper
取决于ICacheRepository
,因此您只需要注册CacheHelper
和ICacheRepository
You should not have to resolve component during build process. Autofac is able to solve object graph dependency. In your case CacheHelper
depends on ICacheRepository
so you just have to register CacheHelper
and ICacheRepository
var builder = new ContainerBuilder();
builder.RegisterType<CacheRepository>().As<ICacheRepository>();
builder.RegisterType<CacheHelper>().As<CacheHelper>();
当 Autofac 解析CacheHelper
时,它将创建依赖关系图,并使用实例ICacheRepository
创建CacheHelper
的实例.如果您需要一个 Singleton ,则可以告诉Autofac仅创建一个实例.
When Autofac will resolve CacheHelper
it will create the dependency graph and create the instance of CacheHelper
with an instance ofsi ICacheRepository
. If you need to have a Singleton you can tell Autofac to create only a single instance.
var builder = new ContainerBuilder();
builder.RegisterType<CacheRepository>().As<ICacheRepository>();
builder.RegisterType<CacheHelper>().As<CacheHelper>().SingleInstance();
另一种解决方案是寄存器lambda表达式,这些注册在需要时调用,因此您可以在构建过程中 resolve :
Another solution would be register lambda expression, these registrations are called when you need it, so you can resolve things during build process :
var builder = new ContainerBuilder();
builder.RegisterType<CacheRepository>().As<ICacheRepository>();
builder.Register(c => new CacheHelper(c.Resolve<ICacheRepository>()))
.As<CacheHelper>()
.SingleInstance(); // It will result of having one CacheHelper whereas
// ICacheRepository is declared as .InstancePerDependency
请谨慎使用此解决方案,因为声明的ICacheRepository
没有范围,默认情况下将使用InstancePerDependency
范围.由于CacheHelper
是SingleInstance
,因此仅会使用ICacheRepository
的单个实例,这可能会导致错误.有关更多信息,请参见圈养依赖性.
Be careful with this solution because ICacheRepository
is declared without scope the InstancePerDependency
scope will be used by default. Because CacheHelper
is SingleInstance
only a single instance of ICacheRepository
will be used which may result to bugs. See Captive Dependency for more information.
就您而言,看来您不需要这种注册.
In your case, it doesn't seem like you need this kind of registration.
这篇关于Autofac-构建前解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!