How to Use DDD to manage "work unit instances"

Source: Internet
Author: User

The figure is as follows:

In common use cases, the class chart object diagram is as follows:

How can we ensure that all the warehouse instances in the same boundary context can share the same unit of work instance during the execution of a case?Solution 1
Warehousing adopts the dependency injection mode + IOC to manage the life cycle (PerRequest or other) of the work unit ).

Sample Code

Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;

Using Autofac;

Namespace AutoFacStudy
{
Class Program
{
Static void Main (string [] args)
{
Var buider = new ContainerBuilder ();
Buider. RegisterType <service> ();
Buider. RegisterType <warehouse A> ();
Buider. RegisterType <warehouse B> ();
Buider. RegisterType <work unit> (). InstancePerLifetimeScope ();

Var container = buider. Build ();

Dynamic service = container. Resolve <service> ();

// The following two lines of code output are the same
Console. WriteLine (service. Warehouse A. unit of work. GetHashCode ());
Console. WriteLine (service. warehousing B. unit of work. GetHashCode ());
}
}

Public class service
{
Private readonly warehouse A _ warehouse;
Private readonly warehouse B _ warehouse B;

Public Service (warehouse A, warehouse B)
{
_ Warehouse A = warehouse;
_ Warehouse B = warehouse B;
}

Public warehouse
{
Get {return _ warehouse ;}
}

Public warehouse B
{
Get {return _ warehouse B ;}
}
}

Public class unit of work {}

Public class warehouse
{
Private readonly _ work unit;

Public warehouse A (unit of work unit)
{
_ Work unit = work unit;
}

Public Work Unit
{
Get {return _ work unit ;}
}
}

Public class warehouse B
{
Private readonly _ work unit;

Public warehouse B (unit of work)
{
_ Work unit = work unit;
}

Public Work Unit
{
Get {return _ work unit ;}
}
}
}

Solution 2
Warehousing adopts the service positioner mode + the service positioner or simple factory to manage the life cycle of the work unit (PerRequest or other ).
Sample Code
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;

Using Autofac;

Namespace AutoFacStudy
{
Class Program
{
Public static IContainer service locator;

Static void Main (string [] args)
{
Var buider = new ContainerBuilder ();
Buider. RegisterType <service> ();
Buider. RegisterType <warehouse A> ();
Buider. RegisterType <warehouse B> ();
Buider. RegisterType <work unit> (). InstancePerLifetimeScope ();

Service locator = buider. Build ();

Dynamic service = service locator. Resolve <service> ();

// The following two lines of code output are the same
Console. WriteLine (service. Warehouse A. unit of work. GetHashCode ());
Console. WriteLine (service. warehousing B. unit of work. GetHashCode ());
}
}

Public class service
{
Private readonly warehouse A _ warehouse;
Private readonly warehouse B _ warehouse B;

Public Service (warehouse A, warehouse B)
{
_ Warehouse A = warehouse;
_ Warehouse B = warehouse B;
}

Public warehouse
{
Get {return _ warehouse ;}
}

Public warehouse B
{
Get {return _ warehouse B ;}
}
}

Public class unit of work {}

Public class warehouse
{
Private readonly _ work unit;

Public warehouse ()
{
_ Work unit = Program. service locator. Resolve <work unit> ();
}

Public Work Unit
{
Get {return _ work unit ;}
}
}

Public class warehouse B
{
Private readonly _ work unit;

Public warehouse B ()
{
_ Work unit = Program. service locator. Resolve <work unit> ();
}

Public Work Unit
{
Get {return _ work unit ;}
}
}
}

From this example, we can see that service locators and dependency injection can be used together. For the sake of simplicity, the service locator and IOC container are the same instance.

Some systems change the implementation of service locators to a simple factory model. They are essentially the same (Service locators are 10 thousand-capable factories ).

Sample Code

Copy codeThe Code is as follows: public class work unit Factory
{
Public static work unit creation ()
{
Var unit of work = (unit of work) CallContext. GetData ("unit of work ");

If (unit of work = null)
{
Work Unit = new work unit ();
CallContext. SetData ("Work Unit", work unit );
}

Return unit of work;
}
}

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.