diff --git a/Source/BSN.Commons/BSN.Commons.csproj b/Source/BSN.Commons/BSN.Commons.csproj index 53808dc..3269580 100644 --- a/Source/BSN.Commons/BSN.Commons.csproj +++ b/Source/BSN.Commons/BSN.Commons.csproj @@ -51,11 +51,11 @@ - - + + - + all runtime; build; native; contentfiles; analyzers; buildtransitive @@ -63,7 +63,7 @@ - + diff --git a/Source/BSN.Commons/ISoftDeletable.cs b/Source/BSN.Commons/ISoftDeletable.cs new file mode 100644 index 0000000..672e158 --- /dev/null +++ b/Source/BSN.Commons/ISoftDeletable.cs @@ -0,0 +1,39 @@ +using System; + +namespace BSN.Commons +{ + /// + /// Provides a mechanism for soft deletion. + /// + /// Soft deletion means that this object is not completely destroyed, but it goes in a deleted state, + /// and you can ask this object if you are deleted or not? + /// + public interface ISoftDeletable + { + /// + /// Shows the time of deletion. + /// + /// Time based on UTC. + /// + /// + /// If this object is not deleted, this property returns . + /// + DateTime DeletedAt + { + get; + } + + /// + /// Shows if this object is deleted or not. + /// + bool IsDeleted + { + get; + } + + /// + /// Delete this object. + /// + void SoftDelete(); + } +} diff --git a/Source/BSN.Commons/SoftDeletableBase.cs b/Source/BSN.Commons/SoftDeletableBase.cs new file mode 100644 index 0000000..23d4c2e --- /dev/null +++ b/Source/BSN.Commons/SoftDeletableBase.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace BSN.Commons +{ + /// + public abstract class SoftDeletableBase : ISoftDeletable + { + /// + public DateTime DeletedAt + { + get; + private set; + } + + /// + public bool IsDeleted => DeletedAt != DateTime.MinValue; + + /// + public virtual void SoftDelete() + { + if (DeletedAt != DateTime.MinValue) + { + throw new InvalidOperationException("This object is already deleted."); + } + + DeletedAt = DateTime.UtcNow; + } + } +}