Software Injection of Properties with Microsoft Unity

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

Say I have an ILogger, and I have a FileName property that I want to set, but at the same time it sets the dependencies in the contructor. How can I switch to type registration and at the same time pass the defaunt connection string.

I hope there is an easy way to do this - preferably without decorating the property with an attribute, but if setting up is easier with an attribute, I think it's cool :)

So the question is, how do I paste the value of a property onto an object that also uses constructor injection - with Unity.

UPDATE: I mentioned this in the title, but I forgot to work in the text - I want to install these dependencies manually (in the code), and not in the configuration file.

+3
source share
3 answers

Well, I think it helped ask a question, I found out - that’s it.

container.Configure<InjectedMembers>().ConfigureInjectionFor<BasicLogger>(
  new InjectionProperty("FileName", @"C:\test.log")
);
+2
source

If you inject properties, you need to use [Dependency] or manually enter this dependency.

0
source

IConfiguration. , , LogFile, .

.

0

Source: https://habr.com/ru/post/1711993/


All Articles