Deep cloning via Automapper ignoring specific property from the hierarchy





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have fairly simple question regarding Automapper mapping definition. My intent is to deep clone an object via Automapper while ignoring 'Id' property, this is why i have chosen it to customize the mapping.



public interface IEntity<T>
{
T Id { get; }
}

public abstract class Entity : IEntity<Guid>
{
public Guid Id { get; set; }
}


All my entities are deriving from Entity class and i simply wants to ignore all Id property in the nested hierarchy of my object without being so explicit about the mapping definition.



So far i have come up with the following piece of code to do the cloning but how to ignore Id property mapping for the nested properties and not just for the root.



public static T AutomapperClone<T>(this T source)
where T : IEntity<Guid>
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<T, T>()
.ForMember(d => d.Id, o => o.Ignore());
});
// checking configuration validity
config.AssertConfigurationIsValid();
// creating mapper
var mapper = config.CreateMapper();
var copy = mapper.Map<T, T>(source);
return copy;
}


The idea is that all entities get their new Id instead of using the same mapped ones. Is it accomplishable via Automapper?



Appreciate your feedback.










share|improve this question

























  • AM is mainly a cache and that cache lives in MapperConfiguration. You app should have only one such object. So unless you're willing to spend some time understanding how AM works, you're better off mapping by hand.

    – Lucian Bargaoanu
    Nov 24 '18 at 8:47











  • Appreciate your comment, yes i have basic understanding of AM but the issue is that there are few dynamic modules which are loaded on demand and extends the model so i can't define the one time MapperConfiguration fully comprising of extended models. So i am trying to seek some help on AM if my source and target Models are same. Its a valid use case and i am asking this question if accomplishable via AM

    – Furqan Safdar
    Nov 24 '18 at 9:15













  • No, modules shouldn't use the MappingConfiguration, they should define profiles to be loaded by the MappingConfiguration singleton defined by the app. So you should fix the way you use AM, before trying to make your mappings work. When your house is on fire, you don't try to fix the faucets.

    – Lucian Bargaoanu
    Nov 24 '18 at 9:28


















0















I have fairly simple question regarding Automapper mapping definition. My intent is to deep clone an object via Automapper while ignoring 'Id' property, this is why i have chosen it to customize the mapping.



public interface IEntity<T>
{
T Id { get; }
}

public abstract class Entity : IEntity<Guid>
{
public Guid Id { get; set; }
}


All my entities are deriving from Entity class and i simply wants to ignore all Id property in the nested hierarchy of my object without being so explicit about the mapping definition.



So far i have come up with the following piece of code to do the cloning but how to ignore Id property mapping for the nested properties and not just for the root.



public static T AutomapperClone<T>(this T source)
where T : IEntity<Guid>
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<T, T>()
.ForMember(d => d.Id, o => o.Ignore());
});
// checking configuration validity
config.AssertConfigurationIsValid();
// creating mapper
var mapper = config.CreateMapper();
var copy = mapper.Map<T, T>(source);
return copy;
}


The idea is that all entities get their new Id instead of using the same mapped ones. Is it accomplishable via Automapper?



Appreciate your feedback.










share|improve this question

























  • AM is mainly a cache and that cache lives in MapperConfiguration. You app should have only one such object. So unless you're willing to spend some time understanding how AM works, you're better off mapping by hand.

    – Lucian Bargaoanu
    Nov 24 '18 at 8:47











  • Appreciate your comment, yes i have basic understanding of AM but the issue is that there are few dynamic modules which are loaded on demand and extends the model so i can't define the one time MapperConfiguration fully comprising of extended models. So i am trying to seek some help on AM if my source and target Models are same. Its a valid use case and i am asking this question if accomplishable via AM

    – Furqan Safdar
    Nov 24 '18 at 9:15













  • No, modules shouldn't use the MappingConfiguration, they should define profiles to be loaded by the MappingConfiguration singleton defined by the app. So you should fix the way you use AM, before trying to make your mappings work. When your house is on fire, you don't try to fix the faucets.

    – Lucian Bargaoanu
    Nov 24 '18 at 9:28














0












0








0








I have fairly simple question regarding Automapper mapping definition. My intent is to deep clone an object via Automapper while ignoring 'Id' property, this is why i have chosen it to customize the mapping.



public interface IEntity<T>
{
T Id { get; }
}

public abstract class Entity : IEntity<Guid>
{
public Guid Id { get; set; }
}


All my entities are deriving from Entity class and i simply wants to ignore all Id property in the nested hierarchy of my object without being so explicit about the mapping definition.



So far i have come up with the following piece of code to do the cloning but how to ignore Id property mapping for the nested properties and not just for the root.



public static T AutomapperClone<T>(this T source)
where T : IEntity<Guid>
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<T, T>()
.ForMember(d => d.Id, o => o.Ignore());
});
// checking configuration validity
config.AssertConfigurationIsValid();
// creating mapper
var mapper = config.CreateMapper();
var copy = mapper.Map<T, T>(source);
return copy;
}


The idea is that all entities get their new Id instead of using the same mapped ones. Is it accomplishable via Automapper?



Appreciate your feedback.










share|improve this question
















I have fairly simple question regarding Automapper mapping definition. My intent is to deep clone an object via Automapper while ignoring 'Id' property, this is why i have chosen it to customize the mapping.



public interface IEntity<T>
{
T Id { get; }
}

public abstract class Entity : IEntity<Guid>
{
public Guid Id { get; set; }
}


All my entities are deriving from Entity class and i simply wants to ignore all Id property in the nested hierarchy of my object without being so explicit about the mapping definition.



