【发布时间】:2011-05-26 19:43:56
【问题描述】:
我有存储库来管理 ComplaintTypes 上的操作,它有大量关联实体,所以我定义。不想加载所有内容,因此我有LazyLoadingEnabled = true;。但是我确实想加载一个关联实体,例如:ComplaintSubType
这对我有用,但我认为有更好的方法吗?谢谢!
namespace Complaint.Dal.Repositories
{
public class ComplaintTypeRepository : RepositoryBase<ComplaintType>, IComplaintTypeRepository
{
#region ctor
/// <summary>
/// ctor
/// </summary>
/// <param name="objectContext"></param>
public ComplaintTypeRepository(IObjectContext objectContext)
: base(objectContext)
{
//Lazy Load so we don't get bloated data
objectContext.LazyLoadingEnabled = true;
}
#endregion
#region Implementation of IComplaintRepository
public IEnumerable<ComplaintType> GetAllComplaintTypes()
{
//Load the related SubTypes
var result = GetAll(t => t.PK_Type_Id);
foreach (var complaintType in result)
{
complaintType.ComplaintSubType.Load();
}
return result;
}
public ComplaintType GetComplaintType(int typeId)
{
var result = GetSingle(t => t.PK_Type_Id == typeId);
result.ComplaintSubType.Load();
return result;
}
public void UpdateComplaintType(ComplaintType entity)
{
Attach(entity);
}
#endregion
}
}
更新:
public IEnumerable<T> GetAll<TKey, TType>(Expression<Func<T, TKey>> orderBy)
{
var ret = ObjectSet;
//set Orderby
ret.OrderBy(orderBy);
return ret.ToList();
}
【问题讨论】:
标签: asp.net entity-framework repository-pattern n-tier-architecture