Skip to content

Give the possibility to clone objects without UseDeepCloning #2113

@samusaran

Description

@samusaran

Is your feature request related to a problem? Please describe.
Hi, I currently use AutoMapper to clone EF entities. The main use is either to support actually copies in the business logic (users can copy business objects) or to support forms which can be cancelled.

Given that I heavily use navigations, my object graph may be complex in some cases, which leads to UseDeepCloning to be impossible to use (maybe I want to clone a small object deep in the graph and I find myself with hundreds of new objects, which between all the other issues, breaks EF Change Tracking).

What I'm looking for is to be able to clone JUST the object I pass to the mapper, all the references either stay exactly the same, or I handle them manually.

Describe the solution you'd like
The solution I'm looking for is a middleground between UseDeepCloning true and false. Because false outright refuses to do any work and just outputs something like this:

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.1.0")]
        private static partial global::Tables.OfferComponentLTR ToNewComponentLTR(global::Tables.OfferComponentLTR ltr)
        {
            return ltr;
        }

But with Deep Cloning enabled I found myself in the situation where I need to fight Mapperly to "undo" most of the deep cloning of references because I just don't need it.

I'm just expecting a normal mapping like:

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.3.1.0")]
        private static partial global::Tables.OfferComponentLTR ToNewComponentLTR(global::Tables.OfferComponentLTR ltr)
        {
            var target = new Tables.OfferComponentLTR();
            target.FirstProp = source.FirstProp;
            target.SecondProp = source.SecondProp;
            ....
        }

Of course I do expect nothing more than an object with all properties set exactly the same, so even references should be all the same.

Describe alternatives you've considered
I've tried to work with UseDeepCloning enabled but the burden is extremely high, and it basically defeats the simplicity of Mapperly. On my codebase the actual result is way way worse than AutoMapper, to the degree that I mostly need to fight against Mapperly to avoid it to clone parent and child objects.

Also I've considered to wrap "patching" map method manually like this:

    private static OfferComponentServiceProduct ToNewServiceProduct(this OfferComponentServiceProduct product)
    {
        var target = new OfferComponentServiceProduct();
        product.MapServiceProduct(target);
        return target;
    }

And it works fine, but one thing lost here is the support for init-only properties, which must be either handled manually or changed to full auto-properties.

Additional context
I know that one possible solution could be to introduce DTOs, but it would be like asking me to basically change the whole application just to enable mapperly to generate something which can already do.

Without this feature Mapperly lose much of it's appeal for our application.

(tested on Mapperly 4.3.1)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions