开发者

C# settings injection

开发者 https://www.devze.com 2023-02-20 09:31 出处:网络
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 sett

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 开发者_JS百科like ninject?

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


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


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

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

Have fun

0

精彩评论

暂无评论...
验证码 换一张
取 消