Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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# ObjectDataSource泛型无参数选择方法_C#_Asp.net_Generics_Objectdatasource - Fatal编程技术网

C# ObjectDataSource泛型无参数选择方法

C# ObjectDataSource泛型无参数选择方法,c#,asp.net,generics,objectdatasource,C#,Asp.net,Generics,Objectdatasource,我知道在这个话题上有很多问题。然而,我真的很困惑,为什么我会在这种情况下得到这个例外 例外情况 ObjectDataSource 'investmentDataSource' could not find a non-generic method 'GetAll' that has no parameters 案例 环境-.NET3.5、asp、SharePoint 2010(这无关紧要) 存储库对象继承自通用存储库对象,如下所示: public class InvestmentReposit

我知道在这个话题上有很多问题。然而,我真的很困惑,为什么我会在这种情况下得到这个例外

例外情况

ObjectDataSource 'investmentDataSource' could not find a non-generic method 'GetAll' that has no parameters
案例 环境-.NET3.5、asp、SharePoint 2010(这无关紧要)

存储库对象继承自通用存储库对象,如下所示:

public class InvestmentRepository : Repository<Investment, EntityContext>, IInvestmentRepository
{
    // ...
}
public abstract class Repository<TEntity, TContext> : IRepository<TEntity>
        where TEntity : EntityObject, new()
        where TContext : ObjectContext, new()
{
     public IQueryable<TEntity> GetAll()
     {
         // returns all entities from context
     }
}
public IQueryable<Investment> GetAll()
<asp:ObjectDataSource
    ID="investmentDataSource"
    runat="server" 
    OnObjectCreating="DataSourceObjectCreating" 
    TypeName="Model.InvestmentRepository"
    SelectMethod="GetAll">
</asp:ObjectDataSource>
this.investmentDataSource.TypeName = this.GetType().AssemblyQualifiedName;
公共类投资存储库:存储库,IIInvestmentRepository
{
// ...
}
通用存储库如下所示:

public class InvestmentRepository : Repository<Investment, EntityContext>, IInvestmentRepository
{
    // ...
}
public abstract class Repository<TEntity, TContext> : IRepository<TEntity>
        where TEntity : EntityObject, new()
        where TContext : ObjectContext, new()
{
     public IQueryable<TEntity> GetAll()
     {
         // returns all entities from context
     }
}
public IQueryable<Investment> GetAll()
<asp:ObjectDataSource
    ID="investmentDataSource"
    runat="server" 
    OnObjectCreating="DataSourceObjectCreating" 
    TypeName="Model.InvestmentRepository"
    SelectMethod="GetAll">
</asp:ObjectDataSource>
this.investmentDataSource.TypeName = this.GetType().AssemblyQualifiedName;
公共抽象类存储库:IRepository
其中tenty:EntityObject,new()
其中TContext:ObjectContext,new()
{
公共IQueryable GetAll()
{
//从上下文返回所有实体
}
}
我假设在编译过程中,在InvestmentRepository上创建了一个如下所示的方法:

public class InvestmentRepository : Repository<Investment, EntityContext>, IInvestmentRepository
{
    // ...
}
public abstract class Repository<TEntity, TContext> : IRepository<TEntity>
        where TEntity : EntityObject, new()
        where TContext : ObjectContext, new()
{
     public IQueryable<TEntity> GetAll()
     {
         // returns all entities from context
     }
}
public IQueryable<Investment> GetAll()
<asp:ObjectDataSource
    ID="investmentDataSource"
    runat="server" 
    OnObjectCreating="DataSourceObjectCreating" 
    TypeName="Model.InvestmentRepository"
    SelectMethod="GetAll">
</asp:ObjectDataSource>
this.investmentDataSource.TypeName = this.GetType().AssemblyQualifiedName;
public IQueryable GetAll()
因此,我假设当我创建如下ObjectDataSource时:

public class InvestmentRepository : Repository<Investment, EntityContext>, IInvestmentRepository
{
    // ...
}
public abstract class Repository<TEntity, TContext> : IRepository<TEntity>
        where TEntity : EntityObject, new()
        where TContext : ObjectContext, new()
{
     public IQueryable<TEntity> GetAll()
     {
         // returns all entities from context
     }
}
public IQueryable<Investment> GetAll()
<asp:ObjectDataSource
    ID="investmentDataSource"
    runat="server" 
    OnObjectCreating="DataSourceObjectCreating" 
    TypeName="Model.InvestmentRepository"
    SelectMethod="GetAll">
</asp:ObjectDataSource>
this.investmentDataSource.TypeName = this.GetType().AssemblyQualifiedName;


。。。它应该会起作用。但是,它不起作用。我在寻找任何建议。谢谢

建议在存储库和ObjectDataSource之间放置另一个辅助适配器类


使用这种适配器控制所有可能的参数组合要比尝试从正方形中画一个圆容易得多(阅读:使您的通用存储库直接作为对象数据源的数据提供程序工作)。

因此,我在用户控件上创建了一个方法,该方法应作为适配器工作:

public System.Collections.IEnumerable GetInvestments()
{
    return this.InvestmentRepository.GetAll();
}
并在用户控件的Page_Load方法中设置ObjectDataSource的TypeName,如下所示:

public class InvestmentRepository : Repository<Investment, EntityContext>, IInvestmentRepository
{
    // ...
}
public abstract class Repository<TEntity, TContext> : IRepository<TEntity>
        where TEntity : EntityObject, new()
        where TContext : ObjectContext, new()
{
     public IQueryable<TEntity> GetAll()
     {
         // returns all entities from context
     }
}
public IQueryable<Investment> GetAll()
<asp:ObjectDataSource
    ID="investmentDataSource"
    runat="server" 
    OnObjectCreating="DataSourceObjectCreating" 
    TypeName="Model.InvestmentRepository"
    SelectMethod="GetAll">
</asp:ObjectDataSource>
this.investmentDataSource.TypeName = this.GetType().AssemblyQualifiedName;

它似乎起作用了。然而,我最近发现,当您设置DataObjectTypeName时,ObjectDataSource的InsertMethod出现了错误,因此ObjectDataSource的可用性是相当值得怀疑的。

一天结束时,我也有同样的想法,默默地希望有一个更简单的解决方案。为了让我的生活稍微简单一点,我将把这个方法放在用户控件或存储库中。第一种可能性更大。