Copied to clipboard

Flag this post as spam?

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


  • AJ Murray 3 posts 72 karma points
    17 days ago
    AJ  Murray
    0

    Mapping from POCO to IContent trying to set mapper.Define using MapperContext extension method for target parentId

    I am using v13.2.2 and want define mappings for an import process that is going to run in the background.

    So I want to define a mapping FROM a simple object TO an Umbraco IContent item that I can then save to my database.

    Having spent a long time investigating the Anaximapper project I determined that I cannot use it. It only provides an easy way to map the wrong way, that is, from IPublishedContent to a simple object, such as a custom view model.

    So I have gone back to looking at the UmbracoMapper service that comes standard with v13 now. That looks like I can define any mapping directions. Have been playing around with test code like this:

    // my extension method for the context to pass in a parentId
    public static void SetId(this MapperContext context, int id)
    {
        context.Items["Id"] = id;
    }
    
    // then this is my map definition with a target construction method and map function
    public void DefineMaps(IUmbracoMapper mapper)
    {
        mapper.Define<BrandDto, BrandItem>((source) => 
            _contentService.Create(source.Title, (int)context.Items.GetValue("Id"), BrandItem.ModelTypeAlias),
            Map);
    }
    
    private void Map(BrandDto source, BrandItem target, MapperContext context)
    {
        target.BrandId = source.Id;
        target.BrandTitle = source.Title;
    }
    

    I know this isn't right as it doesn't even compile but you might get the gist of what I need.

    My problems are that I don't know how to write an Action<> or a Func<> for the construction method, nor do I know how to invoke my custom MapperContext.SetId(21) function to tell the content service what the parentId needs to be, that is referenced in the construction method

    If someone could point me to some example code, I would be most grateful.

Please Sign in or register to post replies

Write your reply to:

Draft