TheBeerHouse:六也谈类的结构下 (一半的辉煌)


 

14.gif最近工作实在是太忙了...所以本文简单了点呵呵先把上次剩下的说完整了

None.gif PollsProvider : DataAccess
None.gif类里面
None.gif    
static   public  PollsProvider Instance
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif         
get
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
if (_instance == null)
InBlock.gif               _instance 
= (PollsProvider)Activator.CreateInstance(
InBlock.gif                  Type.GetType(Globals.Settings.Polls.ProviderType));
InBlock.gif            
return _instance;
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif      }

//---这个是从配置接点实力化数据层
-----------------------------------------------------------------------------------------------------------

public class SqlPollsProvider : PollsProvider-上次没有说完这次继续14.gif--里面的方法基本一样,我找了个有代表的

ContractedBlock.gif ExpandedBlockStart.gif
ExpandedBlockStart.gifContractedBlock.gif   /**//// <summary>
InBlock.gif      
///通过实体插入数据       
ExpandedBlockEnd.gif
/// </summary>

None.gif      public override int InsertPoll(PollDetails poll)
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif{
InBlock.gif         
using (SqlConnection cn = new SqlConnection(this.ConnectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            SqlCommand cmd 
= new SqlCommand("tbh_Polls_InsertPoll", cn);
InBlock.gif            cmd.CommandType 
= CommandType.StoredProcedure;
InBlock.gif            cmd.Parameters.Add(
"@AddedDate", SqlDbType.DateTime).Value = poll.AddedDate;
InBlock.gif            cmd.Parameters.Add(
"@AddedBy", SqlDbType.NVarChar).Value = poll.AddedBy;
InBlock.gif            cmd.Parameters.Add(
"@QuestionText", SqlDbType.NVarChar).Value = poll.QuestionText;
InBlock.gif            cmd.Parameters.Add(
"@IsCurrent", SqlDbType.Bit).Value = poll.IsCurrent;
InBlock.gif            cmd.Parameters.Add(
"@PollID", SqlDbType.Int).Direction = ParameterDirection.Output;
InBlock.gif            cn.Open();
InBlock.gif            
int ret = ExecuteNonQuery(cmd);
InBlock.gif            
return (int)cmd.Parameters["@PollID"].Value;
ExpandedSubBlockEnd.gif         }

ExpandedBlockEnd.gif      }

//--得到一个数据实体类从而方便插入数据,最后返回影响的行数,这种写法是好的~~最后我会分析的哈哈14.gif

None.gif
None.gif
None.gif
using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
namespace  MB.TheBeerHouse.DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public class PollDetails
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif      
public PollDetails() dot.gif{ }
InBlock.gif
InBlock.gif      
public PollDetails(int id, DateTime addedDate, string addedBy, string questionText, bool isCurrent, bool isArchived, DateTime archivedDate, int votes)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
this.ID = id;
InBlock.gif         
this.AddedDate = addedDate;
InBlock.gif         
this.AddedBy = addedBy;
InBlock.gif         
this.QuestionText = questionText;
InBlock.gif         
this.IsCurrent = isCurrent;
InBlock.gif         
this.IsArchived = isArchived;
InBlock.gif         
this.ArchivedDate = archivedDate;
InBlock.gif         
this.Votes = votes;
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private int _id = 0;
InBlock.gif       
public int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif       
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif           
get dot.gifreturn _id;}
ExpandedSubBlockStart.gifContractedSubBlock.gif           
set dot.gif{ _id = value;}
ExpandedSubBlockEnd.gif       }

InBlock.gif
InBlock.gif      
private DateTime _addedDate = DateTime.Now;
InBlock.gif      
public DateTime AddedDate
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _addedDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _addedDate = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private string _addedBy = "";
InBlock.gif      
public string AddedBy
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _addedBy; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _addedBy = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private string  _questionText = "";
InBlock.gif      
public string QuestionText
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _questionText; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _questionText = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private bool _isCurrent = false;
InBlock.gif      
public bool IsCurrent
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _isCurrent; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _isCurrent = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private bool _isArchived = false;
InBlock.gif      
public bool IsArchived
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _isArchived; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _isArchived = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private DateTime _archivedDate = DateTime.MinValue;
InBlock.gif      
public DateTime ArchivedDate
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _archivedDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _archivedDate = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private int _votes = 0;
InBlock.gif      
public int Votes
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _votes; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
set dot.gif{ _votes = value; }
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

//--这就是数据实体类~~首先是构造写的不错但是如果是我的化~~用自动代码生成工具写(当然是自己做的)因为太累了49.gif,这个类的结构和表一样,用来传递信息给数据层操作的~~,不过最后我还是会说下这个设计

