Instances of types that implement a work item unit usually have one owner who needs to control its life. Methods such as Commit, Open, Closeand Disposeoften is a strong signal that the type must be controlled explicitly (or placed for abstraction, if necessary).
, , , ββ : a factory.
, (, , ), . :
public class MyCommand
{
private readonly IUnitOfWorkFactory factory;
public MyCommand(IUnitOfWorkFactory factory)
{
this.factory = factory;
}
public void Execute()
{
using (var context = this.factory.CreateNew())
{
this.DoSomeNiceThings(context);
context.Commit();
}
}
}
DI , . . , , , , . , . , . , ..
?
Commit , , . , (, , ) -.
, .