Copied to clipboard

Flag this post as spam?

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


  • David Peck 690 posts 1896 karma points c-trib
    Aug 03, 2016 @ 08:37
    David Peck
    0

    Polymorphism & minimising reflection

    I currently have a page where the main content is built up using NestedContent and with many different types of content that can go within it (I'm also using the ModelsBuilder). The problem I'm facing is that I have an IEnumerable of IPublishedContent but I want to pass in to each partial, that represents each block of content, a strongly typed model. I've done this and it works just fine however in order to achieve this I've got this code:

    foreach (var nestedContent in landingPage.ContentSections.Where(nc => nc != null))
    {
        switch (nestedContent.DocumentTypeAlias)
        {
            case ActionLandingPageSection.ModelTypeAlias:
                yield return new ActionLandingPageSection(nestedContent);
                continue;
            case BenefitsLandingPageSection.ModelTypeAlias:
                yield return new BenefitsLandingPageSection(nestedContent);
                continue;
            case CardSliderLandingPageSection.ModelTypeAlias:
                yield return new CardSliderLandingPageSection(nestedContent);
                continue;
            case ChargesLandingPageSection.ModelTypeAlias:
                yield return new ChargesLandingPageSection(nestedContent);
                continue;
            case ContactLandingPageSection.ModelTypeAlias:
                yield return new ContactLandingPageSection(nestedContent);
                continue;
            case GuideSliderLandingPageSection.ModelTypeAlias:
                yield return new GuideSliderLandingPageSection(nestedContent);
                continue;
            case HeaderLandingPageSection.ModelTypeAlias:
                yield return new HeaderLandingPageSection(nestedContent);
                continue;
            case ImageContentLandingPageSection.ModelTypeAlias:
                yield return new ImageContentLandingPageSection(nestedContent);
                continue;
            case ListContentLandingPageSection.ModelTypeAlias:
                yield return new ListContentLandingPageSection(nestedContent);
                continue;
            case PromoBarLandingPageSection.ModelTypeAlias:
                yield return new PromoBarLandingPageSection(nestedContent);
                continue;
            case PromoLinkLandingPageSection.ModelTypeAlias:
                yield return new PromoLinkLandingPageSection(nestedContent);
                continue;
            case QuoteLandingPageSection.ModelTypeAlias:
                yield return new QuoteLandingPageSection(nestedContent);
                continue;
            case SignupLandingPageSection.ModelTypeAlias:
                yield return new SignupLandingPageSection(nestedContent);
                continue;
            case SingleGuideSliderLandingPageSection.ModelTypeAlias:
                yield return new SingleGuideSliderLandingPageSection(nestedConten)t;
                continue;
            case ThreeColumnLandingPageSection.ModelTypeAlias:
                yield return new ThreeColumnLandingPageSection(nestedContent);
                continue;
    
        }
    }
    

    And to be honest I simlified the above. The return line for each case is actually like this:

    yield return new LandingPageSectionViewModel<ThreeColumnLandingPageSection>(nestedContent as ThreeColumnLandingPageSection ?? new ThreeColumnLandingPageSection(nestedContent), landingPage);
    

    This code makes me cry for many reasons (e.g. DRY). The best alternative to this that I can think of is to identify each model using an attribute (perhaps using MEF?), and then use reflection. My concern is speed, as there is a lot of reflection needed, and that I'm creating a lot of fairly complex code for a relatively simple requirement.

    Can anyone think of a better architecture, that doesn't require me to copy and paste for each model and doesn't cause a load of reflection?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies