riteshrao / ncommon

A framework for implementing commonly used design patterns when using Domain Driven Design

Home Page:www.codeinsanity.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CompositeUserTypes CANNOT Short-circuit NullSafeSet

jbtule opened this issue · comments

Because this implementation is cited as an example across the internet. I feel it's important to point out a data corrupting bug in this generic implementation.

NullSafeSet needs to get called on each composite property with null when value is null.

If you don't and you have a batch save of objects with said composite type, any null value property will get be updated in the DB with the last nonnull value saved

public void NullSafeSet(IDbCommand cmd, object value, int index, ISessionImplementor session)
{
if (value == null)
return;
var propIndex = index;
for (var i = 0; i < _properties.Count; i++)
{
var property = _properties[i];
var propValue = property.GetValue(value, null);
NHibernateUtil.GuessType(property.PropertyType).NullSafeSet(cmd, propValue, propIndex, session);
propIndex++;
}
}