1

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.

Community
  • 1
  • 1
w0051977
  • 15,099
  • 32
  • 152
  • 329

1 Answers1

2

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:

Public Class OriginalBeast
    Private _dependency As New Dependency()

    Public Function Method1() As Integer
        Return _dependency.Calculate(2)
    End Sub

    Public Function Method2() As Integer
        Return _dependency.Calculate(2)
    End Sub

    ' ...

    Public Function Method1027() As Integer
        Return _dependency.Calculate(1027)
    End Sub
End Class

Public Class Dependency
    Public Function Calculate(value As Integer) As Integer
        Return value * 2
    End Function
End Class

And you want to share the logic in OriginalBeast.Method2 in a class library, you would need to move the Dependency class 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:

Public Interface INice
    Function Method2() As Integer
End Interface

Public Class Nice
    Implements INice

    Public Sub New(dependency As IDependency)
        _dependency = dependency
    End Sub

    Private _dependency As IDependency

    Public Function Method2() As Integer Implements INice.Method2
        Return _dependency.Calculate(2)
    End Function
End Class

Public Interface IDependency
    Function Calculate(value As Integer) As Integer
End Interface

Public Class Dependency
    Implements IDependency

    Public Function Calculate(value As Integer) As Integer Implements IDependency.Calculate
        Return value * 2
    End Function
End Class

Then, you would need to refactor the original beast to use the class library instead of doing the logic itself:

Public Class OriginalBeast
    Public Sub New()
        _dependency = New Dependency()
        _nice = New Nice(_dependency)
    End Sub

    Private _dependency As IDependency
    Private _nice As INice

    Public Function Method1() As Integer
        Return _dependency.Calculate(2)
    End Sub

    Public Function Method2() As Integer
        Return _nice.Method2()
    End Sub

    ' ...

    Public Function Method1027() As Integer
        Return _dependency.Calculate(1027)
    End Sub
End Class

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.

Steven Doggart
  • 43,358
  • 8
  • 68
  • 105
  • Thanks, if you have a function in a class (poorly designed) and you want to share this in a class library then surely you would have to refactor the whole class? Are you sugesing dulcain th code? – w0051977 Jul 18 '12 at 14:25
  • @w0051977 I updated my answer to give an example of what I'm talking about. – Steven Doggart Jul 18 '12 at 14:54
  • thanks. What do you do about naming conventions between projects. For example if you had part of a Person class in a web application (app code folder) and part of a class, which is shared in a DLL. Is there a way to use partial classes in this scenario? – w0051977 Jul 18 '12 at 19:30
  • No, I don't believe you can use partial classes across multiple projects. You can name the class the same thing in the shared library, if you want to, as long as it's in another namespace. I was thinking that if the original class is large and unwieldy, that you would want to break it up into more granular classes with more specific names, anyway. – Steven Doggart Jul 18 '12 at 20:19
  • Yes, it does make more sense to make the class names more specific. What about relationships between the classes? For example, if you had a Person class in the ASP.NET project (app code) and you were sharing the undergraduate class between multiple projects, then I suppose you cannot extend the Student class. – w0051977 Jul 18 '12 at 20:24
  • If you create a derived class in a class library, then its base class must be in a class library as well. – Steven Doggart Jul 18 '12 at 20:49