@muyanfeixiang
2016-01-18T14:53:41.000000Z
字数 8272
阅读 4269
Autofac
手动创建作用域,并Disposal。
Lifetime scopes are disposable and they track component disposal, so make sure you always call “Dispose()”or wrap them in “using” statements.
using(var scope = container.BeginLifetimeScope())
{
// Resolve services from a scope that is a child
// of the root container.
var service = scope.Resolve();
// You can also create nested scopes...
using(var unitOfWorkScope = scope.BeginLifetimeScope())
{
var anotherService = unitOfWorkScope.Resolve();
}
}
有时候你可能需要在Unit of work内共享一些服务,但是由不希望采用全局的共享便利,如单例模式。例如web应用的per-request生命周期,在这中情况下你可以使用InstancePerMatchingLifetimeScope来标识你的生命周期和服务。
举例如下,有个发邮件的组件,事务逻辑中需要发送多次邮件,所以可以在每个逻辑事务片中共享邮件服务。然后不希望邮件组件成为全局单例,可以如下设置。
// Register your transaction-level shared component
// as InstancePerMatchingLifetimeScope and give it
// a "known tag" that you'll use when starting new
// transactions.
var builder = new ContainerBuilder();
builder.RegisterType<EmailSender>()
.As<IEmailSender>()
.InstancePerMatchingLifetimeScope("transaction");
// Both the order processor and the receipt manager
// need to send email notifications.
builder.RegisterType<OrderProcessor>()
.As<IOrderProcessor>();
builder.RegisterType<ReceiptManager>()
.As<IReceiptManager>();
var container = builder.Build();
// Create transaction scopes with a tag.
using(var transactionScope = container.BeginLifetimeScope("transaction"))
{
using(var orderScope = transactionScope.BeginLifetimeScope())
{
// This would resolve an IEmailSender to use, but the
// IEmailSender would "live" in the parent transaction
// scope and be shared across any children of the
// transaction scope because of that tag.
var op = orderScope.Resolve<IOrderProcessor>();
op.ProcessOrder();
}
using(var receiptScope = transactionScope.BeginLifetimeScope())
{
// This would also resolve an IEmailSender to use, but it
// would find the existing IEmailSender in the parent
// scope and use that. It'd be the same instance used
// by the order processor.
var rm = receiptScope.Resolve<IReceiptManager>();
rm.SendReceipt();
}
}
Autofac 允许你在创建生命周期时添加“on the fly”。This can help you when you need to do a sort of “spot weld” limited registration override or if you generally just need some additional stuff in a scope that you don’t want to register globally. You do this by passing a lambda to BeginLifetimeScope() that takes a ContainerBuilder and adds registrations.(在创建生命周期时,注册额外的服务,而不需要全局注册)
using(var scope = container.BeginLifetimeScope(
builder =>
{
builder.RegisterType<Override>().As<IService>();
builder.RegisterModule<MyModule>();
}))
{
// The additional registrations will be available
// only in this lifetime scope.
}
实例范围决定了一个实例在request之间怎么共享的。
当请求一个服务时,autofac可以返回单例(single instance scope),新的实例(per dependency scope),或者某种上下文的单例,如线程或者http请求(per lifetime scope)。
This applies to instances returned from an explicit Resolve() call as well as instances created internally by the container to satisfy the dependencies of another component.
在别的容器中也叫transient’ or ‘factory’,在每次请求服务时都会返回一个独一无二的实例。
如果没有指定的生命周期,这是默认行为。
var builder = new ContainerBuilder();
// This...
builder.RegisterType<Worker>();
// ...is the same as this:
builder.RegisterType<Worker>().InstancePerDependency();
每一Resolve依赖时,都返回一个新的组件。
using(var scope = container.BeginLifetimeScope())
{
for(var i = 0; i < 100; i++)
{
// Every one of the 100 Worker instances
// resolved in this loop will be brand new.
var w = scope.Resolve<Worker>();
w.DoWork();
}
}
在所有请求和嵌套作用域内都会返回同一个实例。
var builder = new ContainerBuilder();
builder.RegisterType<Worker>().SingleInstance();
// It's generally not good to resolve things from the
// container directly, but for singleton demo purposes
// we do...
var root = container.Resolve<Worker>();
// We can resolve the worker from any level of nested
// lifetime scope, any number of times.
using(var scope1 = container.BeginLifetimeScope())
{
for(var i = 0; i < 100; i++)
{
var w1 = scope1.Resolve<Worker>();
using(var scope2 = scope1.BeginLifetimeScope())
{
var w2 = scope2.Resolve<Worker>();
// root, w1, and w2 are always literally the
// same object instance. It doesn't matter
// which lifetime scope it's resolved from
// or how many times.
}
}
}
这个作用域可以应用于嵌套作用域。per-lifetime scope组件在嵌套的作用域内最多有一个实例。
This is useful for objects specific to a single unit of work that may need to nest additional logical units of work. Each nested lifetime scope will get a new instance of the registered dependency.
var builder = new ContainerBuilder();
builder.RegisterType<Worker>().InstancePerLifetimeScope();
当你解析per lifetime scope实例组件时,在每个嵌套的作用域(e.g. per unit of work),只有一个实例。
using(var scope1 = container.BeginLifetimeScope())
{
for(var i = 0; i < 100; i++)
{
// Every time you resolve this from within this
// scope you'll get the same instance.
var w1 = scope1.Resolve<Worker>();
}
}
using(var scope2 = container.BeginLifetimeScope())
{
for(var i = 0; i < 100; i++)
{
// Every time you resolve this from within this
// scope you'll get the same instance, but this
// instance is DIFFERENT than the one that was
// used in the above scope. New scope = new instance.
var w2 = scope2.Resolve<Worker>();
}
}
这个与Instance Per Lifetime Scope类似,但是可以用更精确的实例共享控制。
当创建嵌套生命周期时,可以给该周期打上“标签”或者“名字”。A component with per-matching-lifetime scope will have at most a single instance per nested lifetime scope that matches a given name。这样可以创建scoped singleton,其中的嵌套周期就可以共享组件,而不需要创建全局的实例。
对单个unit of work很有用,如http请求,作为嵌套生命周期来创建。 If a nested lifetime is created per HTTP request, then any component with per-lifetime scope will have an instance per HTTP request. (More on per-request lifetime scope below.)
在大部分应用中,只需要一个层级的容器嵌套就可以代表unit of work。如果需要多个嵌套层级(如global->request->transaction),组件可以通过tag来创建在特定层级共享。
var builder = new ContainerBuilder();
builder.RegisterType<Worker>().InstancePerMatchingLifetimeScope("myrequest");
当开始一个嵌套生命周期时,tag就和该生命周期关联。You will get an exception if you try to resolve a per-matching-lifetime-scope component when there’s no correctly named lifetime scope.(如果解析不存的tag标识的生命周期,会出异常)。
// Create the lifetime scope using the tag.
using(var scope1 = container.BeginLifetimeScope("myrequest"))
{
for(var i = 0; i < 100; i++)
{
var w1 = scope1.Resolve<Worker>();
using(var scope2 = scope1.BeginLifetimeScope())
{
var w2 = scope2.Resolve<Worker>();
// w1 and w2 are always the same object
// instance because the component is per-matching-lifetime-scope,
// so it's effectively a singleton within the
// named scope.
}
}
}
// Create another lifetime scope using the tag.
using(var scope3 = container.BeginLifetimeScope("myrequest"))
{
for(var i = 0; i < 100; i++)
{
// w3 will be DIFFERENT than the worker resolved in the
// earlier tagged lifetime scope.
var w3 = scope3.Resolve<Worker>();
using(var scope4 = scope1.BeginLifetimeScope())
{
var w4 = scope4.Resolve<Worker>();
// w3 and w4 are always the same object because
// they're in the same tagged scope, but they are
// NOT the same as the earlier workers (w1, w2).
}
}
}
// You can't resolve a per-matching-lifetime-scope component
// if there's no matching scope.
using(var noTagScope = container.BeginLifetimeScope())
{
// This throws an exception because this scope doesn't
// have the expected tag and neither does any parent scope!
var fail = noTagScope.Resolve<Worker>();
}
一些应用类型自然的拥有“request”类型语义,如ASP.NET MVC。在这些应用类型中,拥有某种形式的“singleton per request”是很有帮助的。Instance per request builds on top of instance per matching lifetime scope by providing a well-known lifetime scope tag, a registration convenience method, and integration for common application types(每次请求获取一个实例,是通过提供一个well-known的生命周期tag,a registration convenience method, and integration for common application types来构建在per matching lifetime scope之上的). 本质上就是per matching lifetime scope。
这意味这如果没有当前请求,而你却去解析一个基于instance-per-request注册的组件时,就会抛出异常。
There is a detailed FAQ outlining how to work with per-request lifetimes.
var builder = new ContainerBuilder();
builder.RegisterType<Worker>().InstancePerRequest();
Owned隐式的关系类型,创建了新的嵌套生命周期。可以通过 instance-per-owned注册,可以将依赖范围限制在宿主实例中。
var builder = new ContainerBuilder();
builder.RegisterType<MessageHandler>();
builder.RegisterType<ServiceForHandler>().InstancePerOwned<MessageHandler>();
在这个例子中,ServiceForHandler服务的的生命周期限制在宿主MessageHandler实例的生命周期中(In this example the ServiceForHandler service will be scoped to the lifetime of the owned MessageHandler instance.)。
using(var scope = container.BeginLifetimeScope())
{
// The message handler itself as well as the
// resolved dependent ServiceForHandler service
// is in a tiny child lifetime scope under
// "scope." Note that resolving an Owned<T>
// means YOU are responsible for disposal.
var h1 = scope.Resolve<Owned<MessageHandler>>();
h1.Dispose();//要释放资源
}