ObjectDataSource generic parameterless select method

631 views Asked by At

I know there has been many questions on this topic. However, I am really confused why I get this exception in this case.

The exception

ObjectDataSource 'investmentDataSource' could not find a non-generic method 'GetAll' that has no parameters

The case The environment - .net 3.5, asp, SharePoint 2010 (this does not matter)

The repository object inherits from a generic repository object and it looks like this:

public class InvestmentRepository : Repository<Investment, EntityContext>, IInvestmentRepository
{
    // ...
}

The generic repository looks like this:

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
     }
}

I suppose that during the compilation a method on the InvestmentRepository is created which looks like this:

public IQueryable<Investment> GetAll()

So I suppose that when I create an ObjectDataSource like this:

<asp:ObjectDataSource
    ID="investmentDataSource"
    runat="server" 
    OnObjectCreating="DataSourceObjectCreating" 
    TypeName="Model.InvestmentRepository"
    SelectMethod="GetAll">
</asp:ObjectDataSource>

... it should work. However, it does not work. I am looking for any suggestions. Thanks

2

There are 2 answers

1
Wiktor Zychla On BEST ANSWER

The suggestion would be to put another auxiliary adapter class between your repository and your ObjectDataSource.

It will be much easier to controll all possible parameter combinations with such adapter than trying to make a circle from a square (read: making your generic repository working directly as the object data source's data provider).

0
Santhos On

So I created a method on the user control that should work as an adapter:

public System.Collections.IEnumerable GetInvestments()
{
    return this.InvestmentRepository.GetAll();
}

And set the TypeName of the ObjectDataSource in the Page_Load method of the user control like this:

this.investmentDataSource.TypeName = this.GetType().AssemblyQualifiedName;

It seems to be working. However, I have recently found out that the InsertMethod of ObjectDataSource is bugged when you have set DataObjectTypeName, so the usability of ObjectDataSource is quite questionable.