c# MVC5 What is the right place to load menu items

asp.net-mvc c# entity-framework-6 razor

Question

Could you please explain to me the best practice for loading menu items from a database using MVC5 and Entity Framework6? I have a small difficulty. The menu and localization object must only be loaded once before being used from a collection that is accessible worldwide. After the website launches, they won't change much, so I'm just going to implement some Update() methods and call them as needed.

1
1
4/29/2015 5:56:46 PM

Accepted Answer

As I was already depressed, I considered the global situation. asax So I presently have two options for doing it using Global. asax:

Update It is not a good idea to utilize this strategy; choose method two instead.

public static ICollection<MenuItem> MenuItems {
    get
    {
        if (Application["MenuItems"] != null)
            return (ICollection<MenuItems>)Application["MenuItems"];
        else
            return new ICollection<MenuItems>();
    }
    set
    {
        Application["MenuItems"] = value;    
    }
}

private void LoadMenuItems()
{
    MyContext mc = new MyContext();
    this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
} 

protected void Application_Start(object sender, EventArgs e)
{
    this.MenuItems = LoadMenuItems();
}

Additionally (The following):

public static ICollection<MenuItem> MenuItems { get; set; }

private void LoadMenuItems()
{
    MyContext mc = new MyContext();
    this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
} 

protected void Application_Start(object sender, EventArgs e)
{
    this.MenuItems = LoadMenuItems();
}

The same is true for localization.

I need to conduct some experiments before deciding which is superior.

The "CustomHttpApplication" class, which is a descendant of the "HttpApplication" class, contains everything. and Global.asax must derive from the class "CustomHttpApplication". The business logic will be situated one level below, but the Global.asax file will still be clean and legible.

Thus, the whole code might seem as follows:

CustomHttpApplication.cs

public class CustomHttpApplication : HttpApplication
{
    public static ICollection<MenuItem> MenuItems { get; set; }

    private void LoadMenuItems()
    {
        MyContext mc = new MyContext();
        this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
    } 
}

Global.asax.cs

public class MvcApplication : CustomHttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        MenuItems = this.LoadMenuItems();
    }
}

Lastly, if you or I make the "LoadMenuItems" method "static," we will be able to change the MenuItems and/or Localization item collections as necessary.

1
4/30/2015 9:13:52 AM

Popular Answer

Use kid-like gestures.

public class FooController : Controller
{
    ...

    [ChildActionOnly]
    public ActionResult SiteMenu()
    {
        // load menu items however you need to

        return PartialView("_SiteMenu", menuModel);
    }
}

/Views/Shared/_SiteMenu.cshtml

@model Namespace.To.MenuModel

<!-- render your menu here -->

/Views/Shared/_Layout.cshtml

<!-- wherever you want the menu to display -->
@Html.Action("SiteMenu", "Foo")

Using the caching feature will prevent the menu from having to be retrieved from the database for each request if you want to cache the result.OutputCache like any other action, attributes on the child action.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow