例如,給定一個廠用的方法使用存儲庫時,類型是否可以返回存儲庫用於測試現有實體的Func?
public static T Save<T>(T item) where T : Base, new()
{
/* item.Id == Guid.Empty therefore item is new */
if (item.Id == Guid.Empty && repository.GetAll<T>(t => t.Name == item.Name))
{
throw new Exception("Name is not unique");
}
}
如何創建的Base
屬性(比如MustNotAlreadyExist
),這樣我可以改變上面
public static T Save<T>(T item) where T : Base, new()
{
/* item.Id == Guid.Empty therefore item is new */
if (item.Id == Guid.Empty && repository.GetAll<T>(t.MustNotAlreadyExist))
{
throw new Exception("Name is not unique");
}
}
public class Base
{
...
public virtual Expression<Func<T, bool>> MustNotAlreadyExist()
{
return (b => b.Name == name); /* <- this clearly doesn't work */
}
}
,然後方法怎麼可以在Account : Base
public class Account : Base
{
...
public override Expression<Func<T, bool>> MustNotAlreadyExist()
{
return (b => b.Name == name && b.AccountCode == accountCode); /* <- this doesn't work */
}
...
}
我認爲我可能有方法簽名,雖然我很苦惱它:public virtual Expression> MustNotAlreadyExists (T item)where T:Base {return(t => t。 Name == item.Name); } –
2011-01-12 15:49:39
請參閱下面我自己的答案;使用通用接口並在類型 – 2011-01-13 14:52:19