關于DDD:管理工作單元實例的兩種模式的使用方法_.Net教程
推薦:淺談對Jquery+JSON+WebService的使用小結本篇文章介紹了對Jquery+JSON+WebService的使用小結。需要的朋友參考下
圖如下:在常見的用例場景下,類圖的對象圖如下:
問題在一個用例執行過程中,如何保證同一個界限上下文內的所有倉儲實例可以共享同一個工作單元實例?解決方案1倉儲采用依賴注入模式 + 使用IOC管理工作單元的生命周期(PerRequest或其它)。
代碼示例
復制代碼 代碼如下:www.wf0088.com
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<服務>();
buider.RegisterType<倉儲A>();
buider.RegisterType<倉儲B>();
buider.RegisterType<工作單元>().InstancePerLifetimeScope();
var container = buider.Build();
dynamic 服務 = container.Resolve<服務>();
//下邊兩行代碼輸出一樣
Console.WriteLine(服務.倉儲A.工作單元.GetHashCode());
Console.WriteLine(服務.倉儲B.工作單元.GetHashCode());
}
}
public class 服務
{
private readonly 倉儲A _倉儲A;
private readonly 倉儲B _倉儲B;
public 服務(倉儲A 倉儲A, 倉儲B 倉儲B)
{
_倉儲A = 倉儲A;
_倉儲B = 倉儲B;
}
public 倉儲A 倉儲A
{
get { return _倉儲A; }
}
public 倉儲B 倉儲B
{
get { return _倉儲B; }
}
}
public class 工作單元 { }
public class 倉儲A
{
private readonly 工作單元 _工作單元;
public 倉儲A(工作單元 工作單元)
{
_工作單元 = 工作單元;
}
public 工作單元 工作單元
{
get { return _工作單元; }
}
}
public class 倉儲B
{
private readonly 工作單元 _工作單元;
public 倉儲B(工作單元 工作單元)
{
_工作單元 = 工作單元;
}
public 工作單元 工作單元
{
get { return _工作單元; }
}
}
}
解決方案2
倉儲采用服務定位器模式 + 使用服務定位器或簡單工廠管理工作單元的生命周期(PerRequest或其它)。
代碼示例
復制代碼 代碼如下:www.wf0088.com
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 服務定位器;
static void Main(string[] args)
{
var buider = new ContainerBuilder();
buider.RegisterType<服務>();
buider.RegisterType<倉儲A>();
buider.RegisterType<倉儲B>();
buider.RegisterType<工作單元>().InstancePerLifetimeScope();
服務定位器 = buider.Build();
dynamic 服務 = 服務定位器.Resolve<服務>();
//下邊兩行代碼輸出一樣
Console.WriteLine(服務.倉儲A.工作單元.GetHashCode());
Console.WriteLine(服務.倉儲B.工作單元.GetHashCode());
}
}
public class 服務
{
private readonly 倉儲A _倉儲A;
private readonly 倉儲B _倉儲B;
public 服務(倉儲A 倉儲A, 倉儲B 倉儲B)
{
_倉儲A = 倉儲A;
_倉儲B = 倉儲B;
}
public 倉儲A 倉儲A
{
get { return _倉儲A; }
}
public 倉儲B 倉儲B
{
get { return _倉儲B; }
}
}
public class 工作單元 { }
public class 倉儲A
{
private readonly 工作單元 _工作單元;
public 倉儲A()
{
_工作單元 = Program.服務定位器.Resolve<工作單元>();
}
public 工作單元 工作單元
{
get { return _工作單元; }
}
}
public class 倉儲B
{
private readonly 工作單元 _工作單元;
public 倉儲B()
{
_工作單元 = Program.服務定位器.Resolve<工作單元>();
}
public 工作單元 工作單元
{
get { return _工作單元; }
}
}
}
由此示例可以看出,服務定位器和依賴注入可以混合在一起使用。這個例子我為了簡單,服務定位器和IOC容器是同一個實例。
有些系統將服務定位器的實現換成簡單工廠模式,他們本質上是一樣的(服務定位器是一個萬能工廠)。
代碼示例
復制代碼 代碼如下:www.wf0088.com
public class 工作單元工廠
{
public static 工作單元 創建()
{
var 工作單元 = (工作單元)CallContext.GetData("工作單元");
if (工作單元 == null)
{
工作單元 = new 工作單元();
CallContext.SetData("工作單元", 工作單元);
}
return 工作單元;
}
}
分享:ASP.NET中后臺注冊js腳本使用的方法對比接下來為大家介紹下使用Page.ClientScript.RegisterClientScriptBlock 和Page.ClientScript.RegisterStartupScript:區別
相關.Net教程:
- asp.net如何得到GRIDVIEW中某行某列值的方法
- .net SMTP發送Email實例(可帶附件)
- js實現廣告漂浮效果的小例子
- asp.net Repeater 數據綁定的具體實現
- Asp.Net 無刷新文件上傳并顯示進度條的實現方法及思路
- Asp.net獲取客戶端IP常見代碼存在的偽造IP問題探討
- VS2010 水晶報表的使用方法
- ASP.NET中操作SQL數據庫(連接字符串的配置及獲取)
- asp.net頁面傳值測試實例代碼
- DataGridView - DataGridViewCheckBoxCell的使用介紹
- asp.net中javascript的引用(直接引入和間接引入)
- 三層+存儲過程實現分頁示例代碼
- 相關鏈接:
- 教程說明:
.Net教程-關于DDD:管理工作單元實例的兩種模式的使用方法。