Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# multiple arguments in one to DRY out parameter-passing

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?

like image 833
Sarah Vessels Avatar asked Jan 11 '10 21:01

Sarah Vessels


People also ask

What is the full name of C?

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 in C language?

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.

Is C language easy?

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.

What is C language used for?

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 ...


2 Answers

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.

like image 71
jason Avatar answered Oct 30 '22 04:10

jason


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.

like image 29
Francisco Noriega Avatar answered Oct 30 '22 06:10

Francisco Noriega