回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目。

NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的;它包含了开发人员和架构师都可以重用的DDD层。

Github地址:https://github.com/cesarcastrocuba/nlayerappv3

NLayerAppV3的基础结构层一共分为两个部分。处理数据相关的基础组件和Cross-Cutting的基础组件。

处理数据相关的基础组件主要包含UOW和仓储的实现;

Cross-Cutting的基础组件目前主要包含数据适配器、国际化、验证;

本篇介绍NLayerAppV3的Infrastructure(基础结构层)的Data部分和Application(应用层)

1、Infrastructure(基础结构层)的Data部分

Data部分是处理数据相关的基础组件主要包含UOW和仓储的实现。

UOW的实现:BaseContext继承了DbContext和IQueryableUnitOfWork
DbContext是EF Core数据库上下文,包Microsoft.EntityFrameworkCore
IQueryableUnitOfWork继承IUnitOfWork和ISql,是EF Core方式的契约定义

Isql定义了支持sql语句的方式

Repository仓储的层超类型,通过构造函数注入了IQueryableUnitOfWork和ILogger,定义了EF Core方式的CURD以及查询过滤,包括分页等行为

MainBCUnitOfWork实现了BaseContext。示例使用内存数据库的方式来演示,当然,根据实际需要,可以很容易地扩展使用sqlserver、mysql、sqlite等,这也符合了开闭原则

BankAccountRepository是BankAccount仓储,继承Repository<BankAccount>,IBankAccountRepository,通过构造函数注入了MainBCUnitOfWork和ILogger,提供了一个GetAll方法,用来获取BankAccount的集合。

public class BankAccountRepository
:Repository<BankAccount>,IBankAccountRepository
{
#region Constructor /// <summary>
/// Create a new instance
/// </summary>
/// <param name="unitOfWork">Associated unit of work</param>
/// <param name="logger">Logger</param>
public BankAccountRepository(MainBCUnitOfWork unitOfWork,
ILogger<Repository<BankAccount>> logger)
: base(unitOfWork, logger)
{ } #endregion #region Overrides /// <summary>
/// Get all bank accounts and the customer information
/// </summary>
/// <returns>Enumerable collection of bank accounts</returns>
public override IEnumerable<BankAccount> GetAll()
{
var currentUnitOfWork = this.UnitOfWork as MainBCUnitOfWork; var set = currentUnitOfWork.CreateSet<BankAccount>(); return set.Include(ba => ba.Customer)
.AsEnumerable(); }
#endregion
}

2、Application(应用层)

协调领域模型与其它应用、包括事务调度、UOW、数据转换等。
IService定义了应用层服务的契约。Service实现了IService,通过构造函数注入IRepository,为什么要注入IRepository?
因为要获取实体的相关信息,就必须通过仓储去操作聚合,而聚合是通过聚合根跟外部联系的。
ProjectionsExtensionMethods作用是使用扩展方法,将实体转换为DTO或者将实体集合转换为DTO的集合
IBankAppService定义了Bank的应用层契约。有开户、查找账户、锁定账户、查找账户活动集、转账等业务。
BankAppService实现了IBankAppService。通过构造函数注入IBankAccountRepository、ICustomerRepository、IBankTransferService、ILogger。
IBankAccountRepository是BankAccount的仓储契约;
ICustomerRepository是Customer用户的仓储契约;
IBankTransferService是转账的领域服务;

转账方法的代码:

