Skip to content

Instantly share code, notes, and snippets.

@kobi
Last active August 29, 2015 14:20
Show Gist options
  • Save kobi/d52dd1ff27541acaae10 to your computer and use it in GitHub Desktop.
Save kobi/d52dd1ff27541acaae10 to your computer and use it in GitHub Desktop.
Example output for Stack Overflow question: How to simplify repeating if-then-assign construction? - http://stackoverflow.com/a/29918452/7586
// !!!
// !!! Do not modify this file, it is automatically generated. Change the .tt file instead. !!!
// !!!
namespace Your.Mapper
{
partial class Mapper
{
/// <summary>
/// Set <paramref name="target"/> properties by copying them from <paramref name="source"/>.
/// </summary>
/// <remarks>Mapping:<br/>
/// <see cref="Product.Name"/> → <see cref="ProductEntity.Name"/> <br/>
/// <see cref="Product.Id"/> → <see cref="ProductEntity.ServiceId"/> <br/>
/// </remarks>
/// <returns><c>true</c> if any property was changed, <c>false</c> if all properties were the same.</returns>
public bool ModifyExistingEntity(Product source, ProductEntity target)
{
bool dirty = false;
if (target.Name != source.Name)
{
dirty = true;
target.Name = source.Name;
}
if (target.ServiceId != source.Id)
{
dirty = true;
target.ServiceId = source.Id;
}
return dirty;
}
/// <summary>
/// Set <paramref name="target"/> properties by copying them from <paramref name="source"/>.
/// </summary>
/// <remarks>Mapping:<br/>
/// <see cref="Person.Employee"/> → <see cref="DbPerson.Employee"/> <br/>
/// <see cref="Person.Name"/> → <see cref="DbPerson.FullName"/> <br/>
/// <see cref="Person.Addredd"/> → <see cref="DbPerson.HomeAddress"/> <br/>
/// </remarks>
/// <returns><c>true</c> if any property was changed, <c>false</c> if all properties were the same.</returns>
public bool ModifyExistingEntity(Person source, DbPerson target)
{
bool dirty = false;
if (target.Employee != source.Employee)
{
dirty = true;
target.Employee = source.Employee;
}
if (target.FullName != source.Name)
{
dirty = true;
target.FullName = source.Name;
}
if (target.HomeAddress != source.Addredd)
{
dirty = true;
target.HomeAddress = source.Addredd;
}
return dirty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment