aromic1 / Mapper

0 stars 1 forks source link

Comments #3

Closed nathan-chappell closed 1 year ago

nathan-chappell commented 1 year ago

From README.md

About it

both destination type and source type must be types that are assignable from IEnumerable (e.g. List, Array).

But isn't it the case that the source type must be assignable to IEnumerable<object>? Otherwise you wouldn't be able to call the method.

IEnumerable<TDestination> Map<TSource, TDestination>(this IEnumerable<TSource> source)

This way your mapper just maps the items that are enumerated, and is agnostic to the means of enumeration (and hence the way they are enumerated back). Then a user who wants a List<TDestination> can just use ToList. Notice how the signature more closely matches the one there, and that this is an extension method. Then we could have:

var aList = new List<A>() { 1, 2, 3 };
var bList = intList.Map<B>();
B b = null;
try {
  // do some stuff
  // now I think that b is not null
  Map(a, b); // you create a new object and return it to nothing
} catch(Exception e) {
  // handle exceptions
}

// b is still null, now we have the following bug
Console.WriteLine(b.ToString()); // unhandled exception!  WTF!

Configurationless mapping

Things you should know about when using mapper

Motivation and automapper comparison