Adding base model U4-921 and creating new branch.

This commit is contained in:
Morten@Thinkpad-X220
2012-10-03 06:38:31 -02:00
parent f3e04ac871
commit 0b9bd3be50
7 changed files with 317 additions and 0 deletions

View File

@@ -16,5 +16,17 @@ namespace Umbraco.Core
action(i);
}
}
/// <summary>
/// Creates a Guid based on an integer value
/// </summary>
/// <param name="value"><see cref="int"/> value to convert</param>
/// <returns><see cref="Guid"/></returns>
public static Guid ToGuid(this int value)
{
byte[] bytes = new byte[16];
BitConverter.GetBytes(value).CopyTo(bytes, 0);
return new Guid(bytes);
}
}
}

View File

@@ -0,0 +1,221 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
namespace Umbraco.Core.Models.EntityBase
{
/// <summary>
/// Base Abstract Entity
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
public abstract class Entity : IEntity
{
private bool _hasIdentity;
private int? _hash;
private int _id;
private Guid _key;
/// <summary>
/// Integer Id
/// </summary>
[DataMember]
public int Id
{
get
{
return _id;
}
set
{
_id = value;
HasIdentity = true;
}
}
/// <summary>
/// Guid based Id
/// </summary>
/// <remarks>The key is currectly used to store the Unique Id from the
/// umbracoNode table, which many of the entities are based on.</remarks>
[DataMember]
public Guid Key
{
get
{
if (_key == Guid.Empty)
return Id.ToGuid();
return _key;
}
set { _key = value; }
}
/// <summary>
/// Gets or sets the Created Date
/// </summary>
[DataMember]
public DateTime CreatedDate { get; set; }
/// <summary>
/// Gets or sets the Modified Date
/// </summary>
[DataMember]
public DateTime ModifiedDate { get; set; }
/// <summary>
/// Property changed event
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Method to call on a property setter.
/// </summary>
/// <param name="propertyInfo">The property info.</param>
protected virtual void OnPropertyChanged(PropertyInfo propertyInfo)
{
_propertyChangedInfo[propertyInfo.Name] = true;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyInfo.Name));
}
}
/// <summary>
/// Method to call on entity saved when first added
/// </summary>
internal virtual void AddingEntity()
{
CreatedDate = DateTime.UtcNow;
ModifiedDate = DateTime.UtcNow;
}
/// <summary>
/// Method to call on entity saved/updated
/// </summary>
internal virtual void UpdatingEntity()
{
ModifiedDate = DateTime.UtcNow;
}
/// <summary>
/// Tracks the properties that have changed
/// </summary>
private readonly IDictionary<string, bool> _propertyChangedInfo = new Dictionary<string, bool>();
/// <summary>
/// Returns true if the property referenced by the name has been changed on the class
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public bool IsPropertyDirty(string propertyName)
{
return _propertyChangedInfo.Any(x => x.Key == propertyName);
}
/// <summary>
/// Returns true if any properties have been changed on the class
/// </summary>
/// <returns></returns>
public bool IsDirty()
{
return _propertyChangedInfo.Any();
}
/// <summary>
/// Resets dirty properties by clearing the dictionary used to track changes.
/// </summary>
internal void ResetDirtyProperties()
{
_propertyChangedInfo.Clear();
}
/// <summary>
/// Indicates whether the current entity has an identity, eg. Id.
/// </summary>
public virtual bool HasIdentity
{
get
{
return _hasIdentity;
}
protected set
{
_hasIdentity = value;
}
}
public static bool operator ==(Entity left, Entity right)
{
if (ReferenceEquals(null, left))
return false;
return left.Equals(right);
}
public static bool operator !=(Entity left, Entity right)
{
return !(left == right);
}
public virtual bool SameIdentityAs(IEntity other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return SameIdentityAs(other as Entity);
}
public virtual bool Equals(Entity other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
return SameIdentityAs(other);
}
public virtual Type GetRealType()
{
return GetType();
}
public virtual bool SameIdentityAs(Entity other)
{
if (ReferenceEquals(null, other))
return false;
if (ReferenceEquals(this, other))
return true;
if (GetType() == other.GetRealType() && HasIdentity && other.HasIdentity)
return other.Id.Equals(Id);
return false;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
return SameIdentityAs(obj as IEntity);
}
public override int GetHashCode()
{
if (!_hash.HasValue)
_hash = !HasIdentity ? new int?(base.GetHashCode()) : new int?(Id.GetHashCode() * 397 ^ GetType().GetHashCode());
return _hash.Value;
}
}
}

View File

@@ -0,0 +1,10 @@
namespace Umbraco.Core.Models.EntityBase
{
/// <summary>
/// Marker interface for aggregate roots
/// </summary>
public interface IAggreateRoot : IEntity
{
}
}

View File

@@ -0,0 +1,11 @@
namespace Umbraco.Core.Models.EntityBase
{
/// <summary>
/// An interface that defines the object is tracking property changes and if it is Dirty
/// </summary>
public interface ICanBeDirty
{
bool IsDirty();
bool IsPropertyDirty(string propName);
}
}

View File

@@ -0,0 +1,46 @@
using System;
using System.Runtime.Serialization;
namespace Umbraco.Core.Models.EntityBase
{
/// <summary>
/// Defines an Entity.
/// Entities should always have an Id, Created and Modified date
/// </summary>
/// <remarks>The current database schema doesn't provide a modified date
/// for all entities, so this will have to be changed at a later stage.</remarks>
public interface IEntity
{
/// <summary>
/// The Id of the entity
/// </summary>
[DataMember]
int Id { get; set; }
/// <summary>
/// Guid based Id
/// </summary>
/// <remarks>The key is currectly used to store the Unique Id from the
/// umbracoNode table, which many of the entities are based on.</remarks>
[DataMember]
Guid Key { get; set; }
/// <summary>
/// Gets or sets the Created Date
/// </summary>
[DataMember]
DateTime CreatedDate { get; set; }
/// <summary>
/// Gets or sets the Modified Date
/// </summary>
[DataMember]
DateTime ModifiedDate { get; set; }
/// <summary>
/// Indicates whether the current entity has an identity, eg. Id.
/// </summary>
[IgnoreDataMember]
bool HasIdentity { get; }
}
}

View File

@@ -0,0 +1,11 @@
namespace Umbraco.Core.Models.EntityBase
{
/// <summary>
/// Marker interface for value object, eg. objects without
/// the same kind of identity as an Entity (with its Id).
/// </summary>
public interface IValueObject
{
}
}

View File

@@ -37,6 +37,7 @@
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Web" />
<Reference Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
<Reference Include="System.Xml.Linq" />
@@ -91,6 +92,11 @@
<Compile Include="IO\IMediaFileSystem.cs" />
<Compile Include="Macros\MacroTagParser.cs" />
<Compile Include="Mandate.cs" />
<Compile Include="Models\EntityBase\Entity.cs" />
<Compile Include="Models\EntityBase\IAggreateRoot.cs" />
<Compile Include="Models\EntityBase\ICanBeDirty.cs" />
<Compile Include="Models\EntityBase\IEntity.cs" />
<Compile Include="Models\EntityBase\IValueObject.cs" />
<Compile Include="NameValueCollectionExtensions.cs" />
<Compile Include="ObjectResolution\WeightedPluginAttribute.cs" />
<Compile Include="PropertyEditors\DatePickerPropertyEditorValueConverter.cs" />