Re: Separating implementation and interface: HOW?
- From: "Luc Kumps" <NOkumpsSPAM@xxxxxxxxxx>
- Date: Sun, 19 Nov 2006 22:03:30 +0100
William Stacey [C# MVP] wrote:
(IClassRegistration)Activator.CreateInstance(Type.GetType("Namespace.Object,I'll simply use a number of
IClassRegistration cr =
Assembly"))
That seems like an easy way to do it :-) Using this method, does
"Namespace.Object" need to be unique for each one? Check that. The
assembly name makes them unique even with same namespace - right?
Now there's an idea, William!
I simply use the same class name in each namespace to 'register' the factory
(or factories) and I use Reflection to find all the namespaces that have
this class!
Here's the code for the central 'factory repository', the main program
should only call FactoryDepository.Init():
//*******************************************
public interface IFactory
{
object getInstance(Dictionary<string, string> settings);
}
public interface IFactoryRegistry
{
void registerFactory(string name, IFactory factory);
}
public class FactoryDepository: IFactoryRegistry
{
static Dictionary<string, IFactory> _allFactories = new
Dictionary<string, IFactory>();
static public void Init()
{
FactoryDepository f = new FactoryDepository();
// Get a list of all our factories
System.Reflection.Assembly[] assems =
AppDomain.CurrentDomain.GetAssemblies();
foreach (System.Reflection.Assembly a in assems)
{
string[] parts = a.FullName.Split(new char[] { ',' });
if (parts.Length > 0)
{
Type tFact=null;
try
{
tFact = Type.GetType(parts[0] + ".NamespaceFactory, " +
parts[0]);
}
catch (Exception) {
}
if (tFact != null)
{
Activator.CreateInstance(tFact, new object[] { f });
}
}
}
}
public void registerFactory(string name, IFactory factory)
{
_allFactories[name] = factory;
}
}
//*******************************************
And here's the part in an "implementation namespace":
//*******************************************
namespace Nimplementation
{ public class NamespaceFactory : IFactory
{
public object getInstance(Dictionary<string, string> settings)
{
return new BlaBla(); //This is were we need to instantiate the correct
implementation, based upon the "settings"
}
public NamespaceFactory(IFactoryRegistry registry)
{
registry.registerFactory("interfacename", this);
}
}
}
//*******************************************
Whenever a factory is needed , you simply use
_allFactories["interfacename].getInstance(...).
Luc K
.
- Follow-Ups:
- Re: Separating implementation and interface: HOW?
- From: Dave Sexton
- Re: Separating implementation and interface: HOW?
- References:
- Separating implementation and interface: HOW?
- From: Luc Kumps
- Re: Separating implementation and interface: HOW?
- From: Andreas Mueller
- Re: Separating implementation and interface: HOW?
- From: Luc Kumps
- Re: Separating implementation and interface: HOW?
- From: William Stacey [C# MVP]
- Separating implementation and interface: HOW?
- Prev by Date: Re: What keeps track of delegates?
- Next by Date: Re: Member Variables Naming Convention
- Previous by thread: Re: Separating implementation and interface: HOW?
- Next by thread: Re: Separating implementation and interface: HOW?
- Index(es):
Relevant Pages
|