Copied to clipboard

Flag this post as spam?

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


  • Allie 26 posts 116 karma points
    Oct 22, 2021 @ 14:31
    Allie
    0

    Umbraco Repository

    Hi

    I want to create an umbraco repository which will stand for a dal level in mvc (data access level implementation). Can sb provide other ways of how to create it, not using umbraco helpers ?

     public class UmbracoRepository
        {
            UmbracoHelper _helper;
            public UmbracoRepository(UmbracoHelper helper)
            {
                _helper = helper;
            }
            public List<NewsViewModel> GetAllNews()
            {
                var newsList = _helper.Content(Guid.Parse("1d770f10-d1ca-4a26-9d68-071e2c9f7ac1"))
                          .ChildrenOfType("blogpost").Where(x => x.IsVisible()).ToList();
    

    Regards ^^

  • Søren Gregersen 441 posts 1884 karma points MVP 2x c-trib
    Oct 22, 2021 @ 15:01
    Søren Gregersen
    100

    Hi,

    You should use an extension method for creating this

    public static class UmbracoHelperExtensions 
    {
        public static IEnumerable<IPublishedContent> GetAllNews(this UmbracoHelper umbraco)
        {
            return umbraco.Content(Guid.Parse("1d770f10-d1ca-4a26-9d68-071e2c9f7ac1"))
                      .ChildrenOfType("blogpost").Where(x => x.IsVisible()).ToList();
        }
    }
    

    The problem with the solution above is that it will access all your published content. If you publish a lot of blogposts, then every page you use the method on, will have to handle all the posts. This can cause performance issues down the line.

    I would suggest searching for the posts instead, and use paging to only show the posts you need.

    HTH :)

Please Sign in or register to post replies

Write your reply to:

Draft