首页 »博文摘选 » 深度blogengine.net:深度解析 BlogEngine.Net »正文
深度blogengine.net:深度解析 BlogEngine.Net
来源: 发布时间:星期五, 2009年12月11日 浏览:0次 评论:0
最近买了本 C#2005 Business Objects 基于 个CSLA.NET开源项目展开 大致看了下 觉得比较深奥 需要结合CSLA源码 不过在深入源码的前我决定先复习 个BlogEngine.Net BlogEngine得核心类可以理解为是CSLA 阉割版 BusinessBase 是BlogEngine所有业务 基类 1.首先我们看下它继承了那些接口 public abstract BusinessBase<TYPE, KEY> : IDataErrorInfo, INot yPropertyChanged, IChangeTracking, IDisposable where TYPE : BusinessBase<TYPE, KEY>, 1.1 IDataErrorInfo 该功能提供用户界面可以绑定 自定义 信息 public Error
{
get { ValidationMessage; }
}
public this[ columnName]
{
get
{
(_BrokenRules.ContainsKey(columnName))
_BrokenRules[columnName];
.Empty;
}
}
这个接口虽然被实现我发现没有使用不清楚作者如何处理了
1.2 INotyPropertyChanged
向客户端发出某属性值已更改通知
/// <summary> /// Occurs when this instance is marked dirty. /// It means the instance has been changed but not saved. /// </summary> public event PropertyChangedEventHandler PropertyChanged;
MSDN: ms-help://MS.MSDNQTR.v90.chs/fxref_system/html/d7424bfd-9de2-e553-d64e-57a78a2134d4.htm
INotyPropertyChanged 接口用于向客户端(通常是执行绑定客户端)发出某属性值已更改通知
例如考虑个带有名为 FirstName 属性 Person 对象若要提供般性属性更改通知则 Person 类型实现 INotyPropertyChanged 接口并在 FirstName 更改时引发 PropertyChanged 事件如果数据源实现 INotyPropertyChanged 并且您执行是异步操作则不得在后台线程上对数据源进行更改相反应在后台线程上读取这些数据并将这些数据合并到 UI 线程上列表中
若要在将客户端和数据源进行绑定时发出更改通知则绑定类型应具有下列任功能:
-
实现 INotyPropertyChanged 接口(首选)
-
为绑定类型每个属性提供更改事件
1.3 IChangeTracking
定义查询对象更改以及重置更改后状态机制
/// <summary>
/// Res the object state to unchanged by accepting the modications.
/// </summary>
void IChangeTracking.AcceptChanges
{
Save;
}
我也没有发现任何
1.4 IDisposable
这个大家都很熟悉了
private bool _IsDisposed;
protected bool IsDisposed
{
get { _IsDisposed; }
}
protected virtual void Dispose(bool disposing)
{
(this.IsDisposed)
;
(disposing)
{
_ChangedProperties.Clear;
_BrokenRules.Clear;
_IsDisposed = true;
}
}
public void Dispose
{
Dispose(true);
GC.SuppressFinalize(this);
}
1.5 where TYPE : BusinessBase<TYPE, KEY>,
所有业务类都必须继承这个BusinessBase否则vs就会送你个error
2.BusinessBase具有那些功能
2.1 获取对象状态
2.1.1 IsNew private bool _IsNew = true; 化=true
True: 介绍说明这个对象是新创建 False:介绍说明已经存在这个对象了
2.1.2 IsDeletedIsChanged
也是用来做判断标记
2.2 Validation 规则
IsValid 标记 是否需要验证
ValidationRules()思路方法为虚思路方法也是在继承类中实现看下BlogRollItem.cs代码
AddRule()思路方法中最后个参数确定了是否应用这些规则
1 private StringDictionary _BrokenRules = StringDictionary(); 2 3 /// <summary> 4 /// Add or remove a broken rule. 5 /// </summary> 6 /// <param name="propertyName">The name of the property.</param> 7 /// <param name="errorMessage">The description of the error</param> 8 /// <param name="isBroken">True the validation rule is broken.</param> 9 protected virtual void AddRule( propertyName, errorMessage, bool isBroken) 10 { 11 (isBroken) 12 { 13 _BrokenRules[propertyName] = errorMessage; 14 } 15 16 { 17 (_BrokenRules.ContainsKey(propertyName)) 18 { 19 _BrokenRules.Remove(propertyName); 20 } 21 } 22 } 23 24 protected abstract void ValidationRules(); 25 26 public bool IsValid 27 { 28 get 29 { 30 ValidationRules(); 31 this._BrokenRules.Count == 0; 32 } 33 }
BlogRollItem.cs
1 protected override void ValidationRules() 2 { 3 AddRule("Title", "Title must be ", .IsNullOrEmpty(Title)); 4 AddRule("BlogUrl", "BlogUrl must be ", BlogUrl == null); 5 }
最后在提交按钮的前做了处理 代码截取了片段
ValidationMessage 是将信息写成串个思路方法
BusinessBase .cs
1 public virtual SaveAction Save( userName, password) 2 { 3 4 (IsDeleted && !(IsAuthenticated || isValidated)) 5 throw .Security.SecurityException("You are not authorized to delete the object"); 6 7 (!IsValid && !IsDeleted) 8 throw InvalidOperationException(ValidationMessage); 9 10 SaveAction.None; 11 }
2.3 通用数据访问
运用了模板模式在其继承类实现DataSelect思路方法其继承类A类思路方法只需XXX.Load(id).
而无需在A类设定对象状态体现了代码高度重用
代码如下:
1 /// <summary> 2 /// Loads an instance of the object based on the Id. 3 /// </summary> 4 /// <param name="id">The unique identier of the object</param> 5 public TYPE Load(KEY id) 6 { 7 TYPE instance = TYPE(); 8 instance = instance.DataSelect(id); 9 instance.Id = id; 10 11 (instance != null) 12 { 13 instance.MarkOld(); 14 instance; 15 } 16 17 null; 18 } 19 20 /// <summary> 21 /// Retrieves the object from the data store and populates it. 22 /// </summary> 23 /// <param name="id">The unique identier of the object.</param> 24 /// <s>True the object exists and is being populated successfully</s> 25 protected abstract TYPE DataSelect(KEY id);
再看个思路方法:
1 /// <summary> 2 /// Saves the object to the data store (inserts, updates or deletes). 3 /// </summary> 4 public virtual SaveAction Save( userName, password) 5 { 6 bool isValidated; 7 (userName == .Empty) 8 isValidated = false; 9 10 isValidated = Membership.ValidateUser(userName, password); 11 12 (IsDeleted && !(IsAuthenticated || isValidated)) 13 throw .Security.SecurityException("You are not authorized to delete the object"); 14 15 (!IsValid && !IsDeleted) 16 throw InvalidOperationException(ValidationMessage); 17 18 (IsDisposed && !IsDeleted) 19 throw InvalidOperationException(.Format(CultureInfo.InvariantCulture, "You cannot save a disposed {0}", this.GetType().Name)); 20 21 (IsChanged) 22 { 23 Update(); 24 } 25 26 SaveAction.None; 27 }
也是运用了模板模式和上段代码区别在于使用了虚思路方法
整理总结:BlogEngine.Net是个非常好学习源码
相关文章
读者评论
发表评论
|
|