Copied to clipboard

Flag this post as spam?

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


  • Sven Vervloet 10 posts 81 karma points
    Jun 08, 2018 @ 09:07
    Sven Vervloet
    0

    Extending Generated Classes

    Hi,

    So far I asked three questions on this forum, but no responses. I hove this time I have more luck :)

    On the website I'm working on we have a calendar. For this site we use custom controllers (RenderMvcController) to filter the data before we send it to the View.

    We need a way to filter the calendar-items in the controller. For the dates we use two (doc)types of nested content (single date OR a range of dates).

    I would like the filtering to be as simple as possible (Linq), so I need to be able to access the properties of the nested content.

    My collegue has good experience with Vorto and nested content. All he needed was to extend the generated models with another partial class.

    public partial class UmbNewsItem
        {
            [ImplementPropertyType("news")]
            public Newsdetached News
            {
                get
                {
                    var news = this.GetVortoValue<IPublishedContent>("news");
    
                    return news == null ? null : new Newsdetached(news);
                }
            }
        }
    

    I would like something in the same line. The difference is that his nested content is limited to one doctype and only one nested item is permitted. In my case there could be several items and I use nested content of two (doc)types.

    Could someone point me in the right direction?

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 08, 2018 @ 09:23
    Nik
    0

    Hi Sven,

    How are you generating your classes?

    I've you had experience extending classes already, you could add a common interface to the doc types you are looking to work with ?

    Nik

  • Sven Vervloet 10 posts 81 karma points
    Jun 08, 2018 @ 09:41
    Sven Vervloet
    0

    Hi.

    Me and my co-worker are both relatively new to Umbraco (< 1 year). This is my first attempt at extending Umbraco generated models.

    I use LiveAppData to generate the models. I have a partial class to extend the generated class.

    Could this work?

    public partial class Agenda
    {
        //"datum" is the property that has nested content
    
        //returns IEnumerable of Single Date
       public IEnumerable<EnkeleDatum> EnkeleDatum
       {
            get
            {
                return this.GetPropertyValue<IEnumerable<EnkeleDatum>>("datum");
            }
       }
    
        //returns IEnumerable of a range of dates
        public IEnumerable<Datumbereik> DatumBereik
        {
            get
            {
                return this.GetPropertyValue<IEnumerable<Datumbereik>>("datum");
            }
        }     
    }
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 08, 2018 @ 10:04
    Nik
    0

    Hi Sven,

    Yeah, the premise is the same.

    So you could do something like:

    public partial class Agenda : IMySharedInterface
    {
           public bool ShouldIUseThis
           {
                 //My agenda specific implemetation
                 return this.
           }
    }
    

    Personally I don't use the LiveAppData models approach. I've moved to using the API to get compiled models in my own dlls. But that is just another approach you could use.

    Nik

  • Sven Vervloet 10 posts 81 karma points
    Jun 08, 2018 @ 10:11
    Sven Vervloet
    1

    Thank you for the time and the response. This is the proof I needed to know that the forum works :)

    I have not tested it yet, but at the moment I have written this code:

    var parent = (AgendaContainer) Umbraco.TypedContent(result.Id);
    
                agendaList = parent.Children<Agenda>()
                    .Where
                        (x => x.EnkeleDatum.First().Datum.Year == DateTime.Now.Year ||
                         x.DatumBereik.First().StartDatum.Year == DateTime.Now.Year)
                    .ToList();
    
  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Jun 08, 2018 @ 10:14
    Nik
    0

    The forum does work :-) Just sometimes things slip by the wayside unfortunately.

    I know there are discussions about the forum happening to try and help this but I think they will be a little bit of time before they are implemented.

    In the mean time, please persist. Don't get disheartened if your question doesn't get an answer. You can always throw a bump on it (but don't spam it) to get it back up the list or, if you solve the problem yourself share your answer. :-)

Please Sign in or register to post replies

Write your reply to:

Draft