我是新來的實體框架和相對較新的C#以及。我正在使用實體框架和存儲庫模式。我有一個DAL項目,一個業務層項目和一個web項目,其中包含aspx頁面和ObjectDataSource。現在我已經爲所有實體創建了獨立的Repositories,我想使用Generic Repository來處理所有基本的CRUD功能。我可以在代碼示例中爲下面的所有實體創建通用實體DAL類,並在通用知識庫中繼承該類,但是使用對象數據源。1)如何將ObjectDataSource的TypeName映射到泛型Type?分配TypeName和
ObjectDataTypeName?業務層泛型類繼承通用的IDALEntity類。存儲庫模式與泛型和對象數據源
<asp:ObjectDataSource ID="ODSCustomers" runat="server"
TypeName="SampleProject.BLL. " how do i access the Customer instance of BL
DataObjectTypeName="SampleProject.DAL. " how do i access the instance of
Customer entity from the generic DAL
class?
SelectMethod="GetCustomers" >
<SelectParameters>
<asp:SessionParameter Name="client_id" SessionField="ClientID" />
</SelectParameters>
2)如何被相關實體或導航性能的方式來處理?如果我想顯示來自多個實體的列,例如Customer實體和Customer.CustomerAddress實體,那麼我是否會像DataFied =「Customer.CustomerAddress.City」那樣綁定網格列?
public class DALEntityRepository<T> : IDisposable, IDALEntityRepository<T> where T : class
{
private PFOEntities _context;
private ObjectSet<T> _objectSet;
public DALEntityRepository()
{
_context = new Entities(ConnectionStringHelper.GetConnectionString());
_objectSet = (ObjectSet<T>)GetObjectSet();
}
public void Insert(T entity)
{
_context.AddObject(_objectSet.EntitySet.Name, entity);
_context.SaveChanges();
}
public void Update(T newVersion, T origVersion)
{
_objectSet.Attach(origVersion);
_context.ApplyCurrentValues(_objectSet.EntitySet.Name, newVersion);
_context.SaveChanges();
}
public void Delete(T entity)
{
_context.AttachTo(_objectSet.EntitySet.Name, entity);
_objectSet.DeleteObject(entity);
_context.SaveChanges();
}
public IQueryable<T> GetEntities()
{
return _objectSet;
}
public IQueryable<T> GetEntitiesByClientId(int clientId)
{
Expression<Func<T, bool>> predicate = (Expression<Func<T, bool>>)GetPredicate(clientId);
return GetEntities().Where(predicate);
}
private object GetPredicate(int clientId)
{
object retVal = null;
Type type = GetType();
//Use similar if's to check for Different Entities
if (type == typeof(DataEntityRepository<Customers>))
{
Expression<Func<Customers, bool>> predicate = (c) => c.client_id ==
clientId;
retVal = predicate;
}
return retVal;
}
private object GetObjectSet()
{
object retVal = null;
Type type = GetType();
if(type == typeof(DataEntityRepository<Customers>))
{
retVal = _context.Customers;
}
return retVal;
}
您的幫助表示感謝,請讓我知道,如果我havnt解釋清楚,或者如果您有任何問題,謝謝。
與你平時做綁定的代碼隱藏而不是在視圖中的存儲庫模式,記得認爲應該是愚蠢的,只顯示數據(簡單的邏輯是好的),代碼隱藏是大多數人將他們的邏輯視爲數據綁定的視圖,而不是數據訪問僅用於獲取數據。 – Joakim