第一章(2):Nettiers实体层,抽像类EntityBaseCore

  1.         ///EntityBaseCore:所有数据库表实体类的父类(抽像类),定义了所有实体类必须要实现的行为
  2.         /// <summary>
  3.         /// The base object for each database table entity.
  4.         /// </summary>
  5.         ///继承接口IEntity(见第一章), INotifyPropertyChanged(向客户端发出某一属性值已更改的通知), IDataErrorInfo(提供用户界面可以绑定的自定义错误信息), IDeserializationCallback(示在完成整个对象图形的反序列化时通知类)
  6.         [Serializable]//实体为可序列化
  7.         public abstract partial class EntityBaseCore : IEntity, INotifyPropertyChanged, IDataErrorInfo, IDeserializationCallback
  8.         {
  9.                 #region Constructors
  10.                 /// <summary>
  11.                 /// Initializes a new instance of the <see cref="T:EntityBaseCore"/> class.
  12.                 /// </summary>
  13.                 protected EntityBaseCore()
  14.                 {
  15.                 }
  16.               #endregion
  17.             /// <summary>
  18.             /// Determines whether the entity is being tracked by the Locator.
  19.             /// </summary>
  20.                 [NonSerialized]
  21.                 private bool isEntityTracked = false;
  22.                 /// <summary>
  23.                 /// Suppresses Entity Events from Firing,
  24.                 /// useful when loading the entities from the database.
  25.                 /// </summary>
  26.             [NonSerialized]
  27.                 private bool suppressEntityEvents = false;
  28.                 /// <summary>
  29.                 ///  Used by in place editing of databinding features for new inserted row.
  30.                 /// Indicates that we are in the middle of an IBinding insert transaction.
  31.                 /// </summary>
  32.                 [NonSerialized]
  33.                 protected bool bindingIsNew = true;
  34.                 ///<summary>
  35.                 ///  The actual hashcode of the entity.
  36.                 ///</summary>
  37.                 private string entityHashCode;
  38.                 /// <summary>
  39.                 ///        The name of the underlying database table.
  40.                 /// </summary>
  41.                 public abstract string TableName { get;}
  42.                 /// <summary>
  43.                 ///                The name of the underlying database table's columns.
  44.                 /// </summary>
  45.                 /// <value>A string array that holds the columns names.</value>
  46.                 public abstract string[] TableColumns {get;}
  47.                 //private bool _isDeleted = false;
  48.                 /// <summary>
  49.                 ///         True if object has been <see cref="MarkToDelete"/>. ReadOnly.
  50.                 /// </summary>
  51.                 [BrowsableAttribute(false), XmlIgnoreAttribute()]
  52.                 public virtual bool IsDeleted
  53.                 {
  54.                         get { return this.EntityState == EntityState.Deleted; }
  55.                 }
  56.                 //private bool _isDirty = false;
  57.                 /// <summary>
  58.                 ///                Indicates if the object has been modified from its original state.
  59.                 /// </summary>
  60.                 /// <remarks>True if object has been modified from its original state; otherwise False;</remarks>
  61.                 [BrowsableAttribute(false), XmlIgnoreAttribute()]
  62.                 public virtual bool IsDirty
  63.                 {
  64.                         get { return this.EntityState != EntityState.Unchanged; }
  65.                 }
  66.                 //private bool _isNew = true;
  67.                 /// <summary>
  68.                 ///                Indicates if the object is new.
  69.                 /// </summary>
  70.                 /// <remarks>True if objectis new; otherwise False;</remarks>
  71.                 [BrowsableAttribute(false), XmlIgnoreAttribute()]
  72.                 public virtual bool IsNew
  73.                 {
  74.                         get { return this.EntityState == EntityState.Added; }
  75.                         set { this.EntityState = EntityState.Added; }
  76.                 }
  77.                 //private EntityState state = EntityState.Unchanged ;
  78.                 /// <summary>
  79.                 ///                Indicates state of object
  80.                 /// </summary>
  81.                 /// <remarks>0=Unchanged, 1=Added, 2=Changed</remarks>
  82.                 [BrowsableAttribute(false), XmlIgnoreAttribute()]
  83.                 public abstract EntityState EntityState { getset; }
  84.                 /// <summary>
  85.                 /// Accepts the changes made to this object.
  86.                 /// </summary>
  87.                 /// <remarks>
  88.                 /// After calling this method <see cref="IsDirty"/> and <see cref="IsNew"/> are false. <see cref="IsDeleted"/> flag remain unchanged as it is handled by the parent List.
  89.                 /// </remarks>
  90.                 public virtual void AcceptChanges()
  91.                 {
  92.                         this.bindingIsNew = false;
  93.                         EntityState = EntityState.Unchanged;
  94.                         OnPropertyChanged(string.Empty);
  95.                 }
  96.                 ///<summary>
  97.                 ///  Revert all changes and restore original values.  To be implemented in concrete class.
  98.                 ///</summary>
  99.                 public abstract void CancelChanges();
  100.                 ///<summary>
  101.                 ///   Marks entity to be deleted.
  102.                 ///</summary>
  103.                 public virtual void MarkToDelete()
  104.                 {
  105.                         if (this.EntityState != EntityState.Added)
  106.                                 this.EntityState = EntityState.Deleted ;
  107.                 }
  108.                 ///<summary>
  109.                 ///   Remove the "isDeleted" mark from the entity.
  110.                 ///</summary>
  111.                 public virtual void RemoveDeleteMark()
  112.                 {
  113.                         if (this.EntityState != EntityState.Added)
  114.                         {
  115.                                 this.EntityState = EntityState.Changed ;
  116.                         }
  117.                 }
  118.                 /// <summary>
  119.         /// Gets or sets the parent collection.
  120.         /// </summary>
  121.         /// <value>The parent collection.</value>
  122.                 [XmlIgnore]
  123.                 public abstract object ParentCollection{get;set;}
  124.                 #region Common Columns
  125.                 #endregion
  126.                 /// <summary>
  127.                 /// Object that contains data to associate with this object
  128.                 /// </summary>
  129.                 [NonSerialized]
  130.                 private object tag;
  131.                 /// <summary>
  132.                 ///     Gets or sets the object that contains supplemental data about this object.
  133.                 /// </summary>
  134.                 /// <value>Object</value>
  135.                 [System.ComponentModel.Bindable(false)]//指定成员不用于绑定
  136.                 [LocalizableAttribute(false)]
  137.                 [DescriptionAttribute("Object containing data to be associated with this object")]
  138.                 public virtual object Tag
  139.                 {
  140.                         get
  141.                         {
  142.                                 return this.tag;
  143.                         }
  144.                         set
  145.                         {
  146.                                 if (this.tag == value)
  147.                                         return;
  148.                                 this.tag = value;
  149.                         }
  150.                 }
  151.                 /// <summary>
  152.                 /// Determines whether this entity is being tracked.
  153.                 /// </summary>
  154.                 [System.ComponentModel.Bindable(false)]
  155.                 [BrowsableAttribute(false), XmlIgnoreAttribute()]
  156.                 public bool IsEntityTracked
  157.                 {
  158.                         get
  159.                         {
  160.                                 return isEntityTracked;
  161.                         }
  162.                         set
  163.                         {
  164.                                 isEntityTracked = value;
  165.                         }
  166.                 }
  167.                 /// <summary>
  168.                 /// Determines whether this entity is to suppress events while set to true.
  169.                 /// </summary>
  170.                 [System.ComponentModel.Bindable(false)]
  171.                 [BrowsableAttribute(false), XmlIgnoreAttribute()]
  172.                 public bool SuppressEntityEvents
  173.                 {
  174.                         get
  175.                         {
  176.                                 return suppressEntityEvents;
  177.                         }
  178.                         set
  179.                         {
  180.                                 suppressEntityEvents = value;
  181.                         }
  182.                 }
  183.                 ///<summary>
  184.                 /// Provides the tracking key for the <see cref="EntityLocator"/>
  185.                 ///</summary>
  186.                 [XmlIgnoreAttribute(), BrowsableAttribute(false)]
  187.                 public abstract string EntityTrackingKey {        getset; }
  188.                 ///<summary>
  189.                 /// Provides a unique HashCode throughout the lifetime of the entity.
  190.                 ///</summary>
  191.                 public override int GetHashCode()
  192.                 {
  193.                         if (entityHashCode == null)
  194.                                 entityHashCode = Guid.NewGuid().ToString();
  195.                         return entityHashCode.GetHashCode();
  196.                 }
  197.                 #region INotifyPropertyChanged Members
  198.                 /// <summary>
  199.                 /// Event to indicate that a property has changed.
  200.                 /// </summary>
  201.                 [field:NonSerialized]
  202.                 public event PropertyChangedEventHandler PropertyChanged;
  203.                 /// <summary>
  204.                 /// Called when a property is changed
  205.                 /// </summary>
  206.                 /// <param name="propertyName">The name of the property that has changed.</param>
  207.                 protected virtual void OnPropertyChanged(string propertyName)
  208.                 {
  209.                         if (!suppressEntityEvents)
  210.                         {
  211.                                 OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
  212.                         }
  213.                 }
  214.                 /// <summary>
  215.                 /// Called when a property is changed
  216.                 /// </summary>
  217.                 /// <param name="e">PropertyChangedEventArgs</param>
  218.                 protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
  219.                 {
  220.                         if (!suppressEntityEvents)
  221.                         {
  222.                                 //Validate the property
  223.                          ValidationRules.ValidateRules(e.PropertyName);
  224.                                 if (null != PropertyChanged)
  225.                                 {
  226.                                         PropertyChanged(this, e);
  227.                                 }
  228.                         }
  229.                 }
  230.                 #endregion
  231.                 #region IDataErrorInfo Members
  232.                 /// <summary>
  233.                 /// Gets an error message indicating what is wrong with this object.
  234.                 /// </summary>
  235.                 /// <value></value>
  236.                 /// <returns>An error message indicating what is wrong with this object. The default is an empty string ("").</returns>
  237.                 public string Error
  238.                 {
  239.                         get
  240.                         {
  241.                                 string errorDescription = string.Empty;
  242.                                 if (!IsValid)
  243.                                 {
  244.                                         errorDescription = ValidationRules.GetBrokenRules().ToString();
  245.                                 }
  246.                                 return errorDescription;
  247.                         }
  248.                 }
  249.                 /// <summary>
  250.                 /// Gets the <see cref="T:String"/> with the specified column name.
  251.                 /// </summary>
  252.                 /// <value></value>
  253.                 public string this[string columnName]
  254.                 {
  255.                         get
  256.                         {
  257.                                 string errorDescription = string.Empty;
  258.                                 if (!IsValid)
  259.                                 {
  260.                                          errorDescription = ValidationRules.GetBrokenRules().GetPropertyErrorDescriptions(columnName);
  261.                                 }
  262.                                 return errorDescription;
  263.                         }
  264.                 }
  265.               #endregion
  266.               #region Validation
  267.                 [NonSerialized]
  268.              private Validation.ValidationRules _validationRules;
  269.                 /// <summary>
  270.                 /// Returns the list of <see cref="Validation.ValidationRules"/> associated with this object.
  271.                 /// </summary>
  272.                 [XmlIgnoreAttribute()]
  273.                 protected Validation.ValidationRules ValidationRules
  274.                 {
  275.                         get
  276.                         {
  277.                                 if (_validationRules == null)
  278.                                 {
  279.                                         _validationRules = new Validation.ValidationRules(this);
  280.                                         //lazy init the rules as well.
  281.                                         AddValidationRules();
  282.                                 }
  283.                                 return _validationRules;
  284.                         }
  285.                 }
  286.                 /// <summary>
  287.                 /// Assigns validation rules to this object.
  288.                 /// </summary>
  289.                 /// <remarks>
  290.                 /// This method can be overridden in a derived class to add custom validation rules.
  291.                 ///</remarks>
  292.                 protected virtual void AddValidationRules()
  293.                 {
  294.                 }
  295.                 /// <summary>
  296.                 /// Returns a list of all the validation rules that failed.
  297.                 /// </summary>
  298.                 /// <returns><see cref="Validation.BrokenRulesList" /></returns>
  299.                 [XmlIgnoreAttribute()]
  300.                 public virtual Validation.BrokenRulesList BrokenRulesList
  301.                 {
  302.                         get
  303.                         {
  304.                                 return ValidationRules.GetBrokenRules();
  305.                         }
  306.                 }
  307.                 /// <summary>
  308.                 /// Adds a rule to the list of validated rules.
  309.                 /// </summary>
  310.                 /// <param name="handler">The method that implements the rule.</param>
  311.                 /// <param name="propertyName">
  312.                 /// The name of the property on the target object where the rule implementation can retrieve
  313.                 /// the value to be validated.
  314.                 /// </param>
  315.                 public void AddValidationRuleHandler(Validation.ValidationRuleHandler handler, String propertyName)
  316.                 {
  317.                    ValidationRules.AddRule(handler, propertyName);
  318.                 }
  319.                 /// <summary>
  320.                 /// Adds a rule to the list of validated rules.
  321.                 /// </summary>
  322.                 /// <remarks>
  323.                 /// </remarks>
  324.                 /// <param name="handler">The method that implements the rule.</param>
  325.                 /// <param name="args">
  326.                 /// A <see cref="Validation.ValidationRuleArgs"/> object specifying the property name and other arguments
  327.                 /// passed to the rule method
  328.                 /// </param>
  329.                 public void AddValidationRuleHandler(Validation.ValidationRuleHandler handler, Validation.ValidationRuleArgs args)
  330.                 {
  331.                     ValidationRules.AddRule(handler, args);
  332.                 }
  333.                 /// <summary>
  334.                 /// Returns <see langword="true" /> if the object is valid,
  335.                 /// <see langword="false" /> if the object validation rules that have indicated failure.
  336.                 /// </summary>
  337.                 [Browsable(false)]
  338.                 public virtual bool IsValid
  339.                 {
  340.                         get
  341.                         {
  342.                                 Validate();
  343.                                 return ValidationRules.IsValid;
  344.                         }
  345.                 }
  346.                 /// <summary>
  347.                 /// Force this object to validate itself using the assigned business rules.
  348.                 /// </summary>
  349.                 /// <remarks>Validates all properties.</remarks>
  350.                 public void Validate()
  351.                 {
  352.                         ValidationRules.ValidateRules();
  353.                 }
  354.                 /// <summary>
  355.                 /// Force the object to validate itself using the assigned business rules.
  356.                 /// </summary>
  357.                 /// <param name="propertyName">Name of the property to validate.</param>
  358.                 public void Validate(string propertyName)
  359.                 {
  360.                         ValidationRules.ValidateRules(propertyName);
  361.                 }
  362.                 /// <summary>
  363.                 /// Force the object to validate itself using the assigned business rules.
  364.                 /// </summary>
  365.                 /// <param name="column">Column enumeration representing the column to validate.</param>
  366.                 public void Validate(System.Enum column)
  367.                 {
  368.                         Validate(column.ToString());
  369.                 }
  370.               #endregion
  371.                 #region IDeserializationCallback Members
  372.                         /// <summary>
  373.                         /// Runs when the entire object graph has been deserialized.
  374.                         /// </summary>
  375.                         /// <param name="sender">The object that initiated the callback. The functionality for this parameter is not currently implemented.</param>
  376.                         public void OnDeserialization(object sender)
  377.                         {
  378.                                 if(!suppressEntityEvents)
  379.                                 {
  380.                                         ValidationRules.Target = this;
  381.                                         AddValidationRules();
  382.                                 }
  383.                         }
  384.                 #endregion
  385.         }

你可能感兴趣的:(数据库,String,null,图形)