最后看看逻辑层把- -BizObject.cs
--不列代码了~~就是用来数据缓存,用户标识等等比较有意思的一锻我列出来哈哈

None.gif
ExpandedBlockStart.gifContractedBlock.gif      
/**/ /// <summary>
InBlock.gif      
/// 删除指定开头的缓存
ExpandedBlockEnd.gif      
/// </summary>

None.gif        protected   static   void  PurgeCacheItems( string  prefix)
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {
InBlock.gif         prefix 
= prefix.ToLower();
InBlock.gif         List
<string> itemsToRemove = new List<string>();
InBlock.gif         
InBlock.gif         IDictionaryEnumerator enumerator 
= BizObject.Cache.GetEnumerator();
InBlock.gif         
while (enumerator.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            
if (enumerator.Key.ToString().ToLower().StartsWith(prefix))
InBlock.gif               itemsToRemove.Add(enumerator.Key.ToString());
ExpandedSubBlockEnd.gif         }

InBlock.gif
InBlock.gif         
foreach (string itemToRemove in itemsToRemove)
InBlock.gif            BizObject.Cache.Remove(itemToRemove);
ExpandedBlockEnd.gif      }

None.gif


接下来就是具体的逻辑功能类的抽象类

 

None.gif namespace  MB.TheBeerHouse.BLL.Polls
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif   
public abstract class BasePoll : BizObject
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif      
private int _id = 0;
InBlock.gif      
public int ID
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _id; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
protected set dot.gif{ _id = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private DateTime _addedDate = DateTime.Now;
InBlock.gif      
public DateTime AddedDate
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _addedDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
protected set dot.gif{ _addedDate = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
private string _addedBy = "";
InBlock.gif      
public string AddedBy
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn _addedBy; }
ExpandedSubBlockStart.gifContractedSubBlock.gif         
protected set dot.gif{ _addedBy = value; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif
//--这个是他的节点方便你在扩展功能的时候使用
InBlock.gif
      protected static PollsElement Settings
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif         
get dot.gifreturn Globals.Settings.Polls; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
/**//// <summary>
InBlock.gif      
/// 在这里要调用逻辑层基类的方法来缓冲对象,怎么说那按照最近学习的概念对象必须对自己负责所以也是对的
ExpandedSubBlockEnd.gif      
/// </summary>

InBlock.gif      protected static void CacheData(string key, object data)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif         
if (Settings.EnableCaching && data != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif            BizObject.Cache.Insert(key, data, 
null,
InBlock.gif               DateTime.Now.AddSeconds(Settings.CacheDuration), TimeSpan.Zero);
ExpandedSubBlockEnd.gif         }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}
 

//--最后我们来看看真正的实现
Poll : BasePoll--不全写了太多了

ExpandedBlockStart.gif ContractedBlock.gif        /**/ /// <summary>
InBlock.gif      
/// 在这个方法中会创建一个新对象然后和数据层沟通插入数据~~
ExpandedBlockEnd.gif      
/// </summary>

None.gif        public   static   int  InsertPoll( string  questionText,  bool  isCurrent)
ExpandedBlockStart.gifContractedBlock.gif      
dot.gif {         
InBlock.gif    
//--实力化实体类
InBlock.gif
         PollDetails record = new PollDetails(0, DateTime.Now, BizObject.CurrentUserName,
InBlock.gif            questionText, isCurrent, 
false, DateTime.Now, 0);
InBlock.gif         
int ret = SiteProvider.Polls.InsertPoll(record);//--调用数据层的方法
InBlock.gif
         BizObject.PurgeCacheItems("polls_polls_true");
InBlock.gif         
if (isCurrent)
InBlock.gif            BizObject.PurgeCacheItems(
"polls_poll_current");
InBlock.gif         
return ret;
ExpandedBlockEnd.gif      }

最后说说一半的辉煌
关于逻辑层写首先第一个问题是类的继承明显太多了,根据最近看到的一本宝典上记载不要超过两层哈哈14.gif,写的最好的是逻辑层体现了类要对自己负责的思想
关于数据层:入口部分写的不错~~但是问题还是类的设计过多以及,最近看倒了~~Bridge模式精髓..书上是这样说的,Bridge模式中的实现部分部分各不相同,但都可以通过一个通用接口被访问.系统可以接纳新的实现部分,只要它也使用这个通用接口实现...(也就是说数据层不用这么负责我自己就做了一个可以还不错有空共享出来大家研究研究).睡觉去了...

- -共同进步...情多指教14.gif

转载于:https://www.cnblogs.com/ajaxren/archive/2007/07/04/805549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值