Steve Morin Steve Morin - 4 months ago 25
C# Question

Base Controller or method to access shared resources

I've been reading on base controllers and I can't seem to understand the concept. I need to simplify the code in the controller because I just repeat code for nothing. I use a database to collect data for the website and build some lists to use in views. All the pages share two of the lists which i copied in the base controller. When i remove the lists in the pagescontroller I get an error that the name 'sliders' and 'partenaires' does not exist in the current context. What would be the best way to prevent repeating code?

This is my base controller :

public class BaseController : Controller
{
PIAEntities db = new PIAEntities();

public BaseController()
{
var sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
var partenaires = db.Partenaire.Where(p => p.Afficher);
}
}


And this is a part of the controller that manages all my pages

public class PagesController : BaseController
{
public PagesController() : base()
{

}

PIAEntities db = new PIAEntities();

public ActionResult Activités()
{
var sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
var partenaires = db.Partenaire.Where(p => p.Afficher);
var model = new ModelDeBase { Slider = sliders, Partenaire = partenaires };

return View(model);
}

public ActionResult Actualités()
{
var sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
var pageActualites = db.Actualite.OrderByDescending(a => a.Date_publication).Take(10);
var donateurs = db.Donateur.Where(d => d.Afficher);
var partenaires = db.Partenaire.Where(p => p.Afficher);
var model = new ModelActualite { Slider = sliders, Partenaire = partenaires, PageActualite = pageActualites, Donateur = donateurs };

return View(model);
}
}


Solution :

Ok now it works and I ended up using this in my base controller

public class BaseController : Controller
{
PIAEntities db = new PIAEntities();
protected IEnumerable<Actualite> sliders { get; private set; }
protected IEnumerable<Partenaire> partenaires { get; private set; }

public BaseController()
{
sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
partenaires = db.Partenaire.Where(p => p.Afficher);
}
}


and the Pages controller :

public class PagesController : BaseController
{
public PagesController() : base()
{

}

PIAEntities db = new PIAEntities();

public ActionResult Activités()
{
var model = new ModelDeBase { Slider = sliders, Partenaire = partenaires };

return View(model);
}

public ActionResult Actualités()
{
var pageActualites = db.Actualite.OrderByDescending(a => a.Date_publication).Take(10);
var donateurs = db.Donateur.Where(d => d.Afficher);
var model = new ModelActualite { Slider = sliders, Partenaire = partenaires, PageActualite = pageActualites, Donateur = donateurs };

return View(model);
}
}

Answer

Both of your varaibles i.e. sliders and partenaires are declared in the scope of BaseController constructor, so they will not be accessible outside the constructor.

You should declare them at class level with suggestively as private setters so that child classes will not be able to change these properties.

public class BaseController : Controller
{
    PIAEntities db = new PIAEntities();

    protected IEnumerable<Actualite> sliders { get; private set; }

    protected IEnumerable<Paternaire> partenaires { get; private set; }

    public BaseController()
    {
        this.sliders = db.Actualite.Where(a => a.Afficher).OrderByDescending(a => a.Date_publication);
        this.partenaires = db.Partenaire.Where(p => p.Afficher);
    }
}