StructureMap 具有 Profiles Autofac 中的等价物是什么?
StructureMap has Profiles what's the equivalent in Autofac?
Autofac 有没有类似于 StructureMap 的配置文件的东西
IContainer container = new Container(registry =>
{
registry.Profile("something", p =>
{
p.For<IWidget>().Use<AWidget>();
p.For<Rule>().Use<DefaultRule>();
});
});
Profiles are just named child containers that may be configured upfront through Registry configurations.
您可以在 Autofac 中创建子生命周期范围时具有相同的行为。
using (ILifetimeScope scope = container.BeginLifetimeScope(childBuilder =>
{
childBuilder.RegisterType<AWidget>().As<IWidget>();
childBuilder.RegisterType<DefaultRule>().As<Rule>();
}))
{
// do things
}
如果要命名,可以使用tag,tag是一个对象,不仅仅是一个字符串。
using (ILifetimeScope scope = container.BeginLifetimeScope("something", childBuilder =>
{
childBuilder.RegisterType<AWidget>().As<IWidget>();
childBuilder.RegisterType<DefaultRule>().As<Rule>();
}))
{
// do things
}
并且如果您想要与 GetProfile
方法等效,您可以使用此代码示例:
ContainerBuilder builder = new ContainerBuilder();
builder.Register(c => c.Resolve<ILifetimeScope>()
.BeginLifetimeScope("A", _ =>
{
_.RegisterType<FooA>().As<IFoo>();
}))
.Named<ILifetimeScope>("A");
builder.Register(c => c.Resolve<ILifetimeScope>()
.BeginLifetimeScope("B", _ =>
{
_.RegisterType<FooB>().As<IFoo>();
}))
.Named<ILifetimeScope>("B");
// build the root of all profile
IContainer container = builder.Build();
// get the profile lifetimescope
ILifetimeScope profileScope = container.ResolveNamed<ILifetimeScope>("B");
// use a working lifetimescope from your profile
using (ILifetimeScope workingScope = profileScope.BeginLifetimeScope())
{
IFoo foo = workingScope.Resolve<IFoo>();
Console.WriteLine(foo.GetType());
}
顺便说一下,请注意不要像 service locator anti pattern
那样使用此代码
Autofac 有没有类似于 StructureMap 的配置文件的东西
IContainer container = new Container(registry =>
{
registry.Profile("something", p =>
{
p.For<IWidget>().Use<AWidget>();
p.For<Rule>().Use<DefaultRule>();
});
});
Profiles are just named child containers that may be configured upfront through Registry configurations.
您可以在 Autofac 中创建子生命周期范围时具有相同的行为。
using (ILifetimeScope scope = container.BeginLifetimeScope(childBuilder =>
{
childBuilder.RegisterType<AWidget>().As<IWidget>();
childBuilder.RegisterType<DefaultRule>().As<Rule>();
}))
{
// do things
}
如果要命名,可以使用tag,tag是一个对象,不仅仅是一个字符串。
using (ILifetimeScope scope = container.BeginLifetimeScope("something", childBuilder =>
{
childBuilder.RegisterType<AWidget>().As<IWidget>();
childBuilder.RegisterType<DefaultRule>().As<Rule>();
}))
{
// do things
}
并且如果您想要与 GetProfile
方法等效,您可以使用此代码示例:
ContainerBuilder builder = new ContainerBuilder();
builder.Register(c => c.Resolve<ILifetimeScope>()
.BeginLifetimeScope("A", _ =>
{
_.RegisterType<FooA>().As<IFoo>();
}))
.Named<ILifetimeScope>("A");
builder.Register(c => c.Resolve<ILifetimeScope>()
.BeginLifetimeScope("B", _ =>
{
_.RegisterType<FooB>().As<IFoo>();
}))
.Named<ILifetimeScope>("B");
// build the root of all profile
IContainer container = builder.Build();
// get the profile lifetimescope
ILifetimeScope profileScope = container.ResolveNamed<ILifetimeScope>("B");
// use a working lifetimescope from your profile
using (ILifetimeScope workingScope = profileScope.BeginLifetimeScope())
{
IFoo foo = workingScope.Resolve<IFoo>();
Console.WriteLine(foo.GetType());
}
顺便说一下,请注意不要像 service locator anti pattern
那样使用此代码