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

发布于 2024-12-10 14:27:01 字数 3058 浏览 0 评论 0原文

我对实体框架很陌生,对 C# 也比较陌生。我正在将实体框架与存储库模式一起使用。我有一个 DAL 项目、一个业务层项目和一个 Web 项目,其中包含 aspx 页面和 ObjectDataSource。现在我一直在为我的所有实体创建单独的存储库,我想使用通用存储库来处理所有基本的 CRUD 功能。我可以为代码示例中的所有实体创建通用实体 DAL 类,并在通用存储库中继承该类,但使用对象数据源时,我该如何

1) 将 ObjectDataSource 的 TypeName 映射到通用类型?分配类型名称和
对象数据类型名称?业务层泛型类继承泛型 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;
    }

感谢您的帮助,如果我没有解释清楚或者您有任何问题,请告诉我,谢谢。

I am new to Entity framework and relatively new to C# as well. I am using Entity Framework with the Repository Pattern. I have a DAL project, a business layer project and a web project which has the aspx pages along with the ObjectDataSource. Right now I have been creating seperate Repositories for all my entities, I would like to use a Generic Repository to handle all the basic CRUD functions. I can create the Generic entity DAL class for all entities like below in the code sample and inherit that in a generic Repository but with an object data Source how do I

1) map the ObjectDataSource's TypeName to a generic Type? assign the TypeName and
ObjectDataTypeName ? The Business Layer generic class inherits the generic IDALEntity class.

 <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) How are related entities or Navigation properties handled this way? If I want to display columns from multiple entities for e.g Customer entity and Customer.CustomerAddress Entity as well would I bind the grid columns like 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;
    }

Your helps appreciated please let me know if I havnt explained clearly or if you have any questions thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ˇ宁静的妩媚 2024-12-17 14:27:03

对于您的第一个问题,请参考:将泛型类与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

For your first question, please refer to: Using generic classes with ObjectDataSource or a more ASP.NET like solution: http://www.manuelabadia.com/blog/PermaLink,guid,91a1d00f-197e-4148-b4e1-cea324029dc6.aspx

As for your second question:

Yes, you can refer to related entities by following the navigation properties but there is a catch. If you dont include the navigation properties in the output (by either navigating over them, selecting them or using the Include statement) and lazy loading is disabled, you will get a NullReferenceException since the navigation properties are not loaded.

My recommendation is to forcefully Include the navigation properties in your query, see: http://msdn.microsoft.com/en-us/library/bb738708.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文