SmartPublisher设计之旅 — 优秀的设计才是让软件永葆青春的秘诀

本文发布之后,一些朋友给我提出一些非常好的建议,在此表示感谢。本文已修改,在此申明。

永葆青春的秘诀


随着社会经济的发展和生活水平的提高,人们越来越关心生活的质量,并且不断追求高品位的生活。近年来,类似"青春宝美容胶囊"、"太太口服液"保健类商品极其受到人们的青睐,许多"健身运动"、"榆珈"等成为一种生活的时尚。更有甚者,诸如"刘德华永葆青春减肥食谱"、"张曼玉永葆青春美体秘诀"、"永远25的谭咏麟"等明星们的健康生活秘方更是成为炒作的热点。

伦敦大学罗伯逊教授将永葆青春的方法归纳为七大秘诀。我想对我们这些高压力环境下的IT人来说应该有些启示,特给大家转载。

秘诀一:有氧适能,经常进行有氧锻炼,保持大脑供氧充足;

秘诀二:一份能激起活力的工作;

秘诀三:接受良好的教育和不断求知的欲望,保持思想开放;

秘诀四:最小的压力,轻松生活;

秘诀五:有朋友相伴和良好的社交关系;

秘诀六:良好的食欲,多吃鱼类、水果和蔬菜;

秘诀七:始终认为自己还很年轻心态。

只要遵循这七点,即便年纪再大,你也始终能保持头脑敏锐、充满活力。罗伯逊教授特别强调,年轻而乐观的心态非常重要:"如果你自己都认为已经老了,那又怎么可能看起来年轻呢?"

优秀的设计让软件永葆青春

软件和人一样,都有一个从产生到消亡的过程。众所周知,微软系列产品和技术的更新换代速度是空前的,其系列产品涉及应用的方方面面,已经构成了一个非常完整的产品体系,并且能根据用户的需求提供一套完美的解决方案。有时跟朋友开玩笑说,我们就是脱了裤子也追不上它。试想,如果微软对产品没有好的设计思想,Windows能从最初的3.X一直走到现在吗?能造就今天引领世界最新技术的微软帝国吗?优秀的设计对软件的开发、维护、升级扩展都起着非常重要的作用,因为设计的目的就是使软件容易修改、复用和扩展。

数据访问层设计

数据访问层:这一层才真正的实现了数据的存取,它为业务逻辑层提供服务。在这一层上不需要关注业务逻辑,只是存取数据。但在一个分布式的系统中,这种简单的实现是不够的,因供存取数据的不一定来自数据库,也可能来自其他数据文件,比如XML、Excel等,不同的数据库之间也有很大的差异,这些异构的数据对业务逻辑层来讲都是透明的,业务逻辑层没必要了解数据存取的细节。那么如何才能实现这种结构?通常的办法是为数据访问层提供一个接口,业务逻辑层只是调用接口所约定的方法,这样通过接口就可以实现很多异构数据的存取了。


下面以企业会员表T_Enterprise的基本操作为例,进行数据访问层的设计。

数据访问层的操作对象是具体的数据对象(企业会员)。首先我们为数据对象建模(Model)。Enterprise是一个与数据表结构相对应的、只有属性没有方法的类。

ContractedBlock.gif ExpandedBlockStart.gif Model
None.gifusing System;
None.gif
None.gif
namespace PublisherLib.DataLayer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public class Enterprise
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
private string _ID;
InBlock.gif
private string _Code;
InBlock.gif
private string _Password;
InBlock.gif
private string _Name;
InBlock.gif
private string _Type;
InBlock.gif
private string _Address;
InBlock.gif
private string _PostNumber;
InBlock.gif
private string _Tel;
InBlock.gif
private string _Fax;
InBlock.gif
private string _Email;
InBlock.gif
private string _Contactor;
InBlock.gif
private string _CreatedBy;
InBlock.gif
private DateTime _CreatedTime;
InBlock.gif
private string _LastModifiedBy;
InBlock.gif
private DateTime _LastModifiedTime;
InBlock.gif
InBlock.gif
public string ID
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._ID; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._ID = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Code
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Code; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Code = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Password
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Password; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Password = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Name; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Name = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Type
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Type; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Type = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Address
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Address; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Address = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string PostNumber
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._PostNumber; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._PostNumber = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Tel
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Tel; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Tel = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Fax
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Fax; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Fax = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Email
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Email; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Email = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string Contactor
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._Contactor; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._Contactor = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string CreatedBy
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._CreatedBy; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._CreatedBy = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public DateTime CreatedTime
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._CreatedTime; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._CreatedTime = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public string LastModifiedBy
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._LastModifiedBy; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._LastModifiedBy = value; }
ExpandedSubBlockEnd.gif }

