4

I am developing an application using MVC6. I can read JSON configuration in Startup file and dependency inject it in the controllers.

Now, my requirements is that I want to access configuration inside classes that are not controllers. I can't use constructor DI in such cases. These classes reside in some folder of the MVC6 application but they are not model or controller classes.

What's the best way to access configuration in such non-controller classes?

Thank you.

UPDATE:

Just to clarify - I can't use DI with these classes.

These classes are instantiated only when needed based on some condition and methods are called on them. I need to access JSON config settings from within these classes/methods. So, far I know only these ways:

  1. Make Configuration property from Startup as static so that I can say Startup.Configuration["..."].

  2. Set static properties of a custom class from Startup after reading JSON config and then access those static properties.

Are these alternative good (other than DI of course)? Are there any other ways?

5
  • What kind of classes are these "non-controller classes"? Where are they used? What's their lifetime? Commented Aug 3, 2015 at 7:56
  • Some contain some business logic, some are helpers and utilities. They need to access settings stored in config file. Commented Aug 3, 2015 at 8:29
  • 1
    Then hook them up to the DI system and inject them? Commented Aug 3, 2015 at 8:30
  • 1
    seems like you have a false premise that you can't use DI for some kinds of classes. you can use it for any kind of class, not just controllers and models Commented Aug 4, 2015 at 17:34
  • Places where I use the constructor method for the class I'm trying to inject the settings into then complain that I'm not passing the settings object, so they need the DI chained all the way up to the controller. Is that correct? Commented Aug 23, 2017 at 10:56

2 Answers 2

3

what i would do is to create a register service method like this one:

 public static void AddMyTools(this IServiceCollection services, IConfiguration config){
      //Save the config in the class
 }

In this static method i will store the configurations i need in this static class.

Then in the Startup.cs i will use the namespace of my tools then in the ConfigureServices section i will add my tools like this:

 services.AddMyTools(Configuration); 

hope it help you. i did not try it, maybe some change but you have the global idea. (I was searching for this and @Sam Farajpour Ghamari give me the idea with his answer)

Sign up to request clarification or add additional context in comments.

Comments

0

You could inject your configuration to the any classes you want. Simply inject your configuration class on the construction method.

class MyConfig()
{
   // your configuration members
}

class MyBLClass(MyConfig config)
{
    private MyConfig _config;
    public MyBLClass(MyConfig config)
    {
        _config=config;
    }
    // use the _config in your class
}

And in the DI simply register your BI class

container.RegisterSelf<MyBLClass>(); // just an example read your DI doc

Therefore whenever you ask your DI to locate the class. config object automatically injected to the class too.

As a better solution you could use interfaces instead of concert classes.

Comments

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.