Exploring System.Interactive

A couple of weeks ago i was working on an application that would transfer data through a couple of components as a List<object>. In essence, all we were doing over and over again was the following:

interface IMapper<TEntity>
{
 TEntity FromObjectList(List<object> objectList);
 List<object> ToObjectList(TEntity entity);
}

My initial implementation (using EnumerableEx operators from Reactive Extensions) looked like this:

public TEntity FromObjectList(List<object> objectList)
{
 var entity = new TEntity();

 properties
  .Zip(objectList, (property, value) => AssignValueToProperty(entity, property, value))
  .Run();

 return entity;
}

int AssignValueToProperty(object entity, PropertyInfo property, object value)
{
 property.SetValue(entity, value, null);
 return 0;
}

public List<object> ToObjectList(TEntity entity)
{
 return properties
  .Select(property => property.GetValue(entity, null))
  .ToList();
 }

And the consumer code looks like this:

var person = new Person {  Id = 2, Score = 1.3, Name = "Tim", Title = "Sir" };

var personMapper = new Mapper<Person>()
 .Map(x => x.Id)
 .Map(x => x.Score)
 .Map(x => x.Name)
 .Map(x => x.Title);

var data = personMapper.ToObjectList(person);
var clonedPerson = personMapper.FromObjectList(data);

Wait a minute, in most situations we simply want to map all properties on the object. Let’s create a mapper for this:

class AutoMapper<TEntity> : Mapper<TEntity> where TEntity : new()
{
 public AutoMapper()
 {
  typeof(TEntity).GetProperties().Run(property => Map(property));
 }
}

And now we don’t have to waste time doing the same thing over and over again! Because we always need to map all properties of our types we ended up with the following:

public static class Mapper
{
 public static List<Object> ToObjectsList<TEntity>(this TEntity entity)
 {
  return entity.GetType().GetProperties()
   .Select(property => property.GetValue(entity, null))
   .ToList();
  }

 public static TEntity ToEntity<TEntity>(this List<Object> objectsList) where TEntity : new()
 {
  TEntity entity = new TEntity();
  entity.GetType().GetProperties()
   .Zip(objectsList, (property, value) =>{ property.SetValue(entity, value,null); return 0;})
   .Run();
  return entity;
 }
}
This entry was posted in C#. Bookmark the permalink.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>