L
Luc Kumps
(Sorry about the previous post, it got transmitted before it was complete)
We try to separate implementation and interface defintions, but we run into
a problem. I hope the guru's can solve this, as we seem to lack only a
single 'step' to have "full separation"...
We have a first project, namespace Ninterface, that contains the interface
definitions in class1_interface.cs, like this:
namespace Ninterface {
public interface IClass1{
void method1();
}
}
In a second project, namespace Nimplementation, we have an implementation of
class1 in class1.cs:
namespace Nimplementation {
public class Class1: IClass1 {
void method1() {
/** implementation of Class1.method1 **/
}
}
In any project using Class1, we include the first project in the references
and we add a "Using Ninterface". All our instance references use "IClass1",
as "Class1" is completely invisible here.
But here's the problem: how do we create a new instance of a Class1 object,
without referring to the implementation in Nimplementation? We don't want do
refer to Nimplementation, only to Ninterface. So we can't use "new
Class1();". Moreover, we want to be able to have multiple parallel
implementations of Class1, being able to construct a specific one at
runtime.
Of course, we could make a third project that contains class factories able
to generate instances of the different implementations. But then this
project would need to have the 'implementation' projects in the references
and we want to exclude any 'reference' between 'class user' and 'class
implementation'.
We would like to have all the implementations 'register' their factory in a
'central 'factory'. In C++, this is easy to accomplish: define static
instances of a factory in each file, and in the instance constructor call
the central static "RegisterClass" method. But C# doesn't initialize a
static field of a class (and therefore doesn't call the constructor) unless
you USE the class for the first time. But that's exactly what we're trying
to prevent: "using" the implementation directly!
Is there any way to execute code within a number of classes "automatically"
when the program starts up, without needing to USE all these classes? Just
like the constructors of all static objects in C++ are being called (in
random order, but we don't care about order) upon program startup? Or should
we stop dreaming because this is impossible?
Thanks in advance,
Luc Kumps
We try to separate implementation and interface defintions, but we run into
a problem. I hope the guru's can solve this, as we seem to lack only a
single 'step' to have "full separation"...
We have a first project, namespace Ninterface, that contains the interface
definitions in class1_interface.cs, like this:
namespace Ninterface {
public interface IClass1{
void method1();
}
}
In a second project, namespace Nimplementation, we have an implementation of
class1 in class1.cs:
namespace Nimplementation {
public class Class1: IClass1 {
void method1() {
/** implementation of Class1.method1 **/
}
}
In any project using Class1, we include the first project in the references
and we add a "Using Ninterface". All our instance references use "IClass1",
as "Class1" is completely invisible here.
But here's the problem: how do we create a new instance of a Class1 object,
without referring to the implementation in Nimplementation? We don't want do
refer to Nimplementation, only to Ninterface. So we can't use "new
Class1();". Moreover, we want to be able to have multiple parallel
implementations of Class1, being able to construct a specific one at
runtime.
Of course, we could make a third project that contains class factories able
to generate instances of the different implementations. But then this
project would need to have the 'implementation' projects in the references
and we want to exclude any 'reference' between 'class user' and 'class
implementation'.
We would like to have all the implementations 'register' their factory in a
'central 'factory'. In C++, this is easy to accomplish: define static
instances of a factory in each file, and in the instance constructor call
the central static "RegisterClass" method. But C# doesn't initialize a
static field of a class (and therefore doesn't call the constructor) unless
you USE the class for the first time. But that's exactly what we're trying
to prevent: "using" the implementation directly!
Is there any way to execute code within a number of classes "automatically"
when the program starts up, without needing to USE all these classes? Just
like the constructors of all static objects in C++ are being called (in
random order, but we don't care about order) upon program startup? Or should
we stop dreaming because this is impossible?
Thanks in advance,
Luc Kumps