简体   繁体   中英

Can I create a stateless static class in MVC back-end

I have an MVC application that contains a static class providing a boolean value to the rest of the program. My issue is that I want the boolean to reset with each call to the back-end. I want it to be "stateless", getting initialized to false for each and every back-end call. I'll then set it to true as needed. For instance, consider the following where from the Index view we load the About view and then afterwards load the Contact view...

public static class BooleanProvider
{
    public static bool theBool = false;
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";
        BooleanProvider.theBool = true;

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";
        //I want BooleanProvider.theBool to be false here, since it's a different call, even though we set it to true in the About() method

        return View();
    }
}

When I check BooleanProvider.theBool at the commented line, it's set to true. Is there a way to "re-initialize" the boolean with every call? I'd rather not make the class instantiable and have to new it up with each controller call, but maybe there is no other way?

Thank you in advance!

You have to use Dependency injection for that.

dotnetcore solution

Scoped Lifetime is exactly what you needed. https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.1#scoped

At your Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // ...
    services.AddScoped<BooleanProvider>();
    // ...
}

and at your HomeController

public class HomeController : Controller
{
    private readonly BooleanProvider _booleanProvider;
    public HomeController (BooleanProvider booleanProvider){
        _booleanProvider = booleanProvider;
        // ...
    }
    // ...
}

not core solution

Here is a guide if you do not use dotnetcore https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-dependency-injection

PerRequestLifetimeManager is what you needed here

Install-Package Unity.Mvc3

At Global.asax

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
 
    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
 
    Bootstrapper.Initialise();
}
    public static class Bootstrapper
    {
        public static void Initialise()
        {
            var container = BuildUnityContainer();
 
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
        }
 
        private static IUnityContainer BuildUnityContainer()
        {
            var container = new UnityContainer();
            
            Func<LifetimeManager> LM = () => { return new PerRequestLifetimeManager(); };

            container.RegisterType<IBooleanProvider, BooleanProvider>(LM());
            
            return container;
        }
    }
public class HomeController : Controller
{
    private readonly IBooleanProvider _booleanProvider;
    public HomeController (IBooleanProvider booleanProvider){
        _booleanProvider = booleanProvider;
        // ...
    }
    // ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM