I asked this question several weeks ago and received some good answers: ASP.NET Class Library best practice. I now have another question.
The problem I have is that I have inherited an ASP.NET application, which contains lots of classes with tight coupling and low cohesion, which is not ideal. I want to share some of the code with other apps. Most of the code exists in one class, however the class references other classes and those classes reference other classes etc. Is there any way of sharing the code in one class (which references other classes)? The only way I can think of doing this is using web services, but there is sensitive information.
The only good option, in cases like this, is refactoring the code. You don’t have to change the existing class interface, however. You can create multiple new classes that are designed properly and replace the logic in the original poorly designed class. Then you can refactor the original class to use the new classes internally to perform the functionality. You don’t have to do this all at once. As you find that you need a particular bit of logic in a shared library, just refactor that logic and leave the rest untouched. Over time you can, in this way, refactor the whole thing. Unless, of course, it’s not that big or you have all the time in the world to refactor the beast. However, usually that’s not the case.
For instance, let’s say you have the following overly simplified classes:
And you want to share the logic in
OriginalBeast.Method2in a class library, you would need to move theDependencyclass to the class library (and likely need to partially refactor it as well). Then you would need to create a new class that contains just the desired methods from the original beast:Then, you would need to refactor the original beast to use the class library instead of doing the logic itself:
Obviously real-world beasts are never that simple and it will likely require a lot of work to refactor even a small part of it, but hopefully that gives you an idea of what I’m talking about.