How to package the Factory into separate component assembly?
-
There is some
Interface
e.g. database and many implementationImp1, Imp2, Imp3, ...
e.g. sql database, xml database, in memory database. Then there is aFactory
to create those implementations returned asInterface
objects. How to assign them to different assemblies?Interface
and implementations should be in different assemblies to allow shipping to some clients product with sql database implementation, otheres with xml database etc... But what assembly to putFactory
? If it is put withInterface
or any other separate assembly it will need to ship all the implementations assemblies with it. I do not want to vialote DIP to allocate implementations directly in the code.Чесноков
-
There is some
Interface
e.g. database and many implementationImp1, Imp2, Imp3, ...
e.g. sql database, xml database, in memory database. Then there is aFactory
to create those implementations returned asInterface
objects. How to assign them to different assemblies?Interface
and implementations should be in different assemblies to allow shipping to some clients product with sql database implementation, otheres with xml database etc... But what assembly to putFactory
? If it is put withInterface
or any other separate assembly it will need to ship all the implementations assemblies with it. I do not want to vialote DIP to allocate implementations directly in the code.Чесноков
Although it's not stated directly, I assume that your question is about .NET (the word 'assembly' points in this direction). We solved this problem by creating an extra level of methods around the instantiation points of implementation, like this:
class DbFactory {
public static InterfaceDb Make(string kind) {
switch (kind) {
case "xml": return MakeXmlDb();
case "sql": return MakeSqlDb();
}
throw new ArgumentException("kind");
}
private static InterfaceDb MakeXmlDb() {
return new XmlDb();
}
private static InterfaceDb MakeSqlDb() {
return new SqlDb();
}
}This code does not throw an exception when the assembly with SqlDb is missing unless you call Make("sql"); calling Make("xml") runs fine as long as the assembly with the implementation is present. Note: this functionally equivalent code does not work the same way:
class DbFactory {
public static InterfaceDb Make(string kind) {
switch (kind) {
case "xml": return new XmlDb();
case "sql": return new SqlDb();
}
throw new ArgumentException("kind");
}
}This code requires both assemblies to be present.