我有
builder.Register<ISomething>(c => new Something()).InstancePerLifetimeScope();
如果Something
采用的参数也是Autofac解析的接口,那么Autofac会是什么样子?
builder.Register<ISomeInitializer>(c => new SomeInitializer()).InstancePerLifetimeScope();
builder.Register<ISomething>(c => new Something(????)).InstancePerLifetimeScope();
答案 0 :(得分:1)
如果您不想将任何特殊参数传递给您的服务,那么您只需写下:
builder.Register<SomeInitializer>().As<ISomeInitializer>()
.InstancePerLifetimeScope();
builder.Register<Something>().As<ISomething>()
.InstancePerLifetimeScope();
在这种情况下,Autofac将使用其默认构造函数实例化您的SomeInitializer
,并通过传入Something
实例化您的SomeInitializer
。
如果您想保留当前代码,可以使用c
参数(IComponentContext
)来解决依赖关系:
builder.Register<ISomeInitializer>(c => new SomeInitializer())
.InstancePerLifetimeScope();
builder.Register<ISomething>(c => new Something(c.Resolve<ISomeInitializer>()))
.InstancePerLifetimeScope();