NLayerAppV3--基础结构层(Cross-Cutting部分)
回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目。
NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的;它包含了开发人员和架构师都可以重用的DDD层。
Github地址:https://github.com/cesarcastrocuba/nlayerappv3
NLayerAppV3的基础结构层一共分为两个部分。处理数据相关的基础组件和Cross-Cutting的基础组件。
处理数据相关的基础组件主要包含UOW和仓储的实现;
Cross-Cutting的基础组件目前主要包含数据适配器、国际化、验证;
本节我们主要介绍Cross-Cutting的基础组件。
这部分相关的项目主要有两个Infrastructure.Crosscutting和Infrastructure.Crosscutting.NetFramework。
Infrastructure.Crosscutting封装了数据适配器、国际化、验证相关的接口;
Infrastructure.Crosscutting.NetFramework包含了Infrastructure.Crosscutting中相关契约的实现;
1、Infrastructure.Crosscutting
--Adapter数据适配器
这部分是数据适配或者数据转换的功能契约。
数据转换是什么?为什么要数据转换?
DTO:数据转换对象。
数据转换主要是用于将用户的输入转换为DTO,在持久化的时候又将DTO转为领域模型进行持久化;如果是用户请求数据的话,则是相反的过程。
首先定义了一个类型转换的契约ITypeAdapter。
接口中定义了类型转换的方法,给我一个TSource,我还你一个TTarget。
TTarget Adapt<TSource, TTarget>(TSource source)
where TTarget : class, new()
where TSource : class;
ITypeAdapterFactory是适配器的工厂契约
ITypeAdapter Create();
创建适配器,返回适配器的契约。
TypeAdapterFactory是适配器工厂。
静态类,有两个方法,设置当前工厂和创建适配器的方法。
static ITypeAdapterFactory _currentTypeAdapterFactory = null; #endregion #region Public Static Methods /// <summary>
/// Set the current type adapter factory
/// </summary>
/// <param name="adapterFactory">The adapter factory to set</param>
public static void SetCurrent(ITypeAdapterFactory adapterFactory)
{
_currentTypeAdapterFactory = adapterFactory;
}
/// <summary>
/// Create a new type adapter from currect factory
/// </summary>
/// <returns>Created type adapter</returns>
public static ITypeAdapter CreateAdapter()
{
return _currentTypeAdapterFactory.Create();
}
--Localization 国际化
结构跟Adapter类似。
ILocalization定义了国际化的契约。
public interface ILocalization
{
string GetStringResource(string key);
string GetStringResource(string key, CultureInfo culture);
string GetStringResource<T>(T key) where T : struct, IConvertible;
string GetStringResource<T>(T key, CultureInfo culture) where T : struct, IConvertible;
}
GetStringResource:根据key和CultureInfo获取value。
ILocalizationFactory是创建国际化的工厂。
LocalizationFactory是国际化的工厂。
有两个方法,创建当前国际化的工厂SetCurrent和创建当前资源的CreateLocalResources。
LocalizationKeys定义了资源的枚举。
--Validator 验证
结构跟Adapter类似。
IEntityValidator实体验证的契约。
包含两个方法:是否验证IsValid和获取验证信息集GetInvalidMessages。
/// <summary>
/// Perform validation and return if the entity state is valid
/// </summary>
/// <typeparam name="TEntity">The type of entity to validate</typeparam>
/// <param name="item">The instance to validate</param>
/// <returns>True if entity state is valid</returns>
bool IsValid<TEntity>(TEntity item)
where TEntity : class; /// <summary>
/// Return the collection of errors if entity state is not valid
/// </summary>
/// <typeparam name="TEntity">The type of entity</typeparam>
/// <param name="item">The instance with validation errors</param>
/// <returns>A collection of validation errors</returns>
IEnumerable<String> GetInvalidMessages<TEntity>(TEntity item)
where TEntity : class;
IEntityValidatorFactory是实体验证工厂的契约。
定义了一个创建方法。
EntityValidatorFactory是实体验证工厂。
有两个方法,一个是设置当前工厂SetCurrent,另一个是创建验证器CreateValidator。
static IEntityValidatorFactory _factory = null; #endregion #region Public Methods /// <summary>
/// Set the log factory to use
/// </summary>
/// <param name="factory">Log factory to use</param>
public static void SetCurrent(IEntityValidatorFactory factory)
{
_factory = factory;
} /// <summary>
/// Create the validator factory
/// </summary>
/// <returns></returns>
public static IEntityValidator CreateValidator()
{
return (_factory != null) ? _factory.Create() : null;
}
2、Infrastructure.Crosscutting.NetFramework
--Adapter 数据适配器
使用AutoMapper库实现了类型转换适配器。
AutomapperTypeAdapter实现了ITypeAdapter,完成了源目标数据类型到时目标数据类型的转换工作。
AutomapperTypeAdapterFactory实现了ITypeAdapterFactory。
在构造函数里使用反射的方式,加载Application层中的Profiler文件,实现类型转换的配置。
Create方法返回AutomapperTypeAdapter对象。
--Localization 国际化
ResourcesManager实现了ILocalization,完成了从资源文件读取信息实现国际化。
ResourcesManagerFactory实现了ILocalizationFactory。
Create方法返回了ResourcesManager对象。
Resources文件夹下放的是资源文件。
--Validator 验证
DataAnnotationsEntityValidator实现了IEntityValidator。
使用System.ComponentModel.DataAnnotations验证实体的正确性。
DataAnnotationsEntityValidatorFactory实现了IEntityValidatorFactory。
Create方法返回了DataAnnotationsEntityValidator对象。
总结:NLayerAppV3项目的Infrastructure(基础设施层)CrossCutting部分目前有三部分内容:适配器、国际化、验证。
定义了适配器、国际化、验证的契约以及它们的工厂;
实现了AutoMapper类型适配器、、国际化资源的获取、System.ComponentModel.DataAnnotations方式的实体验证。
参考:dax.net文章 https://www.cnblogs.com/daxnet/archive/2011/06/01/2067134.html
NLayerAppV3--基础结构层(Cross-Cutting部分)的更多相关文章
- NLayerAppV3-Infrastructure(基础结构层)的Data部分和Application(应用层)
回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...
- 1、HA Cluster基础原理
Linux Cluster --> linux集群类型分三种: LB:负载均衡,LoadBalance HA:双机集群系统,指高可用性集群,High Available HP:Hadoop ...
- 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)
一. mybatis的高级映射 1 单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...
- Spring Boot使用AOP实现REST接口简易灵活的安全认证
我们继续上一篇文章的分析,本文将通过AOP的方式实现一个相对更加简易灵活的API安全认证服务. 我们先看实现,然后介绍和分析AOP基本原理和常用术语. 一.Authorized实现 1.定义注解 pa ...
- Spring03-AOP
一. AOP介绍 1. Aop介绍 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编 ...
- Dubbo x Cloud Native 服务架构长文总结(很全)
Dubbo x Cloud Native 服务架构长文总结(很全) mercyblitz SpringForAll社区 3天前 分享简介 Cloud Native 应用架构随着云技术的发展受到业界特别 ...
- Spring AOP详解和实现方式
一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善. ...
- Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)
参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...
- NLayerAppV3--DDD之领域层
回顾:NLayerAppV3是一个使用.net 2.1实现的经典DDD的分层架构的项目. NLayerAppV3是在NLayerAppV2的基础上,使用.net core2.1进行重新构建的:它包含了 ...
随机推荐
- java并发:Condition的应用
Condition类可以使线程等待,也可以唤醒线程.Condition类的await方法和Object类的wait方法等效Condition类的signal方法和Object类的notify方法等效C ...
- 使用HttpModule实现网址重写
1. 修改配置文件: <httpModules> <</span>add name="html" type="HttpModule&quo ...
- php的反射
我们可以在PHP运行时,通过PHP的反射动态的获取类的方法.属性.参数等详细信息. 用途:插件的设计,文档的自动生成,扩充PHP语言. <?php class Person { const ...
- linux用户和组
1.用户隶属于用户组的. 2.用户与用户组配置文件 1)用户组配置文件 /etc/group 第一列:用户组的组名 第二列:组密码(真正的密码存储在了gshadow中) 第三列:用户组组ID,用户组唯 ...
- spring框架之数组,集合(List,Set,Map),Properties等的注入
先编写User类: package com.huida.demo4; import java.util.Arrays; import java.util.List; import java.util. ...
- struts框架问题六之从值栈中获取值
6. 问题六: 在JSP中获取值栈的数据 * 总结几个小问题: > 访问root中数据 不需要# > 访问context其它对象数据 加 # > 如果向root中存入对象的话,优先使 ...
- 05 Maven 生命周期和插件
Maven 生命周期和插件 除了坐标.依赖以及仓库之外, Maven 另外两个核心概念是生命周期和插件.在有关 Maven 的日常使用中,命令行的输入往往就对应了生命周期,如 mvn package ...
- async 和 await
win8 app开发中使用async,await可以更方便地进行异步开发. async,await的使用可参考代码:Async Sample: Example from "Asynchron ...
- Devexpress VCL Build v2014 vol 14.2.6 发布
终于支持XE8 了.需要这么长时间吗? New Major Features in 14.2 What's New in VCL Products 14.2 Feature Highlights To ...
- 美团点评2017校招笔试真题-算法工程师B
美团点评2017校招笔试真题-算法工程师B 1.以下关于经典的k-means聚类的说法哪个是错误的? A:k-means聚类算法是全局收敛的 B:k-means的聚类结果和初始聚类中心点的选取有关 C ...