系统架构师-基础到企业应用架构-企业应用架构
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统架构师-基础到企业应用架构-企业应用架构相关的知识,希望对你有一定的参考价值。
一、上篇回顾
我们先来回顾下上篇讲解的内容,我们前面的几节分别讲述了,业务逻辑层、数据访问层、服务层、表现层,我们了解了这些分层的职责和分层之间的大概的关联
关系,本篇可能主要是简单的介绍下企业应用的几类模式,结合这几个分层直接的交互来完成系统功能的构建。我们还是先对我们学习的四个分层的职责和功能做个大
概的回顾,我们先来看看下图来回顾下我们讲述的内容。
我想通过上图,大家能回忆起我们讲述的相关内容,然后整理好自己的思路,我们本文将会针对这几个分层进行相应的模式的讲解,并且会结合实例来说明企业应
用架构的简单应用。我想这也是大家关心的内容,我也希望大家能多提出宝贵意见,大家共同提高。
之前说是提供PDF文件的下载的,以提供给需要学习的朋友更方便的形式,但是由于最近时间有限,没能整理好,等过阵子提供一个完整的整合好的PDF版本,提
供给大家下载,还望大家见谅。
二、开篇
本篇我们将针对我们前面讲述的几个分层做个简单的整合,就是通过一个简单的实例代码来说明下,我们给出的一个可能的企业应用架构模式去完成企业应用,并
且顺带分析下,企业应用中可能存在的瓶颈等,当然可能本章只是点出一些概念,然后在后面的章节中去完成,比如我们的性能优化,性能瓶颈等,这些我们后面通过
这篇:系统架构师-基础到企业应用架构-性能优化(架构瓶颈)(后续篇)详细的分析。当然由于本章主要是基于前面讲述的内容的一个整合和分析,可能部分观点
还有更好的改进方案,还希望大家多提出宝贵意见和您的建议,谢谢!那么我们先来看看我们本章讲述的内容吧:
本章主要讲述下各分层之间的交互方式及可行的设计方案,并且分析我们在每个分层采用什么样的模式,及给出相应的示例代码,当然这里可能讲述的不会是最好
的实现方案,还期待大家提出更好的改进方案。
三、本文提纲
1、内容回顾
2、开篇
3、本文提纲
4、企业应用架构实例
4.1、企业应用架构中的物理分层
4.2、数据访问层分析
4.3、业务逻辑层分析
4.4、服务层分析
4.5、表现层分析
4.6、分析总结
5、结束语
6、系列进度
7、下篇预告
四、企业应用架构实例
4.1、企业应用架构的物理分层
本节将会分层的相关介绍,并且分析分层的原因,为下篇:系统架构师-基础到企业应用架构-分层[上篇] 做铺垫。
分层我想对大家来说都不会太陌生。因为我们平时在开发的过程中一般都是采用分层架构的方式,通过分层将系统的功能进行划分,我们将系
统中的若干有共同特征的部分放在一个分层中,然后通过分层之间的交互去完成系统的功能。
我们看看企业应用的几种应用程序模式:
1、单击应用程序就是一些本地服务的应用程序,这个应该没啥特别的难度,例如Office应用程序,当然现在有在线版本的Office,这个应用的太多了。
2、客户端/服务器的形式,特别是在WinForm、WPF、SilverLight方面的应用等,例如我们的常用的OutLook,虽然不是采用.NET平台开发的,但是原理一样就
是通过客户端通过通信服务来访问服务器,然后取回相应的邮件信息返回给客户端,相当于我们通过客户端的形式来访问Web服务。
3、Web服务:通过将开发的Web服务器部署在服务器上,然后我们就可以通过输入HTTP网址的形式来访问web服务,我们这里是通过浏览器来完成的,其实这个
模式也是客户端/服务器的形式。只不过这时的客户端变成浏览器+服务页面,然后通过浏览器来完成Web服务的访问。
4、其他服务:我们这里主要是针对一些其他的设备,例如通信设备等方面的访问,比如说我们现在提供一个卫星定位的服务,那么通过设备调用服务来完成,告知
用户的GPS定位信息等。很多这个方面的应用。这个方向应该是未来的一个主流吧。
我们上面简单的讲述了系统的物理分层的形式,那么我们来看看如何对系统的功能进行分层,也就是我们下面要详细讲解的实例分析。
4.2、数据访问层分析
数据访问层我们知道是唯一一个可以与数据库之间直接进行交互的分层,数据访问层必须满足的几个功能和职责,我们这里就不复述了,我们本篇可能就是直接给
出数据访问层的相关实现,并且分析出数据访问层与其他层之间的交互。
大家可能一看就知道是什么意思,可能在您的眼中应该就和你们平时项目中采用的分层结构有点类似吧。
我这里的数据访问层提供所有的数据库访问服务。我们来看看基本的服务功能吧
我们这里定义了一个DDL语句操作的枚举,CUD的枚举,因为我们提供了一个统一的数据访问服务,通过Excute来完成。
/// <summary> /// 数据访问操作类型 /// </summary> public enum DDLType { Create, Update, Delete } |
这里用枚举来定义数据库字段与查询条件值之间的关系
/// <summary> /// 数据字段与字段值之间的关系表达式 /// </summary> public enum FieldExpressionType { EquleTo, BeginThen, LessThen, BetweenAnd, Like } |
我们再来看看我们定义的查询条件的接口
/// <summary> /// 定义子查询接口 /// </summary> public interface ICondition { int Add( string fieldName, object value,FieldExpressionType fetType); int Add(ICondition condition); string WhereCondition; string OrderCondition; } |
数据访问层接口代码
/// <summary> /// 定义统一的数据访问服务 /// </summary> public interface IDataAccess { #region CUD int Create<T>(T item); int Update<T>(T item); int Delete<T>(T item); int Execute<T>(T item,DDLType ddlType); #endregion #region Read服务 List<T> Query<T>(ICondition condition); T GetModelByPrimaryKey<T>( object key); List<T> GetAll<T>(); #endregion #region 事务操作 void BeginTransaction(); bool Commit(); bool RollBack(); bool IsTransaction; #endregion } |
我们知道具体的代码我们是通过反射+特性的形式来完成数据库操作语句的生成的,我们来看看可行的代码,属性项特性的定义。
/// <summary> /// Model中的字段属性特性 /// </summary> [AttributeUsage(AttributeTargets.All, AllowMultiple = false )] public class PropertyAttribute : Attribute { private string dbColumnName; private bool isPrimary; private DbType dbType; private object defaultValue; private bool isIdentify; private int length; public string DbColumnName { get { return this .dbColumnName; } set { this .dbColumnName = value; } } public bool IsPrimary { get { return this .isPrimary; } set { this .isPrimary = value; } } public bool IsIdentify { get { return this .isIdentify; } set { this .isIdentify = value; } } public DbType DbType { get { return this .dbType; } set { this .dbType = value; } } public object DefaultValue { get { return this .defaultValue; } set { this .defaultValue = value; } } public int DbLength { get { return this .length; } set { this .length = value; } } public PropertyAttribute( string dbName, bool isPrimery, DbType type, object dValue) { this .dbColumnName = dbName; this .isPrimary = isPrimery; this .dbType = type; this .defaultValue = this .GetDefaultValue(); } private object GetDefaultValue() { return new object (); } public PropertyAttribute( string dbName) { this .dbColumnName = dbName; this .isPrimary = false ; this .dbType = DbType.String; this .defaultValue = this .GetDefaultValue(); } public PropertyAttribute( string dbName, bool isPrimery) { this .dbColumnName = dbName; this .isPrimary = isPrimery; this .dbType = DbType.String; this .defaultValue = this .GetDefaultValue(); } public PropertyAttribute( string dbName, bool isPrimery, DbType type) { this .dbColumnName = dbName; this .isPrimary = isPrimery; this .dbType = type; this .defaultValue = null ; } } |
我们再来看看基于表上的特性
/// <summary> /// 基于表的自定义特性类 /// </summary> [AttributeUsage(AttributeTargets.All, AllowMultiple = false )] public class TableAttribute : Attribute { private string dbTableName; public TableAttribute( string dbName) { this .dbTableName = dbName; } public string TableName { get { return this .dbTableName; } set { this .dbTableName = value; } } } 根据反射取出表名 /// <summary> /// 返回Model对应的数据库表名 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public string DbTableName<T>(T model) { string dbName = string .Empty; DPM.Common.TableAttribute attr = null ; object [] attributes = model.GetType().GetCustomAttributes( typeof (DPM.Common.TableAttribute), true ); if (attributes.Length > 0) { attr = (DPM.Common.TableAttribute)attributes[0]; } if (attr != null ) dbName = attr.TableName; return dbName; } 根据反射取出表中的列 /// <summary> /// 动态创建表中字段列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public string InitDbColumns<T>(T model) { StringBuilder commandBuilder = new StringBuilder(); DPM.Common.PropertyAttribute attr = null ; foreach (PropertyInfo property in model.GetType().GetProperties()) { object [] attributes = property.GetCustomAttributes( typeof (DPM.Common.PropertyAttribute), true ); if (attributes.Length > 0) { attr = (DPM.Common.PropertyAttribute)attributes[0]; } if (commandBuilder.length>0) commandBuilder.Append(“,”); commandBuilder.Append(attr.DbColumnName); } return commandBuilder.ToString(); } |
当然这里只是给出了简单的示例,我们来看看生成的Insert 语句的格式吧
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/// <summary> /// 动态创建表中字段更新列表 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="model"></param> /// <returns></returns> public string InitInsertColumns<T>(T model) { StringBuilder filedBuilder = new StringBuilder(); StringBuilder valueBuilder = new StringBuilder(); StringBuilder commandBuilder = new StringBuilder(); DPM.Common.PropertyAttribute attr = null ; foreach (PropertyInfo property in model.GetType().GetProperties()) { object [] attributes = property.GetCustomAttributes( typeof (DPM.Common.PropertyAttribute), true ); if (attributes.Length > 0) { attr = (DPM.Common.PropertyAttribute)attributes[0]; } if (attr.DbColumnName == "" ) continue ; if (attr.IsIdentify) continue ; if (filedBuilder.Length > 0) { &n |
以上是关于系统架构师-基础到企业应用架构-企业应用架构的主要内容,如果未能解决你的问题,请参考以下文章