【问题标题】:Automapper returning count of 0 from List map?Automapper 从列表映射返回计数为 0?
【发布时间】:2011-12-14 16:07:26
【问题描述】:

所以我在 SO(例如:Automapper mapping list becomes 0)上发现了一些关于 automapper 从映射中返回 0 列表的问题,但似乎没有一个是我所关注的。

我有两种类型:

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; }
}

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; }
}

那里很容易。这是我正在使用的“快速地图”方法:

 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;
    }

我猜到目前为止一切都很好。然而,

TToType map = Mapper.Map(fromObject, toObject);

什么都不做。问题是这段代码在应该返回 4 时返回一个空的 DNSContract(来自我的单元测试):

 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>());
        }

在调试时,为了保持理智,我已将调用分为一、二、三。所以基本上,我有一个列表,想返回一个列表,但失败了。

有效的方法是:

return Mapper.Map(two, new List<DNSContract>());

但我想使用通用方法,而不是在整个服务层中散布映射。

使用 automapper 是否需要为列表映射做一些特殊的事情?我认为这是我映射类型的问题,但由于某种原因,类型 B 的列表无法正常工作。

谢谢。这已经让我烦恼了几个星期,并且有点忽略了它,但我需要尽快解决。

更新 #1:根据要求,以下是其类的 sn-p 中的 WhereInternal 方法,该方法位于我的 DAL 中并从实体框架中提取:

 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);
    }
}

我认为这在上下文中并不重要,因为我转换为列表然后尝试映射。

【问题讨论】:

  • 请提交 WhereInternal(whereClause) 返回什么 - 我怀疑 IEnumerable

标签: c# automapper


【解决方案1】:

首先,您的 QuickMap 方法的实现存在问题 - 如果您实际上只需要第二个参数的 type,我不确定为什么要传递第二个参数。您还调用不是最直接的方法来执行映射。

其次,根据documentation,只为简单类型注册一个映射,因此我会将注册和映射本身分开。 这是我想出的:

static class MapperHelper
{    
    static void Register<TSource, TDestination>()
    {
        var mapped = Mapper.FindTypeMapFor(typeof(TSource), typeof(TDestination));
        if (mapped == null)
        {
            var expression = Mapper.CreateMap<TSource, TDestination>();
        }
    }
    static TDestination QuickMap<TSource, TDestination>(this TSource source)
    {
         return Mapper.Map<TSource, TDestination>(source);
    }
}

及用法:

//Registration
MapperHelper.Register<DNS_Entity, DNSContract>();
//Mapping
var result = WhereInternal(whereClause).ToList().QuickMap<IList<DNS_Entity>, IList<DNSContract>>();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2013-05-09
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多