public void PerformBankTransfer(BankAccountDTO fromAccount, BankAccountDTO toAccount, decimal amount)
{
//Application-Logic Process:
// 1º Get Accounts objects from Repositories
// 2º Start Transaction
// 3º Call PerformTransfer method in Domain Service
// 4º If no exceptions, commit the unit of work and complete transaction if (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)); }

Application.MainBoundedContext.DTO项目
项目中是所有的DTO对象和使用AutoMapper转换的配置转换规则的Profile文件。
这里的Profile文件是在Infrastructure(基础设施层)Crosscutting部分的Adapter(适配器)中AutomapperTypeAdapterFactory(AutoMapper的类型转换器创建工厂)创建AutomapperTypeAdapter(AutoMapper的类型转换器)时使用反射的方式调用的。

NLayerAppV3-Infrastructure(基础结构层)的Data部分和Application(应用层)的更多相关文章

  1. Enterprise Library - Data Access Application Block 6.0.1304

    Enterprise Library - Data Access Application Block 6.0.1304 企业库,数据访问应用程序块 6.0.1304 企业库的数据访问应用程序块的任务简 ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(五) Data Access Application Block 企业库数据库访问模块通过抽象工厂模式,允许用户 ...

  3. Pass Infrastructure基础架构(下)

    Pass Infrastructure基础架构(下) pass注册  PassRegistration该类在示例中简要显示了各种pass类型的定义 .该机制允许注册pass类,以便可以在文本pass管 ...

  4. Pass Infrastructure基础架构(上)

    Pass Infrastructure基础架构(上) Operation Pass OperationPass : Op-Specific OperationPass : Op-Agnostic De ...

  5. sql基础之DDL(Data Definition Languages)

    好久没写SQL语句了,复习一下. DDL数据定义语言,DDL定义了不同的数据段.数据库.表.列.索引等数据库对象的定义.经常使用的DDL语句包含create.drop.alter等等. 登录数据:my ...

  6. NLayerAppV3--基础结构层(Cross-Cutting部分)

    回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...

  7. (转)EntityFramework之领域驱动设计实践

    EntityFramework之领域驱动设计实践 - 前言 EntityFramework之领域驱动设计实践 (一):从DataTable到EntityObject EntityFramework之领 ...

  8. EntityFramework之领域驱动设计实践

    EntityFramework之领域驱动设计实践 - 前言 EntityFramework之领域驱动设计实践 (一):从DataTable到EntityObject EntityFramework之领 ...

  9. 【系统架构】软件核心复杂性应对之道-领域驱动DDD(Domain-Driven Design)

    前言 领域驱动设计是一个开放的设计方法体系,目的是对软件所涉及到的领域进行建模,以应对系统规模过大时引起的软件复杂性的问题,本文将介绍领域驱动的相关概念. 一.软件复杂度的根源 1.业务复杂度(软件的 ...

随机推荐

  1. 3.Longest Substring Without Repeating Characters(string; HashTable)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. Labyrinth(记忆化BFS)

    Labyrinth http://codeforces.com/problemset/problem/1064/D time limit per test 2 seconds memory limit ...

  3. 3sum, 3sum closest

    [抄题]: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  4. IBM MQ 与spring的整合

    文件名:applicationContext-biz-mq.xml 新浪博客把里面的代码全部转换成HTML了,所以无法粘贴 可以查看CSDN里面的:http://blog.csdn.net/xiazo ...

  5. tp中引入js、css、img的问题

    方法一: 直接把js.css.img放到网站公共目录/Public/下. 然后直接在模板文件中使用__PUBLIC__进行替换. 方法二: 在模块配置文件config.php中配置指定的路径,如下: ...

  6. CodeSmith生成SQL Server视图的实体类脚本/对应的生成模板

    C#生成sql视图的实体类 using System;using System.Text;using CodeSmith.Engine;using SchemaExplorer;using Syste ...

  7. cookie、session、token区分

    https://www.cnblogs.com/moyand/p/9047978.html http://www.cnblogs.com/JamesWang1993/p/8593494.html Co ...

  8. 老板说你的UI设计的不高级?你肯定没用这7个技巧...

    对于每个网页设计师而言,在设计过程中总会碰到需要作出设计决策的时候.也许你的公司并没有全职设计师,而需求上则要求设计出全新的UI:又或者你正在制作一个你自己的个人项目,而你希望它比 Bootstrap ...

  9. Jmeter发送某个request时而成功,时而失败(处理办法:失败的时候尝试重新发送这个HTTP request)

    Jmeter发送某个request时而成功,时而失败 Maybe it’s Jmeter’s problem, after all, is not a commercial software. And ...

  10. 31. The New Bread Earners 挣钱养家的新军

    31. The New Bread Earners 挣钱养家的新军 ① They call them the new bread earners.They are women,and they are ...