Re: Separating implementation and interface: HOW?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



William Stacey [C# MVP] wrote:
I'll simply use a number of
IClassRegistration cr =


(IClassRegistration)Activator.CreateInstance(Type.GetType("Namespace.Object,
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




.



Relevant Pages

  • Re: Separating implementation and interface: HOW?
    ... Are you expecting to register any assemblies through configuration after deployment or will ... I simply use the same class name in each namespace to 'register' the factory ... public interface IFactory ...
    (microsoft.public.dotnet.languages.csharp)
  • Serialization & Deserialization
    ... When an instance of Factory class gets serialized ... xsi:type for doesn't include namespace. ... Widget may come from different namespaces. ... public class WidgetX: Widget ...
    (microsoft.public.dotnet.xml)
  • RE: Self populating collection
    ... Create an abstract factory that fills collections and then a concrete ... > So far I have a design along the following lines. ... > public interface IItem ... > public class SomeItemClass:IItem ...
    (microsoft.public.dotnet.framework)
  • Re: PHP to a COM object
    ... This "factory" object is an instance of a class called Factory. ... doc also makes mention of something called an interface (IFactory) who has ... }When I do it on the interface, I get all my object's properties. ... myFactory is an interface that has no working parts. ...
    (comp.lang.php)
  • Re: PHP to a COM object
    ... get the Factory functionality...probably not. ... that *implement* that interface will provide a caller, ... IFactory will provide you no other functionality than that. ... myFactory is an interface that has no working parts. ...
    (comp.lang.php)