[英]Entity Framework 4.0 and DDD patterns
我將EntityFramework用作ORM,我有一個簡單的POCO域模型,帶有兩個表示值對象和實體對象模式(Evans)的基類。 這兩種模式都是關於兩個對象的相等性,因此我覆蓋了Equals和GetHashCode方法。 這是這兩個類:
public abstract class EntityObject<T>{
protected T _ID = default(T);
public T ID {
get { return _ID; }
protected set { _ID = value; }
}
public sealed override bool Equals(object obj) {
EntityObject<T> compareTo = obj as EntityObject<T>;
return (compareTo != null) &&
((HasSameNonDefaultIdAs(compareTo) ||
(IsTransient && compareTo.IsTransient)) &&
HasSameBusinessSignatureAs(compareTo));
}
public virtual void MakeTransient() {
_ID = default(T);
}
public bool IsTransient {
get {
return _ID == null || _ID.Equals(default(T));
}
}
public override int GetHashCode() {
if (default(T).Equals(_ID))
return 0;
return _ID.GetHashCode();
}
private bool HasSameBusinessSignatureAs(EntityObject<T> compareTo) {
return ToString().Equals(compareTo.ToString());
}
private bool HasSameNonDefaultIdAs(EntityObject<T> compareTo) {
return (_ID != null && !_ID.Equals(default(T))) &&
(compareTo._ID != null && !compareTo._ID.Equals(default(T))) &&
_ID.Equals(compareTo._ID);
}
public override string ToString() {
StringBuilder str = new StringBuilder();
str.Append(" Class: ").Append(GetType().FullName);
if (!IsTransient)
str.Append(" ID: " + _ID);
return str.ToString();
}
}
public abstract class ValueObject<T, U> : IEquatable<T> where T : ValueObject<T, U> {
private static List<PropertyInfo> Properties { get; set; }
private static Func<ValueObject<T, U>, PropertyInfo, object[], object> _GetPropValue;
static ValueObject() {
Properties = new List<PropertyInfo>();
var propParam = Expression.Parameter(typeof(PropertyInfo), "propParam");
var target = Expression.Parameter(typeof(ValueObject<T, U>), "target");
var indexPar = Expression.Parameter(typeof(object[]), "indexPar");
var call = Expression.Call(propParam, typeof(PropertyInfo).GetMethod("GetValue", new[] { typeof(object), typeof(object[]) }),
new[] { target, indexPar });
var lambda = Expression.Lambda<Func<ValueObject<T, U>, PropertyInfo, object[], object>>(call, target, propParam, indexPar);
_GetPropValue = lambda.Compile();
}
public U ID { get; protected set; }
public override Boolean Equals(Object obj) {
if (ReferenceEquals(null, obj)) return false;
if (obj.GetType() != GetType()) return false;
return Equals(obj as T);
}
public Boolean Equals(T other) {
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
foreach (var property in Properties) {
var oneValue = _GetPropValue(this, property, null);
var otherValue = _GetPropValue(other, property, null);
if (null == oneValue && null == otherValue) return false;
if (false == oneValue.Equals(otherValue)) return false;
}
return true;
}
public override Int32 GetHashCode() {
var hashCode = 36;
foreach (var property in Properties) {
var propertyValue = _GetPropValue(this, property, null);
if (null == propertyValue)
continue;
hashCode = hashCode ^ propertyValue.GetHashCode();
}
return hashCode;
}
public override String ToString() {
var stringBuilder = new StringBuilder();
foreach (var property in Properties) {
var propertyValue = _GetPropValue(this, property, null);
if (null == propertyValue)
continue;
stringBuilder.Append(propertyValue.ToString());
}
return stringBuilder.ToString();
}
protected static void RegisterProperty(Expression<Func<T, Object>> expression) {
MemberExpression memberExpression;
if (ExpressionType.Convert == expression.Body.NodeType) {
var body = (UnaryExpression)expression.Body;
memberExpression = body.Operand as MemberExpression;
}
else
memberExpression = expression.Body as MemberExpression;
if (null == memberExpression)
throw new InvalidOperationException("InvalidMemberExpression");
Properties.Add(memberExpression.Member as PropertyInfo);
}
}
一切正常,直到我嘗試刪除一些相關對象(將根對象與兩個從屬對象聚合在一起,並標記為級聯刪除):我遇到了一個異常“由於一個或多個外鍵屬性,該關系無法更改是不可為空的”。 我在Google上搜索了一下,發現http://blog.abodit.com/2010/05/the-relationship-could-not-be-changed-because-因為-one-or-more-of-the-foreign-key-properties- is -non-nullable /我將GetHashCode更改為base.GetHashCode(),錯誤消失了。 但是現在它破壞了我的所有代碼:我無法為POCO對象覆蓋GetHashCode =>我無法覆蓋Equals =>我無法為POCO對象實現Value Object和Entity Object模式。 因此,我感謝任何解決方案,解決方法等。
如果要覆蓋GetHashCode,則必須直接解決問題。 問題說:
"The relationship could not be changed because one or more of the foreign-key properties is non-nullable"
所以,
1.查找用作外鍵的不可為空的字段並使它為可空(因此,當您刪除記錄時-fk可以為空)。
2.不要將依賴項標記為級聯刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.