Using generic classes with ObjectDataSource

后端 未结 3 1458
走了就别回头了
走了就别回头了 2021-01-02 10:30

I have a generic Repository class I want to use with an ObjectDataSource. Repository lives in a separate project called DataAccess. According to this post

相关标签:
3条回答
  • 2021-01-02 10:31

    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

    0 讨论(0)
  • 2021-01-02 10:50

    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
    
    0 讨论(0)
  • 2021-01-02 10:53

    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.

    0 讨论(0)
提交回复
热议问题