Using generic classes with ObjectDataSource
Asked Answered
L

3

9

I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T> lives in a separate project called DataAccess. According to this post from the MS newsgroups (relevant part copied below):

Internally, the ObjectDataSource is calling Type.GetType(string) to get the type, so we need to follow the guideline documented in Type.GetType on how to get type using generics. You can refer to MSDN Library on Type.GetType:

http://msdn2.microsoft.com/en-us/library/w3f99sx1.aspx

From the document, you will learn that you need to use backtick (`) to denotes the type name which is using generics.

Also, here we must specify the assembly name in the type name string.

So, for your question, the answer is to use type name like follows:

TypeName="TestObjectDataSourceAssembly.MyDataHandler`1[System.String],TestObjectDataSourceAssembly"

Okay, makes sense. When I try it, however, the page throws an exception:

<asp:ObjectDataSource ID="MyDataSource" TypeName="MyProject.Repository`1[MyProject.MessageCategory],DataAccess" />

[InvalidOperationException: The type specified in the TypeName property of ObjectDataSource 'MyDataSource' could not be found.]

The curious thing is that this only happens when I'm viewing the page. When I open the "Configure Data Source" dialog from the VS2008 designer, it properly shows me the methods on my generic Repository class. Passing the TypeName string to Type.GetType() while debugging also returns a valid type. So what gives?

Lobbyism answered 9/9, 2008 at 21:43 Comment(0)
H
13

Do something like this.

Type type = typeof(Repository<MessageCategory);
string assemblyQualifiedName = type.AssemblyQualifiedName;

get the value of assemblyQualifiedName and paste it into the TypeName field. Note that Type.GetType(string), the value passed in must be

The assembly-qualified name of the type to get. See AssemblyQualifiedName. If the type is in the currently executing assembly or in Mscorlib.dll, it is sufficient to supply the type name qualified by its namespace.

So, it may work by passing in that string in your code, because that class is in the currently executing assembly (where you are calling it), where as the ObjectDataSource is not.

Most likely the type you are looking for is

MyProject.Repository`1[MyProject.MessageCategory, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null], DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null
Harmonist answered 9/9, 2008 at 23:8 Comment(1)
<% Response.Write(typeof(YourClassName).AssemblyQualifiedName); %>Kalahari
F
1

I know this is an old post but I have recently had this problem myself. Another solution would be to replace the inheritance with object composition, e.g.

[DataObject]
public class DataAccessObject {
    private Repository<MessageCategory> _repository;

    // ctor omitted for clarity
    // ...

    [DataObjectMethod(DataObjectMethodType.Select)]
    public MessageCategory Get(int key) {
        return _repository.Get(key);
    }
}

This way the ObjectDataSource doesn't know about the repository because its hidden within the class. I have a class library in my facade layer that is a perfectly reasonable place to put this code in the project I am working on.

In addition, if you are using Resharper and interfaces, its possible to get Resharper to do the refactoring using Resharpers "Implement using field" function.

Fischer answered 21/10, 2014 at 16:58 Comment(0)
G
0

Darren,

Many, many thanks for your post. I've been fighting with this all day. Strangely, in my case, I need to double the square brackets, e.g. for your piece of code:

MyProject.Repository`1[[MyProject.MessageCategory, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null]], DataAccess, Version=1.0.0.0, Culture=neutral, PublicKey=null

Roger

Grunter answered 30/9, 2009 at 17:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.