Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatic property injection with Microsoft Unity

I use contructor injection in my solution, but this one class has a property that i do not want to pass in the constructor where i have the invariant dependencies.

Let's say i got an ILogger and it has a FileName property i want to set, while still having it set the dependancies in the contructor. How do i go about registering the type, and at the same time pass the defaunt connection string.

I hope there is an easy way to do it - preferably without decorating the property with an attribute, but if the setup is easier with the attribute i guess that's cool :)

So the question is, how do i inject a property value on an object that also uses contructor injection - with Unity.

UPDATE: I mentioned it in the title, but i forgot to elaborate in the body of the text - i want to set these dependencies up manually (in code) as opposed to in a config file.

like image 681
Per Hornshøj-Schierbeck Avatar asked Apr 22 '26 00:04

Per Hornshøj-Schierbeck


1 Answers

Ok i guess it helped to ask the question, i found out - here it is.

container.Configure<InjectedMembers>().ConfigureInjectionFor<BasicLogger>(
  new InjectionProperty("FileName", @"C:\test.log")
);
like image 200
Per Hornshøj-Schierbeck Avatar answered May 01 '26 02:05

Per Hornshøj-Schierbeck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!