當我創建我的存儲庫時,我認爲每個EntityObject都有ID屬性。然後,我創建庫:
public class Repository<T> : IRepository<T> where T : EntityObject, IBasicEntityInfo
IBasicEntityInfo:
public interface IBasicEntityInfo
{
int ID { get; set; }
}
然後
OrderBy(a => a.ID)
自動生成EntityObject類沒有實現IBasicEntityInfo接口,但我用T4生成:
public partial class User : IBasicEntityInfo ,ILastModificationInfo
public partial class Project : IBasicEntityInfo ,ILastModificationInfo
public partial class Group : IBasicEntityInfo ,ILastModificationInfo
public partial class GroupUser : IBasicEntityInfo
public partial class OperationSystem : IBasicEntityInfo ,ILastModificationInfo
T4很簡單:
<#@ template language="C#" #>
<#@ output extension="cs" #>
<#@ import namespace="System.Collections.Generic" #>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Web;
<#
String[] classNames = new String[] {"User","Project","Group","GroupUser","OperationSystem","TaskType","Priority","Severity","Status","Version","Platform","Task","TaskUser","Attachment","Comment","Setting"};
#>
namespace CamelTrap.Models
{
<# foreach(String className in classNames) { #>
public partial class <#= className #> : IBasicEntityInfo
{
}
<# } #>
}
進行這樣的假設解決其他問題也是一樣,從創建複雜的表達式救了我。
這在很多方面都很有用(我們幾乎完成同樣的事情),但實際上並沒有幫助解決他遇到的問題。 – 2009-11-09 13:36:01
它確實解決了這個問題,因爲你可以使用OrderBy(a => a.ID)。我的存儲庫通過實現IBasicEntityInfo知道每個對象都具有ID屬性。 – LukLed 2009-11-09 14:36:27
當然,你的解決方案稍微簡單:) – LukLed 2009-11-09 14:37:31