Share code between projects in a solution in Visual Studio 2008, when building a common assembly is impossible
I create an add-on for the product Foo. There are different versions of Foo, namely version 1, 2, 3 and 4.
These versions have a mostly compatible API, but not fully.
I currently have 5 projects:
- DotNetCommon - here are the common methods which could be used if I create an add-on or something other than the Foo product.
- FooOne
- FooTwo
- FooThree
- FooFour
The Foo*-projects contains the add-in for version 1-4 of Foo.
There are a lot of duplicated files in the Foo*-projects, as there are a lot of things in the API which are identical for all versions of Foo. It would be nice to separate out everything which is common for all Foo-versions.
Why not just create a common assembly for开发者_JAVA技巧 all versions of Foo called FooCommon?
If I would put all classes which are common for all versions of Foo into a new library project, I would still have to choose which version of Foo the new FooCommon should reference. As said, they are not identical.
Create an interface containing the common methods:
public interface IFoo
{
void CommonMethod1();
void CommonMethod2();
}
Create an abstract base class from IFoo:
public abstract class FooBase : IFoo
{
// Implement the common calls here
public void CommonMethod1()
{
// concrete shared code goes here
}
public void CommonMethod2()
{
// concrete shared code goes here
}
}
Create your one-off code from the FooBase:
public class FooOne : FooBase
{
// concrete code specific to FooOne goes here
}
public class FooTwo : FooBase
{
// concrete code specific to FooTwo goes here
}
精彩评论