Yippie-Ki-Yay
Yippie-Ki-Yay

Reputation: 22794

C# settings injection

Is it okay to use injection when writing code for a simple settings class?

I have some class like Simulator, which has it's own SimulatorSettings. So what approach should I take to inject these settings using something like ninject?

Or maybe my approach is incorrect and I should do something other about this kind of settings?

Upvotes: 0

Views: 228

Answers (2)

Daniel Marbach
Daniel Marbach

Reputation: 2314

When you are talking about configuration sections you could do something like:

this.Bind<SimulatorSettings>().ToMethod(
    ctx => (SimulatorSettings) ConfigurationManager.GetSection["Simulator"])

Have fun

Upvotes: 1

Oded
Oded

Reputation: 498914

Your question is not very clear - if I understand correctly, you are asking if you should be using Dependency Injection and an IoC container (such as ninject) in order to inject a settings object into your class.

This is perfectly fine, though you should probably consider using a factory for constructing your objects instead (in particular the settings object would to be initialized correctly).

Upvotes: 2

Related Questions