Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Mathias Hove 3 posts 23 karma points
    Sep 22, 2016 @ 22:39
    Mathias Hove
    0

    Using the same initialized umbraco context trough all repositories

    Hi

    I have set up repositories and bound the data to viewmodels.

    I have been trying to find a way to create a shared UmbracoContext.Current.PublishedContentRequest.PublishedContent, that can be used through all repositories. The reason for that is that i have a base controller where i hit the repository 1 time. And when i render the view i hit 1 or 2 more repositories.

    Is it bad to initialize a new UmbracoContext.Current.PublishedContentRequest.PublishedContent for each repository ??

    It seems wrong in my head. I have done the following to avoid exactly that. I want to hear your opinion on it, and if it is totally wrong.

    An initalizer to create the context

     public class ContextInitializer
     {
    
        public ContextInitializer()
        {
            Content = Umbraco.Web.UmbracoContext.Current.PublishedContentRequest.PublishedContent;
        }
    
        public IPublishedContent Content { get; set; }
    
     }
    

    A controller where i create an instance of the context.

      public class BaseController : RenderMvcController
      {
    
        private IBaseRepository baseRepository;
        public ContextInitializer context = new ContextInitializer();
    
        public BaseController()
        {
            baseRepository = new BaseRepository(context.Content);
        }
    
        protected ViewResult View(BaseVm model)
        {
            return View(null, model);
        }
    
        protected ViewResult View(string view, BaseVm model)
        {
            //Map Vm
            model.LayoutData.Base = baseRepository.GetBase();
            model.LayoutData.Seo = baseRepository.GetSeo();
            model.LayoutData.Nav = baseRepository.Navigation();
    
            return base.View(model);
        }
    }
    

    And the controller that renders the view

     public class HomeController : BaseController
     {
        private IHomeRepository homeRepository { get; }
        private IGalleryRepository galleryRepository { get; }
    
    
        public HomeController()
        {
            homeRepository = new HomeRepository(context.Content);
            galleryRepository = new GalleryRepository(context.Content);
    
    
        }
    
        public ActionResult Home()
        {
            HomeVm homeVm = new HomeVm();
    
            homeVm.Home = homeRepository.Get();
            homeVm.Galleries = galleryRepository.GetSelectedGalleryThumbs();
    
            return View(homeVm);
        }
    }
    

    Example of repository

    public class BaseRepository : IBaseRepository
    {
        private IPublishedContent _content { get; }
        public BaseRepository(IPublishedContent content)
        {
            _content = content;
        }
    }
    

    Am i overthinking this, or should i just create a new context in each repository?? By creating i mean writing the logic to access the context in each repository.

Please Sign in or register to post replies

Write your reply to:

Draft