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;
}
}