Unit of work

I am looking for some tips on the unit of work template.

Is fixing on a unit of work called several times or just once, and then leaving the object to collect garbage?

Is it a good idea to introduce a unit of work, or do I need to pass it in a method call when requesting objects to do some work?

+4
source share
2 answers

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 , , . , (, , ) -.

, .

+4

" ", , (, , , ).

"" ( ), , - )

, commit . ORM/s, framework/nhibernate, , , .

0

All Articles