2011-10-18 29 views
0

我是新来的实体框架和相对较新的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解释清楚,或者如果您有任何问题,谢谢。

+0

与你平时做绑定的代码隐藏而不是在视图中的存储库模式,记得认为应该是愚蠢的,只显示数据(简单的逻辑是好的),代码隐藏是大多数人将他们的逻辑视为数据绑定的视图,而不是数据访问仅用于获取数据。 – Joakim

回答

0

关于第一个问题,请参考:Using generic classes with ObjectDataSource或更多的ASP.NET喜欢的解决方案:http://www.manuelabadia.com/blog/PermaLink,guid,91a1d00f-197e-4148-b4e1-cea324029dc6.aspx

至于你的第二个问题:

是的,你可以参考相关实体按照导航属性,但有一个问题。如果您不在输出中包含导航属性(通过浏览它们,选择它们或使用Include语句)和延迟加载,则会禁用导航属性,因为导航属性未加载,您将收到NullReferenceException。

我的建议是强制在查询中包括导航性能,请参见:http://msdn.microsoft.com/en-us/library/bb738708.aspx