-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
This library takes two objects of the same type and performs a merge based on the supplied values within the object graph. Each merge is customizable and the merge behaviour can be overridden.
The primary use cases for this library are when you have a current object state and you need to merge in partial new data (e.g., if the user submitted new details or updates but didn’t have to fill out the full details).
Let’s say that we have a simple class like so;
class Person
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
And in our DB, our first record has values such that;
ID = 1
FirstName = "Joe"
LastName = "Soap"
However, later our user wanted to update just their name in our versioned database and only the FirstName
was updated. The model supplied from the UI now looks like this;
ID = 0
FirstName = "Joseph"
LastName = null
We want to merge these two models so that only FirstName
is updated. Using MergeO, we can create a default merger for our objects like so;
using MergeO.Contracts;
public Person Foo(Person original, Person updatedLastName)
{
var merger = new Merger(); // default merge style will not overwrite with null or default values
return = merger.MergeItems(original, updatedLastName);
}
The resulting merged model will now look like;
ID = 1
FirstName = "Joseph"
LastName = "Soap"
A history of objects and also be merged, not just 2. If we have 3 instances of Person
such that there is an original record and two subsequent updates like so;
var original = new Person
{
ID = 1,
FirstName = "Joe",
LastName = "Soap"
};
var firstUpdate = new Person { FirstName = "Joseph" };
var secondUpdate = new Person { LastName = "Soappee" };
var history = new List<Person>(new [] { original, firstUpdate, secondUpdate });
The call to Merge.Merge(history)
will result in the following values in a new Person
instance;
ID = 1
FirstName = "Joseph"
LastName = "Soappee"