Tuesday, October 13, 2009

Sharing an Interface (What not to do)

Have you ever had a class library that uses an Interface?  How about two or more libraries that need to use the same Interface?  For instance, ClassLibrary1 and ClassLibrary2 need to implement ISomeInterface below:

Interface1

Instead of copying ISomeInterface.vb to both ClassLibrary projects or adding a project reference, you can add a link to ISomeInterface.vb and have access to ISomeInterface:

Interface2

Interface3

Now you’ll notice that ISomeInterface.vb is part of ClassLibrary1 as a linked file:

Interface3

Now we can implement ISomeInterface in ClassLibrary1:

Interface4

The benefit of adding the Interface file as a link is that you can maintain the Interface in one location.  You don’t have to worry about having to copy new versions if you decide to change your contract.

No comments: