Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 具有泛型和对象数据源的存储库模式_C#_Entity Framework_C# 4.0_Entity Framework 4 - Fatal编程技术网

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

C# 具有泛型和对象数据源的存储库模式,c#,entity-framework,c#-4.0,entity-framework-4,C#,Entity Framework,C# 4.0,Entity Framework 4,我不熟悉实体框架,也不太熟悉C。我正在使用实体框架和存储库模式。我有一个DAL项目、一个业务层项目和一个web项目,其中包含aspx页面和ObjectDataSource。现在我已经为我的所有实体创建了单独的存储库,我想使用一个通用存储库来处理所有基本的CRUD函数。我可以在代码示例中为下面这样的所有实体创建泛型实体DAL类,并在泛型存储库中继承它,但是使用对象数据源,我该怎么做 1是否将ObjectDataSource的TypeName映射到泛型类型?分配TypeName和 ObjectDat

我不熟悉实体框架,也不太熟悉C。我正在使用实体框架和存储库模式。我有一个DAL项目、一个业务层项目和一个web项目,其中包含aspx页面和ObjectDataSource。现在我已经为我的所有实体创建了单独的存储库,我想使用一个通用存储库来处理所有基本的CRUD函数。我可以在代码示例中为下面这样的所有实体创建泛型实体DAL类,并在泛型存储库中继承它,但是使用对象数据源,我该怎么做

1是否将ObjectDataSource的TypeName映射到泛型类型?分配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;
    }

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

关于您的第一个问题,请参阅:或更类似ASP.NET的解决方案:

关于你的第二个问题:

是的,您可以按照导航属性引用相关实体,但有一个问题。如果通过在导航属性上导航、选择它们或使用include语句而不在输出中包含导航属性,则禁用延迟加载,因为导航属性未加载,因此将出现NullReferenceException


我的建议是在查询中强制包含导航属性,请参阅:

对于通常在codebehind中而不是在视图中进行数据绑定的存储库模式,请记住视图应该是哑的,并且仅显示数据简单逻辑是可以的,代码隐藏是大多数人将他们的逻辑放在视图中的地方,比如数据绑定之类的,而数据访问只是用于获取数据。