我正在爲一個實體列表的存儲庫工作,我應該不止一次地重複同一個類,唯一的區別是類型類型..是否有一種方法使其通用?創建通用類C#
應該很容易,我肯定不知道如何使這個通用:
private Namespace.DAL.UserProfileRepository _rep = new Namespace.DAL.UserProfileRepository();
類我不停地重複它這樣的:
public class UserProfileRepository : IEntityRepository<IUserProfile>
{
private Namespace.DAL.UserProfileRepository _rep = new Namespace.DAL.UserProfileRepository();
public IUserProfile[] GetAll()
{
return _rep.GetAll();
}
public IUserProfile GetById(int id)
{
return _rep.GetById(id);
}
public IQueryable<IUserProfile> Query(Expression<Func<IUserProfile, bool>> filter)
{
return _rep.Query(filter);
}
}
請看看這個鏈接的信息,你的問題。 http://codereview.stackexchange.com/questions/19037/entity-framework-generic-repository-pattern – TYY
你想讓Namespace.DAL.UserProfileRepository是通用的嗎?然後你可以定義一個名爲Namespace.DAL.Repository的新類。創建泛型非常簡單http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx –
我只想指出,當您使用這些存儲庫時,應該使用它們的代碼應該使用接口IEntityRepository而不是直接使用該接口的實現。 –