InBlock.gif
public DateTime LastModifiedTime
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif
get dot.gif{ return this._LastModifiedTime; }
ExpandedSubBlockStart.gifContractedSubBlock.gif
set dot.gif{ this._LastModifiedTime = value; }
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

数据访问层的主要责任是对数据表的操作,主要包括新增(Add)、修改(Update)、删除(Delete)、查询(Select)等。所以我们把对数据表的基本操作抽象出来,定义一个统一接口(ITableOperator),如下:
ContractedBlock.gif ExpandedBlockStart.gif IDAL
None.gifusing System;
None.gif
using System.Data;
None.gif
None.gif
namespace PublisherLib.DataLayer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public interface ITableOperator<T>
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
bool Add(T model);
InBlock.gif
bool Update(T model);
InBlock.gif
bool Delete(string ID);
InBlock.gif
int GetCount(string strWhere);
InBlock.gif T GetModel(
string ID);
InBlock.gif DataSet GetList(
string strWhere);
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}
其企业会员数据对象的具体访问类EnterpriseDAL继承ITableOperator接口,并实现接口中的方法,运用返型,对持有的Enterprise对象进行操作。
ContractedBlock.gif ExpandedBlockStart.gif DAL
None.gifusing System;
None.gif
using System.Data;
None.gif
using System.Text;
None.gif
using PublisherLib.DbAccess;
None.gif
None.gif
namespace PublisherLib.DataLayer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public class EnterpriseDAL : ITableOperator<Enterprise>
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
public bool Add(Enterprise model)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
public bool Update(Enterprise model)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
public bool Delete(string ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
public int GetCount(string strCondition)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
public Enterprise GetModel(string ID)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif }

InBlock.gif
InBlock.gif
public DataSet GetList(string strCondition)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}
根据三层架构的体系结构关系,为了解除数据访问层与业务逻辑层之间的以来关系,我们可以提供一个数据工厂DataFactory来实现上下之间的松藕合目标。
ContractedBlock.gif ExpandedBlockStart.gif DataFactory
None.gifusing System;
None.gif
using System.Reflection;
None.gif
None.gif
namespace PublisherLib.DataLayer
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
public class DataFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//assembly和className可系统外部配置
InBlock.gif
public static ITableOperator<Enterprise> CreateEnterprise()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
string assembly = "PublisherLib.dll";
InBlock.gif
string className = "PublisherLib.DataLayer.EnterpriseDAL";
InBlock.gif
InBlock.gif
return (ITableOperator<Enterprise>)Assembly.Load(assembly).CreateInstance(className);
ExpandedSubBlockEnd.gif }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}
IDAL、Model、DAL与DataFactory之间的关系如下图:
DataLayer.JPG
按照同样的办法,可以设计出产品信息(ProductDAL)、站点信息(WebSiteDAL)、发布日志(PublishLogDAL)的数据访问类,继承ITableOperator接口,并且在DataFactory工厂类中创建实例。在这里就不把代码写出来了,我会在最后提供代码下载。

SmartPublisher技术架构
SmartPublisherArchitecture2.JPG


源代码下载地址:PublisherLib2008-1-12.rar

转载于:https://www.cnblogs.com/ruochen/archive/2008/01/12/1030374.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值