Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

aspnet 5 dependency injection

$
0
0

Hi,

i know this might sound a bit daft, but everything i read about dependency injection is around the controller, if i wanted to use dependency injection within my models does anyone know if its possible and how would i go about doing it,

i have looked at the following threads which help explain how to define dependency injections but none explain doing it in model classes etc,

https://marriedgeek.com/dependency-injection-out-of-the-box-with-asp-net-vnext/

http://stackoverflow.com/questions/28970140/how-properly-inject-httpcontext-in-mvc6

in short what i am trying to accomplish is injecting the httpcontext into a model so i can create a controller class variable of the model rather than having to recreate the instance for every method in the controller as thats where the httpcontext is accessible from.

I.E

public class HomeController
{
    //class variable    
    public TheModel myModel = new TheModel();


    public IActionResult index()
    {
        //use myModel here to do stuff
        return View();
    }
}

//Trying to find out how to httpcontect inject into this model class
public class TheModel
{
    public TheModel("httpcontext injection here")
    {
       //Do some stuff with the httpcontext object
    }
}


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>