8000 Merge objects similar to JS spread operator · Issue #232 · agileobjects/AgileMapper · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Merge objects similar to JS spread operator #232
Open
@artemiusgreat

Description

@artemiusgreat

Is it possible to merge two objects in a way that properties will be copied from source to destination only if in the source they are not NULL? Using JavaScript syntax for spread ... operator or Object.assign() method as an example.

var source = { Name: "X", Items: [ 1, 2, 3, 4, 5 ] }
var destination = { Name: "X", Items: [ 1, 2, 3, 4, 5 ], Count: 5 }

var res1 = { ...destination, ...source }
var res2 = Object.assign(destination, source)

res1 == res2 // True

I was able to achieve similar functionality with Automapper by checking, if the relevant property in the source is NULL, then do not override it in the destination.

var setup = new MapperConfiguration(expression =>
{
  expression
    .CreateMap<DemoModel, DemoModel>()
    .ForAllMembers(o => o.Condition((x, y, memberX, memberY) => memberX is not null));
};

var mapper = setup.CreateMapper();
var source = new DemoModel { Name: "X"};
var destination = new DemoModel { Name: "Y", Items: new[] { 1, 2, 3 }};
var res = mapper.Map(source, destination);

// Expected result is { Name: "X", Items: new[] { 1, 2, 3 }} so only Name property got overridden

Unfortunately, Automapper implements some ridiculously horrible behavior by clearing collections even when they're not supposed to be touched.

Question

Is merging, aka overriding only specific properties, possible in mapper without breaking collections?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0