3

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.

3 Answers 3

2

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")
);
Sign up to request clarification or add additional context in comments.

Comments

0

If you are injecting properties you have to use [Dependency] or else manually inject that dependency.

1 Comment

Yeah i didn't make it very clear - but i do want to do it manually ;)
0

You usually want an IConfiguration interface to be injected. This would probably have a LogFile property that you can read.

The Configuration implimentation is usually just a simple wrapper to read items from the config file.

1 Comment

You got a good point there, but we're trying to keep it as simple as possible, and all this specific implementation needs is a filename.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.