NLayerAppV3--.net Core2实现的DDD分层架构
? ? ? ? 雖然每天的工作都是CURD、還是使用著傳統(tǒng)的三層架構(gòu)、使用著.net Framework,但是這并不能影響我學(xué)習(xí)DDD和.net core。
總是希望軟件在應(yīng)對(duì)復(fù)雜多變的場(chǎng)景時(shí)能夠游刃有余,在解決問(wèn)題時(shí)能夠多一種選擇。
? ? ? ? 很早就有關(guān)注大神dax.net的NLayerApp系列博文https://www.cnblogs.com/daxnet/category/304141.html
? ? ? ? 近期入坑.net core,看了進(jìn)擊的辣條的文章Asp.Net Core微服務(wù)初體驗(yàn),很有收獲,感謝感謝。
? ? ? ? 抽時(shí)間讀了下NLayerAppV3項(xiàng)目的源碼,Github地址:https://github.com/cesarcastrocuba/nlayerappv3
分享下體會(huì),希望相互交流學(xué)習(xí),謝謝!
1、項(xiàng)目簡(jiǎn)介
NLayerAppV3是一個(gè)使用.net 2.1實(shí)現(xiàn)的經(jīng)典DDD的分層架構(gòu)的項(xiàng)目。
NLayerAppV3是在NLayerAppV2的基礎(chǔ)上,使用.net core2.1進(jìn)行重新構(gòu)建的;它包含了開(kāi)發(fā)人員和架構(gòu)師都可以重用的DDD層;以銀行和博客上下文示例。
2、項(xiàng)目結(jié)構(gòu)
放一張DDD風(fēng)格架構(gòu)l圖。主要分為四層:表現(xiàn)層、應(yīng)用層、領(lǐng)域?qū)雍突A(chǔ)結(jié)構(gòu)層。
詳細(xì)的各層的含義可以查看dax.net的文章https://www.cnblogs.com/daxnet/archive/2011/05/10/2042095.html
1.1-Presentation(表現(xiàn)層)
系統(tǒng)和用戶的交互層。UI展示給終端用戶、收集用戶的反饋信息。
1.2-Distributed Services(分布式服務(wù)層)
為應(yīng)用程序提供遠(yuǎn)程訪問(wèn);如果是http協(xié)議,一般設(shè)計(jì)為WebApi。
1.2.1-Seedwork --DistributedServices.Seedwork
分布式服務(wù)的Controller、Filter等
1.2.2-MainBoundedContext
--DistributedServices.MainBoundedContext
以銀行的業(yè)務(wù)示例,構(gòu)建上下文、對(duì)外提供WebApi服務(wù),結(jié)合Swagger UI
--DistributedServices.MainBoundedContext.Tests
DistributedServices.MainBoundedContext的測(cè)試項(xiàng)目
1.2.3-BlogContext
--DistributedServices.BlogBoundedContext
以博客業(yè)務(wù)示例,構(gòu)建上下文,對(duì)外提供WebApi服務(wù),結(jié)合Swagger UI
--DistributedServices.BlogBoundedContext.Tests
DistributedServices.BlogBoundedContext項(xiàng)目的測(cè)試項(xiàng)目
1.3-Application(應(yīng)用層)
協(xié)調(diào)領(lǐng)域模型與其它應(yīng)用、包括事務(wù)調(diào)度、UOW、數(shù)據(jù)轉(zhuǎn)換等。
主要作用:通過(guò)倉(cāng)儲(chǔ)來(lái)訪問(wèn)持久層;對(duì)不同領(lǐng)域的數(shù)據(jù)組織整理;協(xié)調(diào)領(lǐng)域?qū)ο笾g、領(lǐng)域?qū)ο笈c基礎(chǔ)層的協(xié)作關(guān)系;應(yīng)用服務(wù)等
1.3.1-Seedwork --Application.Seedwork
應(yīng)用層共用接口、實(shí)現(xiàn)的封裝
1.3.2-MainBoundedContext
--Application.MainBoundedContext
業(yè)務(wù)模塊的應(yīng)用層服務(wù)接口和實(shí)現(xiàn)
--Application.MainBoundedContext.DTO
應(yīng)用層業(yè)務(wù)模塊的數(shù)據(jù)轉(zhuǎn)換等
--Application.MainBoundedContext.Tests
Application.MainBoundedContext的測(cè)試項(xiàng)目
1.3.3-BlogBoundedContext
同1.3.2,不同的是業(yè)務(wù)換為博客業(yè)務(wù)
1.4-Domain(領(lǐng)域?qū)?#xff09;
展現(xiàn)業(yè)務(wù)/領(lǐng)域邏輯、業(yè)務(wù)處理狀態(tài),以及實(shí)現(xiàn)業(yè)務(wù)規(guī)則,它同時(shí)也包含了領(lǐng)域?qū)ο蟮臓顟B(tài)信息。
1.4.1-Seedwork
--Domain.Seedwork
領(lǐng)域?qū)訉?shí)體基類、倉(cāng)儲(chǔ)接口、UOW接口、值對(duì)象基類、規(guī)約等
--Domain.Seedwork.Tests
Domain.Seedwork的測(cè)試項(xiàng)目
1.4.2-MainBoundedContext
--Domain.MainBoundedContext
實(shí)體、值對(duì)象、領(lǐng)域服務(wù)、聚合根、聚合及工廠
--Domain.MainBoundedContext.Tests
Domain.MainBoundedContext的測(cè)試項(xiàng)目
1.4.3-BlogBoundedContext
同1.4.2 業(yè)務(wù)邏輯換成了博客
1.5-Infrastructure(基礎(chǔ)設(shè)施層)
1.5.1-Data??
為應(yīng)用程序的數(shù)據(jù)存取提供服務(wù),它可以是應(yīng)用程序本身的持久化機(jī)制,也可以是外部系統(tǒng)提供的數(shù)據(jù)訪問(wèn)的Web Service等
--Infrastructure.Data.Seedwork
UOW的實(shí)現(xiàn)基類、倉(cāng)儲(chǔ)的實(shí)現(xiàn)基類
--Infrastructure.Data.MainBoundedContext
UOW的實(shí)現(xiàn)、倉(cāng)儲(chǔ)的實(shí)現(xiàn)
--Infrastructure.Data.MainBoundedContext.Tests
Infrastructure.Data.MainBoundedContext的測(cè)試項(xiàng)目
1.5.2-CrossCutting?
該層提供了能被其它各層訪問(wèn)的通用技術(shù)框架,比如異常捕獲與處理、日志、認(rèn)證、授權(quán)、驗(yàn)證、跟蹤、監(jiān)視、緩存等等。
--Infrastructure.Crosscutting
適配器、國(guó)際化、驗(yàn)證等接口的封裝
--Infrastructure.Crosscutting.NetFramework
適配器、國(guó)際化、驗(yàn)證等實(shí)現(xiàn)、國(guó)際化依賴的資源
--Infrastructure.Crosscutting.Tests
Infrastructure.Crosscutting的測(cè)試項(xiàng)目
3、示例
以DistributedServices.MainBoundedContext項(xiàng)目的BankAccounts的轉(zhuǎn)賬api示例,說(shuō)明下我的理解
[HttpPut]public void PerformTransfer([FromBody]BankAccountDTO from, [FromBody]BankAccountDTO to, [FromBody]decimal amount){_bankAppService.PerformBankTransfer(from, to, amount);} View Code_bankAppService通過(guò)構(gòu)造函數(shù)注入。
readonly IBankAppService _bankAppService;public BankAccounts(IBankAppService bankAppService){_bankAppService = bankAppService;} View CodeIBankAppService是應(yīng)用層的服務(wù)接口
對(duì)應(yīng)的應(yīng)用層有接口的實(shí)現(xiàn)BankAppService
public BankAppService(IBankAccountRepository bankAccountRepository, // the bank account repository dependencyICustomerRepository customerRepository, // the customer repository dependency IBankTransferService transferService,ILogger<BankAppService> logger){//check preconditionsif (bankAccountRepository == null)throw new ArgumentNullException("bankAccountRepository");if (customerRepository == null)throw new ArgumentNullException("customerRepository");if (transferService == null)throw new ArgumentNullException("trasferService");_bankAccountRepository = bankAccountRepository;_customerRepository = customerRepository;_transferService = transferService;_logger = logger;_resources = LocalizationFactory.CreateLocalResources();} View CodeBankAppService通過(guò)構(gòu)造函數(shù)注入了領(lǐng)域?qū)拥你y行賬號(hào)倉(cāng)儲(chǔ)IBankAccountRepository和客戶倉(cāng)儲(chǔ)ICustomerRepository、轉(zhuǎn)賬服務(wù)等,全部依賴于接口。
public void PerformBankTransfer(BankAccountDTO fromAccount, BankAccountDTO toAccount, decimal amount){//Application-Logic Process: // 1o Get Accounts objects from Repositories// 2o Start Transaction// 3o Call PerformTransfer method in Domain Service// 4o If no exceptions, commit the unit of work and complete transactionif (BankAccountHasIdentity(fromAccount)&&BankAccountHasIdentity(toAccount)){var source = _bankAccountRepository.Get(fromAccount.Id);var target = _bankAccountRepository.Get(toAccount.Id);if (source != null & target != null) // if all accounts exist {using (TransactionScope scope = new TransactionScope()){//perform transfer _transferService.PerformTransfer(amount, source, target);//comit unit of work _bankAccountRepository.UnitOfWork.Commit();//complete transaction scope.Complete();}}else_logger.LogError(_resources.GetStringResource(LocalizationKeys.Application.error_CannotPerformTransferInvalidAccounts));}else_logger.LogError(_resources.GetStringResource(LocalizationKeys.Application.error_CannotPerformTransferInvalidAccounts));} View Code實(shí)現(xiàn)轉(zhuǎn)賬的功能,輸入?yún)?shù)都是Dto對(duì)象,通過(guò)AutoMapper或者其它映射工具完成數(shù)據(jù)轉(zhuǎn)換;通過(guò)倉(cāng)儲(chǔ)訪問(wèn)聚合,獲取源和目標(biāo)賬號(hào)信息、開(kāi)啟事務(wù),UOW提交轉(zhuǎn)賬操作。
DistributedServices.MainBoundedContext的StartUp中注冊(cè)IOC
倉(cāng)儲(chǔ)、領(lǐng)域服務(wù)、應(yīng)用層服務(wù)、適配器、國(guó)際化、驗(yàn)證、Swagger UI等。
運(yùn)行項(xiàng)目
在DistributedServices.MainBoundedContext目錄下打開(kāi)cmd命令,輸入dotnet restore,完成之后,輸入dotnet run
運(yùn)行結(jié)果:
?4、想法和計(jì)劃
跟.net core microservice結(jié)合
使用ocelot搭建服務(wù)網(wǎng)關(guān)、結(jié)合identityserver4實(shí)現(xiàn)授權(quán)認(rèn)證;polly服務(wù)熔斷;限流;consul服務(wù)發(fā)現(xiàn);Appolo配置中心;Skywalking追蹤結(jié)果。
轉(zhuǎn)載于:https://www.cnblogs.com/net-yuan/p/NLayerAppV3.html
總結(jié)
以上是生活随笔為你收集整理的NLayerAppV3--.net Core2实现的DDD分层架构的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 海思接收bt1120外同步视频流
- 下一篇: [Altium Designer 202