So far i have come up with the following piece of code to do the cloning but how to ignore Id property mapping for the nested properties and not just for the root.



public static T AutomapperClone<T>(this T source)
where T : IEntity<Guid>
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<T, T>()
.ForMember(d => d.Id, o => o.Ignore());
});
// checking configuration validity
config.AssertConfigurationIsValid();
// creating mapper
var mapper = config.CreateMapper();
var copy = mapper.Map<T, T>(source);
return copy;
}


The idea is that all entities get their new Id instead of using the same mapped ones. Is it accomplishable via Automapper?



Appreciate your feedback.







c# automapper deep-copy automapper-8






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 7:21







Furqan Safdar

















asked Nov 24 '18 at 7:12









Furqan SafdarFurqan Safdar

12.5k114780




12.5k114780













  • AM is mainly a cache and that cache lives in MapperConfiguration. You app should have only one such object. So unless you're willing to spend some time understanding how AM works, you're better off mapping by hand.

    – Lucian Bargaoanu
    Nov 24 '18 at 8:47











  • Appreciate your comment, yes i have basic understanding of AM but the issue is that there are few dynamic modules which are loaded on demand and extends the model so i can't define the one time MapperConfiguration fully comprising of extended models. So i am trying to seek some help on AM if my source and target Models are same. Its a valid use case and i am asking this question if accomplishable via AM

    – Furqan Safdar
    Nov 24 '18 at 9:15













  • No, modules shouldn't use the MappingConfiguration, they should define profiles to be loaded by the MappingConfiguration singleton defined by the app. So you should fix the way you use AM, before trying to make your mappings work. When your house is on fire, you don't try to fix the faucets.

    – Lucian Bargaoanu
    Nov 24 '18 at 9:28



















  • AM is mainly a cache and that cache lives in MapperConfiguration. You app should have only one such object. So unless you're willing to spend some time understanding how AM works, you're better off mapping by hand.

    – Lucian Bargaoanu
    Nov 24 '18 at 8:47











  • Appreciate your comment, yes i have basic understanding of AM but the issue is that there are few dynamic modules which are loaded on demand and extends the model so i can't define the one time MapperConfiguration fully comprising of extended models. So i am trying to seek some help on AM if my source and target Models are same. Its a valid use case and i am asking this question if accomplishable via AM

    – Furqan Safdar
    Nov 24 '18 at 9:15













  • No, modules shouldn't use the MappingConfiguration, they should define profiles to be loaded by the MappingConfiguration singleton defined by the app. So you should fix the way you use AM, before trying to make your mappings work. When your house is on fire, you don't try to fix the faucets.

    – Lucian Bargaoanu
    Nov 24 '18 at 9:28

















AM is mainly a cache and that cache lives in MapperConfiguration. You app should have only one such object. So unless you're willing to spend some time understanding how AM works, you're better off mapping by hand.

– Lucian Bargaoanu
Nov 24 '18 at 8:47





AM is mainly a cache and that cache lives in MapperConfiguration. You app should have only one such object. So unless you're willing to spend some time understanding how AM works, you're better off mapping by hand.

– Lucian Bargaoanu
Nov 24 '18 at 8:47













Appreciate your comment, yes i have basic understanding of AM but the issue is that there are few dynamic modules which are loaded on demand and extends the model so i can't define the one time MapperConfiguration fully comprising of extended models. So i am trying to seek some help on AM if my source and target Models are same. Its a valid use case and i am asking this question if accomplishable via AM

– Furqan Safdar
Nov 24 '18 at 9:15







Appreciate your comment, yes i have basic understanding of AM but the issue is that there are few dynamic modules which are loaded on demand and extends the model so i can't define the one time MapperConfiguration fully comprising of extended models. So i am trying to seek some help on AM if my source and target Models are same. Its a valid use case and i am asking this question if accomplishable via AM

– Furqan Safdar
Nov 24 '18 at 9:15















No, modules shouldn't use the MappingConfiguration, they should define profiles to be loaded by the MappingConfiguration singleton defined by the app. So you should fix the way you use AM, before trying to make your mappings work. When your house is on fire, you don't try to fix the faucets.

– Lucian Bargaoanu
Nov 24 '18 at 9:28





No, modules shouldn't use the MappingConfiguration, they should define profiles to be loaded by the MappingConfiguration singleton defined by the app. So you should fix the way you use AM, before trying to make your mappings work. When your house is on fire, you don't try to fix the faucets.

– Lucian Bargaoanu
Nov 24 '18 at 9:28












1 Answer
1






active

oldest

votes


















0














I wouldn't use Automapper for this person, try AnyClone to do this. It does deep cloning and can ignore by property name which seems to be what you are looking for.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456025%2fdeep-cloning-via-automapper-ignoring-specific-property-from-the-hierarchy%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I wouldn't use Automapper for this person, try AnyClone to do this. It does deep cloning and can ignore by property name which seems to be what you are looking for.






    share|improve this answer




























      0














      I wouldn't use Automapper for this person, try AnyClone to do this. It does deep cloning and can ignore by property name which seems to be what you are looking for.






      share|improve this answer


























        0












        0








        0







        I wouldn't use Automapper for this person, try AnyClone to do this. It does deep cloning and can ignore by property name which seems to be what you are looking for.






        share|improve this answer













        I wouldn't use Automapper for this person, try AnyClone to do this. It does deep cloning and can ignore by property name which seems to be what you are looking for.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 5:56









        Michael BrownMichael Brown

        7681119




        7681119
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456025%2fdeep-cloning-via-automapper-ignoring-specific-property-from-the-hierarchy%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()