Using generic classes with ObjectDataSource

后端 未结 3 1473
走了就别回头了
走了就别回头了 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: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 _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.

提交回复
热议问题