I want to control the creation of a bunch of classes that all share a common interface and all need a bit of logic in the construction. Also, I don't want any other code than the class factory to be able to create objects from these classes.
My main stumbling blocks are:
(1) for the generic method to be able to create instances of the classes I need the new() constraint which means I must have a public constructor on the classes which means they can be created publicly.
(2) An alternative would be for the classes themselves to have a static method which returns an instance of the class. But I can't call that from my generic class because I need to be dealing in terms of interfaces/types and you can't have statics via interfaces.
Here's the kind of thing I've currently got, but it's using the new() constraint which is allowing my classes to be created publicly:
internal static class MyClassFactory
{
internal static T Create<T>(string args) where T : IMyType, new()
{
IMyType newThing = new T();
newThing.Initialise(string args);
return (T)newThing;
}
}
public interface IMyType
{
void Initialise(string args);
}
public class ThingA: IMyType
{
public void Initialise(string args)
{
// do something with args
}
}
Any help greatly appreciated :)
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
It looks like you are trying to roll your own service locator.
Have you considered taking the dependency injection (DI) approach? as there are reasons why you may want to avoid a service locator.
I highly recommend you take a look at some of the popular IOC containers that can perform the kind of functionality you are trying to build. Looking back, i am very glad i chose DI over service locator.
-Ninject
-Autofac
-Unity
There is something you can do, but it's really ugly...
public class ThingA: IMyType
{
[Obsolete("This constructor must not be called directly", true)]
public ThingA()
{
}
public void Initialise(string args)
{
// do something with args
}
}
This will cause a compile error if you try to call the constructor explicitly, but won't prevent calling it in the generic method with the new()
constraint.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With