About DDD: How to manage the use of two modes of "working unit Instances" _ Practical Tips

Source: Internet
Author: User
The figure is as follows:

In a common use case scenario, the object diagram for a class diagram is as follows:

How do you ensure that all warehouse instances within the same boundary context can share the same work unit instance during the execution of a use case? Solution 1
Warehousing uses a dependency injection model to manage the lifecycle of the work unit (perrequest or others) using IOC.

code example

Copy Code code 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< warehousing a> ();
Buider. registertype< warehousing b> ();
Buider. registertype< work Unit > (). Instanceperlifetimescope ();

var container = Buider. Build ();

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

The bottom two lines of code output the same
Console.WriteLine (service. Storage A. Work Unit-GetHashCode ());
Console.WriteLine (service. Warehousing B. Work Unit-GetHashCode ());
}
}

public class Service
{
Private ReadOnly Warehousing a _ warehousing A;
Private ReadOnly Warehousing B _ warehousing B;

Public Service (Warehousing a warehousing A, warehousing b warehousing B)
{
_ Warehousing a = warehousing A;
_ Warehousing B = warehousing B;
}

Public warehousing a warehousing a
{
Get {return _ warehousing A;}
}

Public warehousing b Warehousing B
{
Get {return _ warehousing B;}
}
}

public class unit of work {}

public class storage A
{
Private ReadOnly Work Unit _ Work unit;

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

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

public class Warehousing B
{
Private ReadOnly Work Unit _ Work unit;

Public Warehousing B (Work Unit work unit)
{
_ Work unit = Work unit;
}

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

Solution 2
Warehousing uses service locator mode + lifecycle (perrequest or other) that uses a service locator or a simple factory to manage work units.
code Example
Copy Code code 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< warehousing a> ();
Buider. registertype< warehousing b> ();
Buider. registertype< work Unit > (). Instanceperlifetimescope ();

Service locator = Buider. Build ();

Dynamic service = Service Locator. resolve< service > ();

The bottom two lines of code output the same
Console.WriteLine (service. Storage A. Work Unit-GetHashCode ());
Console.WriteLine (service. Warehousing B. Work Unit-GetHashCode ());
}
}

public class Service
{
Private ReadOnly Warehousing a _ warehousing A;
Private ReadOnly Warehousing B _ warehousing B;

Public Service (Warehousing a warehousing A, warehousing b warehousing B)
{
_ Warehousing a = warehousing A;
_ Warehousing B = warehousing B;
}

Public warehousing a warehousing a
{
Get {return _ warehousing A;}
}

Public warehousing b Warehousing B
{
Get {return _ warehousing B;}
}
}

public class unit of work {}

public class storage A
{
Private ReadOnly Work Unit _ Work unit;

Public warehousing A ()
{
_ Work Unit = program. Service Locator .resolve< Work unit > ();
}

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

public class Warehousing B
{
Private ReadOnly Work Unit _ Work unit;

Public Warehousing B ()
{
_ Work Unit = program. Service Locator .resolve< Work unit > ();
}

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

This example shows that the service locator and dependency injection can be mixed together. This example I am for simplicity, the service locator and the IOC container are the same instance.

Some systems change the implementation of the service locator into simple Factory mode, which is essentially the same (the service locator is a universal factory).

code example

Copy Code code as follows:

public class work Unit factory
{
public static unit of work created ()
{
var work unit = (work unit) CallContext.GetData ("Work unit");

if (work unit = NULL)
{
Work unit = new unit of work ();
Callcontext.setdata ("Work unit", work unit);
}

return work unit;
}
}

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.