I never saw the need to add isdirty to a domainentity untill today.
After showing the user a list of objects on the screen in a datagridview (which I don’t use very often) I then had to save that list. And there are two ways to go about it. Let nHibernate handle it or pick out the changed objects. Since the nhibernate route was a bit slow. I decided to do it myself and add an IsDirty to the Domain object.
First of all why haven’t I done this before? Because I think a domainobject doesn’t need to concern itself with those kinds of things it is there to enforce the bussiness rules and for nothing else. But I think I can compromise. Since all the Domainobjects have a corresponding interface I can make Different Domainobjects for differnt purposses. I can have one for nhibernate with the attributes I can have a simple one and I can have one that inherits from the DomainEntity superclass. I really couldn’t care less since the view uses the interface and doesn’t know about the implementation.
So here is the IDomainEntity interface.
Namespace Common.Interfaces
''' <summary>
''' Interface for our Superclass DomainEntity
''' </summary>
''' <remarks></remarks>
<CLSCompliant(True)> _
Public Interface IDomainEntity
''' <summary>
''' Tells me when an object has been changed
''' </summary>
''' <value></value>
''' <returns></returns>
''' <remarks></remarks>
ReadOnly Property IsDirty() As Boolean
''' <summary>
''' Clears the isdirtystate
''' </summary>
''' <remarks></remarks>
Sub ClearDirtyStatus()
End Interface
End Namespace```
And this is the implementation
Namespace Common “’ <summary> “’ Superclass DomainEntity which holds the Isdirty logic “’ </summary> “’ <remarks></remarks> <CLSCompliant(True)> _ Public MustInherit Class DomainEntity Implements Interfaces.IDomainEntity
#Region “ Private members “ “’ <summary> “’ Private member to hold a boolean saying when my object is dirty “’ </summary> “’ <remarks></remarks> Private _IsDirty As Boolean #End Region
#Region “ Constructors “ “’ <summary> “’ Empty constructor “’ </summary> “’ <remarks></remarks> Public Sub New()
End Sub
#End Region
#Region “ Public properties “ “’ <summary> “’ Tells me when an object has been changed “’ </summary> “’ <value></value> “’ <returns>true when the object has been changed.</returns> “’ <remarks>Sets IsDirty to false</remarks> Public ReadOnly Property IsDirty() As Boolean Implements Interfaces.IDomainEntity.IsDirty Get Return _IsDirty End Get End Property #End Region
#Region “ Public methods “ “’ <summary> “’ Clears the isdirtystate “’ </summary> “’ <remarks></remarks> Public Sub ClearDirtyStatus() Implements Interfaces.IDomainEntity.ClearDirtyStatus _IsDirty = False End Sub
''' <summary>
''' Protectd method to be used by the inheriting classes.
''' </summary>
''' <remarks>Sets IsDirty to true</remarks>
Protected Sub SetDirty()
_IsDirty = True
End Sub
#End Region
End Class
End Namespace ``` Don’t hold back on the comments, but be gentle I’m a nice guy afterall. And I know about CSLA.NET.