Giter VIP home page Giter VIP logo

Comments (3)

tompipe avatar tompipe commented on June 30, 2024 1

I've added some crazy magic to all the value resolvers in one of my codebases recently. At it's core, it converts IPublishedContent(s) to strongly typed models, and also returns the type of the matched model from the value converter

Models builder uses that return type the next time models are generated, and it will generate strongly typed properties for all properties returning IPublishedContent(s), including nested content pickers (with a single doctype as the blueprint)

So automatically I get

///<summary>
/// Team Members
///</summary>
[ImplementPropertyType("teamMembers")]
public IEnumerable<Core.Models.Content.TeamMember> TeamMembers
{
    get { return this.GetPropertyValue<IEnumerable<Core.Models.Content.TeamMember>>("teamMembers"); }
}

instead of

///<summary>
/// Team Members
///</summary>
[ImplementPropertyType("teamMembers")]
public IEnumerable<IPublishedContent> TeamMembers
{
    get { return this.GetPropertyValue<IEnumerable<IPublishedContent>>("teamMembers"); }
}

It needs some refactoring and further testing before I share it (maybe as a PR), but in the meantime, this is what handles the model builder conversion

public IPublishedContent GetTypedModel<TModel>(IPublishedContent nestedContent) where TModel : PublishedContentModel
{
    var factoryResolver = Umbraco.Core.Models.PublishedContent.PublishedContentModelFactoryResolver.Current;
    if (factoryResolver?.HasValue == true)
    {
        return factoryResolver.Factory?.CreateModel(nestedContent) as TModel;
    }

    return nestedContent; // or throw an exception perhaps?
}

So I'd vote option 1, given that that factory resolver is in the core anyway. If a factory is configured you can handle the conversion in the value converter, and additionally return the correct strong type, to allow models builder to generate strongly typed nested content properties for free 😄

from umbraco-nested-content.

mattbrailsford avatar mattbrailsford commented on June 30, 2024

This is a ModelsBuilder related question. Looking at the implementation of OfType in core, it just does a simple "is" type check, so the model needs to already be of the given type.

Options could be 1) we support models builder and within the value converter convert the items to the respective types or 2) implement an OfType method of our own that simply create a models builder model for the given type.

I think option 1 would probably be the correct answer but would mean we'd have a dependency on MB

from umbraco-nested-content.

leekelleher avatar leekelleher commented on June 30, 2024

Closing this ticket, further details on #124 (comment)

from umbraco-nested-content.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.