Created
August 10, 2010 13:58
-
-
Save mps/517301 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#region Usings | |
using System; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.ComponentModel; | |
using System.Linq; | |
using System.Runtime.Serialization; | |
#endregion | |
namespace Asi.AccuAuto.Domain | |
{ | |
///<summary> | |
/// Provides a base class for all business objects that encapsulate data and behavior that is independent of the presentation. | |
///</summary> | |
[DataContract] | |
public abstract class BusinessObject : INotifyPropertyChanged | |
{ | |
#region Private members | |
public event PropertyChangedEventHandler PropertyChanged; | |
private Dictionary<string, bool> _modified; | |
private Dictionary<string, bool> Modified | |
{ | |
get { return _modified ?? (_modified = new Dictionary<string, bool>()); } | |
} | |
protected List<BusinessRule> _rules; | |
#endregion | |
#region Constructor | |
protected BusinessObject() { } | |
#endregion | |
#region Protected members | |
///<summary> | |
/// A dictionary of rules that operates on this object. | |
///</summary> | |
protected List<BusinessRule> Rules | |
{ | |
get | |
{ | |
if (_rules == null) | |
{ | |
_rules = new List<BusinessRule>(); | |
DefineRules(); | |
} | |
return _rules; | |
} | |
} | |
#endregion | |
#region Public Properties | |
///<summary> | |
/// Gets or sets a value that indicates if BusinessRules for this object should be validated. | |
///</summary> | |
public bool IsValidationEnabled { get; set; } | |
#endregion | |
#region Protected methods | |
///<summary> | |
/// Defines the set of rules that operate on this object. | |
///</summary> | |
protected virtual void DefineRules() | |
{ | |
} | |
protected virtual void RaisePropertyChanged(string propertyName) | |
{ | |
if (PropertyChanged == null) | |
return; | |
PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); | |
foreach (var key in Modified.Keys.ToArray()) | |
Modified[key] = true; | |
} | |
protected virtual void RaisePropertyChanged(params string[] properties) | |
{ | |
foreach (var property in properties) | |
{ | |
RaisePropertyChanged(property); | |
} | |
} | |
#endregion | |
#region Public methods | |
///<summary> | |
/// Checks all business rules. | |
///</summary> | |
///<returns>A value indicating if all business rules defined for this object pass.</returns> | |
public virtual bool IsValid() | |
{ | |
return GetFailedBusinessRules().Count == 0; | |
} | |
///<summary> | |
/// Retrieves a collection of business rules that fail. | |
///</summary> | |
///<returns>A collection of business rules that fail.</returns> | |
public virtual ReadOnlyCollection<BusinessRule> GetFailedBusinessRules() | |
{ | |
var failedRules = Rules.Where(rule => !rule.IsValid).ToList(); | |
return failedRules.AsReadOnly(); | |
} | |
///<summary> | |
/// Validates a business rule for a specific property if one is defined. | |
///</summary> | |
///<param name="propertyName">The name of the property to check.</param> | |
///<exception cref="Exception"></exception> | |
public virtual void ValidatePropertyValue(string propertyName) | |
{ | |
if (!IsValidationEnabled) return; | |
if (_rules == null) return; | |
foreach (var rule in _rules) | |
{ | |
if (rule.PropertyName == propertyName) | |
{ | |
// Validate the rule. Note that I don't break the loop afterwards - there may be | |
// more than one rule per property so we need to continue to look for more rules | |
if (!rule.IsValid) | |
throw new Exception(rule.ErrorMessage); | |
} | |
} | |
} | |
public void EnableValidation() | |
{ | |
IsValidationEnabled = true; | |
} | |
public void DisableValidation() | |
{ | |
IsValidationEnabled = false; | |
} | |
public virtual bool DetectModified(string key) | |
{ | |
if (!Modified.ContainsKey(key)) | |
return true; | |
var val = Modified[key]; | |
return val; | |
} | |
public virtual void ResetModified(string key) | |
{ | |
Modified[key] = false; | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment