开发者

Removing copy/pasted code without an interface

开发者 https://www.devze.com 2023-02-03 18:47 出处:网络
I have two data access objects that are reverse generated and jar\'ed up for use by my application. They represent tables that are very similar. One table has a few additional columns than the other.

I have two data access objects that are reverse generated and jar'ed up for use by my application. They represent tables that are very similar. One table has a few additional columns than the other. This is out of my control due to business oriented database ownership concerns.

The application currently has two implementations of a repository that operates on these DAOs. The implementations are very similar. One has a few extra operations that correspond to the extra columns on the second DAO. However with only a few exceptions, one implementation is a copy and paste of the other. The implementations are hundreds of lines long.

So I wanted to remove the copy/paste job. Ideally I could just stick an interface in front of the DAOs, and then maybe use an abstract class to hold the shared code (nearly all of it). However, I c开发者_开发知识库annot put an interface in front of the DAOs. Remember they are reverse generated, and without upgrading our ORM software I don't think this is a reasonable choice (Kodo 3.x I believe, changing this is not in scope).

The only thing I can think of that would even work is some nastiness with reflection but that results in something much worse than I have now.

Any clever solutions?

edit: Here is very watered down code example

package one.dao
//reverse generated
class UserDao {
    getFirstName(..);
    setFirstName(..);
    getLastName(..);
    .... 50 more just like this
}

package two.dao
//reverse generated
class UserDao {
    getFirstName(..);
    setFirstName(..);
    getLastName(..);
    .... the same 50 more as above

    getSomethingElse(..); //doesn't exist in one.dao.UserDao
    setSomethingElse(..); //doesn't exist in one.dao.UserDao
}

class RepositoryOne(one.dao.UserDao userDao) {
    //insert code here. perform operations on nearly all methods, lots of code
}

class RepositoryTwo(two.dao.UserDao userDao) {  
    //insert code here. same as Repository one

    //some extra code that isn't above, maybe 10 lines
}


I am assuming you have some control over the duplicated code. If your code generator is producing all of it, you'll need to search for solutions within its API & configuration, I suspect.

When Inheritance doesn't work, try Composition. Make a third class to hold the shared code (SharedCode). Give each of the two existing classes a private member instance of the SharedCode class and make all routines implemented in SharedCode pass through methods to the member instance.

0

精彩评论

暂无评论...
验证码 换一张
取 消