具有泛型和对象数据源的存储库模式

时间:2011-10-18 02:29:34

标签: c# entity-framework c#-4.0 entity-framework-4

我是Entity框架的新手,也是C#的新手。我正在使用Entity Framework和Repository Pattern。我有一个DAL项目,一个业务层项目和一个web项目,它有aspx页面和ObjectDataSource。现在我一直在为我的所有实体创建单独的存储库,我想使用Generic Repository来处理所有基本的CRUD函数。我可以为代码示例中的所有实体创建通用实体DAL类,并在通用存储库中继承它,但是使用对象数据源我如何

1)将ObjectDataSource的TypeName映射到泛型Type?分配TypeName和
   ObjectDataTypeName? Business Layer泛型类继承了通用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;
    }

如果我有明确的解释或者您有任何疑问,请告诉我。

1 个答案:

答案 0 :(得分: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