About the relationship between repository and unitofwork patterns

Source: Internet
Author: User
Tags commit rollback in domain

The following three points explain briefly:

1,repository mode
2,unitofwork mode
3, the two common relationship one, repository mode

Description and Function:

According to the original author's introduction, it is a link between the data mapping layer and the domain , which acts as a collection of domain objects in memory. The client object combines some entities of the query and submits them to repository. objects can be removed or added from repository, as if these objects were to be manipulated on a collection object, while the code of the mapping layer would fetch corresponding data from the database.

Conceptually, Repository is a collection of data from a data store that is encapsulated into objects and provides operations on those collections ....

In domain-driven design, we have the concept of a set (aggregate), and usually we are defining a repository for each set of domains that corresponds. It is said that not every entity will have a corresponding repository. (assuming this is a warehouse, the introduction originates from an online excerpt.) ) Two, unitofwork mode

Description and Function:

As Jane says, the primary role is to ensure that data is committed during data persistence, to ensure the integrity of the data, and that the object uses the same context object. Provides rollback if there is an exception. three, the relationship between the two

That

The work unit serves the warehousing and initializes the context in the unit of work, providing the context object for the storage unit, thereby ensuring the same context object.

So at this point, the question is, how to get the context in the warehouse. (The ORM used is EF, which is injected as AUTOFAC or MEF, as an example) so the implementation becomes simple at this point.

Look at the script:

In UOW

The context object is initialized in UOW, and the Property object of the context is defined (of course, this place can not be initialized, there can be an IOC in the injection time into the parameter injection, see IOC, the common way is several, the implementation is very similar. If you feel uncomfortable here, change to Dbset object can also, you please feel free.

In Repository

in the warehousing constructor, the default initialization of the warehousing object and get the context object , as if finished, no ~ ~ ~

The following is my present, I can say to play it. Is the comparative love, but there are drawbacks, with a long time to find that the combination is not very suitable, although many people respected. Of course, the benefits are obvious, is to provide a unified development model and specifications, is the development of developers more regular, better maintenance.

UoW

Public Olympicoverdbcontext Context 
{
    get 
    {
        ... Initialize Context
    }
} public
bool iscommitted {get;set;}

public int Commit () 
{
    if (iscommitted) return 0;
    Try 
    {
        int result = Context.savechanges ();
        Iscommitted = true;
        return result;
    }
    catch (dbentityvalidationexception Advex) 
    {
        ... Catch exception
        return-1;
    }
}
public void RollBack () 
{
    iscommitted = false;
}
public void Dispose () 
{
    if (! iscommitted) 
    {
        Commit ();
    }
    Context.dispose ();
}

Repository Warehousing:

Public olympicoverrepository (iunitofwork unitofwork) 
{
    unitofwork = unitofwork;
    Context = Unitofwork.getdbcontext;
    Context = ((unitofwork.unitofwork) unitofwork). Context;
}

#region Properties///<summary>//     Get an instance of the warehousing context/
//</summary> public
iunitofwork Unitofwork 
{
    get;
}

<summary>///data context///
</summary>
protected DbContext context 
{
    get;
    private set;
}

<summary>////
get  query datasets for the current entity///
</summary> public
iqueryable<tentity> Entities 
{
    get 
    {
        return context.set<tentity> ();
    }
}
#endregion

.... Add to the deletion, a little ....

If there is an improper understanding, but also you can point out, a lot of enlighten, thank you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.