I don't know if this is possible, but in some of my unit tests, I end up initializing different objects with the same arguments. I would like to be able to store those arguments in some variable and just initialize the multi-parameter object constructor with that variable so instead of doing:
Thing thing1 = new Thing(arg1, arg2, arg3, arg4);
Thing thing2 = new Thing(arg1, arg2, arg3, arg4);
Thing thing3 = new Thing(arg1, arg2, arg3, arg4);
I could do the following:
MagicalArgumentsContainer args = (arg1, arg2, arg3, arg4);
Thing thing1 = new Thing(args);
Thing thing2 = new Thing(args);
Thing thing3 = new Thing(args);
Is there any way of doing this without overriding Thing
's constructor to take a list that it manually explodes and plucks arguments out of? Maybe some C# syntactic sugar?
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.
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.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
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 ...
I mean, there's this:
Func<Thing> f = () => new Thing(arg1, arg2, arg3, arg4);
Thing thing1 = f();
Thing thing2 = f();
Thing thing3 = f();
Thing thing4 = f();
Just be careful of closure semantics.
Well I guess you could use an IoC container, since several of this also offer an ObjectFactory, ie you tell the IoC how to make a new instance of type T and then you just ask the IoC to give you an instance of it.
However if you dont want to get an IoC, you could make yourself a little factory class
public MagicFactory
{
T arg1, T2 arg2, T3 arg3,.., TN argN;
public MagicFactory(T1 a1,..., TN aN)
{
this.arg1=a1;
...
this.argN = an;
}
public Thing GimmeDaThing()
{
return new Thing(this.arg1,...,this.argN);
}
}
however keep in mind that if the arguments are not of value type, then all your instances of Thing
will have references to the same objects, so, even though you have different instance of Things, they all would point to the same arg1. What you could do to fix that is to actually take in a Func in the parameter, so you can actually create a new one:
public MagicFactory
{
Func<T1> arg1, ,.., Func<TN> argN;
public MagicFactory(Func<T1> a1,..., Func<TN> aN)
{
this.arg1=a1;
...
this.argN = an;
}
public Thing GimmeDaThing()
{
return new Thing(this.arg1(),...,this.argN());
}
}
and you would call it like this:
var magicContainer = new MagicFactory(()=> new T1(...),..., ()=>new T2(..);
var thing1 = magicContainer.GimmeDaThing();
var thing1 = magicContainer.GimmeDaThing();
var thing1 = magicContainer.GimmeDaThing();
var thing1 = magicContainer.GimmeDaThing();
and you would get a fresh instance of Thing each time, each with their own property objects.
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