P
Peter Gummer
This is a design question.
I have a project containing a dozen or so classes with protected
internal constructors.
My reason for having protected internal constructors is to prevent
classes in other assemblies from instantiating them. There is a factory
class that instantiates them. Only the factory should do so, because
some of these classes have subclasses that will be created instead. The
decision of which class to instantiate belongs to the factory.
I've got this working just fine, except one detail.
Because the constructors are protected internal, it's possible for
classes inside the project to by-pass the factory. It's not too hard to
ensure this doesn't happen, by checking manually within the project;
but it would be easier and safer if the compiler could catch this
mistake.
Ideally, I'd like the constructors to have a more restricted visibility
than "internal". I'd like to specify that only the factory can see
them. I.e., instead of this:
protected internal Foo() {}
I'd like to be be able to write:
protected visibleto(Factory) Foo() {}
Unfortunately, C# has extremely coarse-grained visiblity modifiers.
I can think of a couple of work-arounds:
Work-around 1. Declare the constructors protected and let the factory
use reflection to instantiate them.
Work-around 2. Declare a nested protected class within the factory for
each of the classes it instantiates. The nested classes would have
public constructors. Then only the factory, or its descendants, could
instantiate the classes. Exactly what I want!
Both solutions would be worse than the original problem. Reflection
(Work-around 1) kills type-safety, so I avoid it unless I absolutely
need it; besides, it's slow. Work-around 2, on the other hand, would be
a bad case of code-bloat.
Is there a decent solution to this in C#?
-- Peter Gummer
I have a project containing a dozen or so classes with protected
internal constructors.
My reason for having protected internal constructors is to prevent
classes in other assemblies from instantiating them. There is a factory
class that instantiates them. Only the factory should do so, because
some of these classes have subclasses that will be created instead. The
decision of which class to instantiate belongs to the factory.
I've got this working just fine, except one detail.
Because the constructors are protected internal, it's possible for
classes inside the project to by-pass the factory. It's not too hard to
ensure this doesn't happen, by checking manually within the project;
but it would be easier and safer if the compiler could catch this
mistake.
Ideally, I'd like the constructors to have a more restricted visibility
than "internal". I'd like to specify that only the factory can see
them. I.e., instead of this:
protected internal Foo() {}
I'd like to be be able to write:
protected visibleto(Factory) Foo() {}
Unfortunately, C# has extremely coarse-grained visiblity modifiers.
I can think of a couple of work-arounds:
Work-around 1. Declare the constructors protected and let the factory
use reflection to instantiate them.
Work-around 2. Declare a nested protected class within the factory for
each of the classes it instantiates. The nested classes would have
public constructors. Then only the factory, or its descendants, could
instantiate the classes. Exactly what I want!
Both solutions would be worse than the original problem. Reflection
(Work-around 1) kills type-safety, so I avoid it unless I absolutely
need it; besides, it's slow. Work-around 2, on the other hand, would be
a bad case of code-bloat.
Is there a decent solution to this in C#?
-- Peter Gummer