so I have found a few questions on SO (for example: Automapper mapping list becomes 0) about automapper returning a list of 0 from a mapping, but none seem quite what I’m looking at.
I have two types:
public class DNSContract : BaseContract
{
public int DoNotSolicitID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Zip4 { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string BusinessName { get; set; }
public string Partner { get; set; }
public string Origination { get; set; }
}
and
public DNS_Entity()
{
// set default values which can be expicity set if needed
InsertDT = DateTime.Now;
InsertDT = DateTime.Now;
// InsertUserID = 999;
Origination = "RDI";
}
public long DoNotSolicitID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Zip4 { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string BusinessName { get; set; }
public string Partner { get; set; }
public string Origination { get; set; }
public Nullable<System.DateTime> InsertDT { get; set; }
//public int InsertUserID { get; set; }
public DateTime? UpdateDT { get; set; }
//public int UpdateUserID { get; set; }
}
pretty easy there. Here’s a “quick map” method I’m using:
public static TToType QuickMap<TFromType, TToType>(this TFromType fromObject, TToType toObject)
where TFromType : class
where TToType : class, new()
{
// Look for an existing map, and if none is found add one.
if (Mapper.FindTypeMapFor(typeof (TFromType), typeof (TToType)) == null)
{
Mapper.CreateMap(typeof (TFromType), typeof (TToType));
}
// Execute the auto map
TToType map = Mapper.Map(fromObject, toObject);
return map;
}
so far so good I guess. However,
TToType map = Mapper.Map(fromObject, toObject);
does nothing. The problem is that this code returns an empty DNSContract when it should return 4 (from my unit test):
using (var scope = dnsWork)
{
scope.Register(this);
var one = WhereInternal(whereClause);
var two = one.ToList();
var three = two.QuickMap(new List<DNSContract>());
return three;
//return WhereInternal(whereClause).ToList().QuickMap(new List<DNSContract>());
}
I’ve broken the call out into one, two, three for sanity’s sake while debugging this. So basically, I have a List and want to return a List and that’s failing.
Something that does work is:
return Mapper.Map(two, new List<DNSContract>());
but I’d like to use the generic method and not have mapping sprinkled throughout the service layers.
With automapper do I need to do anything special for list mappings? I think that’s the problem I’ve mapping types but for some reason the list to typeB is just not working right.
Thanks. This has been annoying me for a couple of weeks now and sort of ignored it, but I need to fix is ASAP.
UPDATE #1: as requested, below is the WhereInternal method in a snippet of its class which is in my DAL and pulls from Entity Framework:
public abstract class EFRepository<T> : IRepository<T> where T : BaseEntity
{
public IUnitOfWork UnitOfWork { get; set; }
private IDbSet<T> _objectset;
private IDbSet<T> ObjectSet
{
get { return _objectset ?? (_objectset = UnitOfWork.Context.Set<T>()); }
}
public IQueryable<T> WhereInternal(Expression<Func<T, bool>> expression)
{
return ObjectSet.Where(expression);
}
}
I don’t think it’s really important in the context, since I convert to a list and then attempt to map.
First, there is a problem in your implementation of QuickMap method – i’m not sure why you want a second argument to be passed at all if you actually just need a type of it. You also call not the most straightforward methods to perform the mappings.
Second, as per documentation, register a mapping only for the simple types, hence I would separate registration and mapping itself.
Here is what I came up with:
And the usage: