我已经开始使用https://github.com/robconery/massive进行项目,我想知道是否有任何映射工具允许支持动态到静态类型映射?
我之前使用过AutoMapper,AutoMapper是否支持此功能?
我知道AutoMapper中的DynamicMap函数,但我相信这个函数用于运行地图而不先创建Map.在我下面的例子中,它不起作用.
dynamic curUser = users.GetSingleUser(UserID);
var retUser = Mapper.DynamicMap<UserModel>(curUser);
users.GetSingleUser(UserID); // returns a dynamic object
Run Code Online (Sandbox Code Playgroud) 假设我有以下实体(类)
public class Target
{
public string Value;
}
public class Source
{
public string Value1;
public string Value2;
}
Run Code Online (Sandbox Code Playgroud)
现在我想配置自动映射,如果Value1以"A"开头,则将Value1映射到Value,否则我想将Value2映射到Value.
这是我到目前为止:
Mapper
.CreateMap<Source,Target>()
.ForMember(t => t.Value,
o =>
{
o.Condition(s =>
s.Value1.StartsWith("A"));
o.MapFrom(s => s.Value1);
<<***But then how do I supply the negative clause!?***>>
})
Run Code Online (Sandbox Code Playgroud)
然而,我仍然不知道的部分是如果在早期条件失败时告诉AutoMapper 采取s.Value2措施.
在我看来,API的设计并不像它可能......但可能是我缺乏知识妨碍了.
我一直在尝试使用AutoMapper来节省从我的DTO到我的域对象的时间,但是我在配置地图时遇到了麻烦,以至于它工作正常,我开始怀疑AutoMapper是否可能是错误的工具工作.
考虑这个域对象的例子(一个实体和一个值):
public class Person
{
public string Name { get; set; }
public StreetAddress Address { get; set; }
}
public class StreetAddress
{
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的DTO(来自Linq-to-SQL对象)的出现大致如下:
public class PersonDTO
{
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; } …Run Code Online (Sandbox Code Playgroud) 我在使用AutoMapper(这是一项出色的技术)时遇到了一个问题,即将业务对象映射到DTO,在DTO中我从集合中的抽象基类继承.
这是我的对象:
abstract class Payment
class CashPayment : Payment
class CreditCardPayment : Payment
Run Code Online (Sandbox Code Playgroud)
我还有一个发票对象,其中包含一系列付款,如下所示:
public class Invoice
{
... properties...
public ICollection<Payment> Payments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我还有每个这些对象的相应DTO版本.
DtoInvoice对象定义为:
[DataContract]
public class DtoInvoice
{
...properties...
[DataMember]
public List<DtoPayment> Payments { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这就是我的Mapper定义:
Mapper.CreateMap<Invoice, DtoInvoice>();
Mapper.CreateMap<Payment, DtoPayment>()
.Include<CashPayment, DtoCashPayment>()
.Include<CreditCardPayment, DtoCreditCardPayment>();
Mapper.CreateMap<CashPayment, DtoCashPayment>();
Mapper.CreateMap<CreditCardPayment, DtoCreditCardPayment>();
Run Code Online (Sandbox Code Playgroud)
执行映射的代码如下所示:
var invoice = repo.GetInvoice(invoiceId);
var dtoInvoice = Mapper.Map<Invoice, DtoInvoice>(invoice);
Run Code Online (Sandbox Code Playgroud)
因此,例如,如果我的发票对象包含特定付款的集合(比如1现金和1张信用卡),当映射器尝试映射它们时,我会收到错误,即无法创建抽象类付款.如果我从Payment对象中删除了abstract关键字,那么代码可以工作,但我只收到Payment对象的集合,我没有得到他们的特定对象(现金和信用卡付款).
所以问题是:如何让AutoMapper映射特定的支付类型而不是基类?
更新
我做了一些挖掘,并认为我发现了一个问题,但我不确定如何使用AutoMapper解决这个问题.我认为这更像是EF的事情,而不是AutoMapper的错.:-)
在我的代码中,我使用延迟加载的Entity Framework 4代理POCO.
因此,当我尝试映射从EF返回的实体(代理POCO)时,它会获得如下滑稽的类型:
System.Data.Entity.DynamicProxies.CashPayment_86783D165755C316A2F58A4343EEC4842907C5539AF24F0E64AEF498B15105C2
Run Code Online (Sandbox Code Playgroud)
所以我的理论是,当AutoMapper尝试将CashPayment映射到DtoCashPayment并且传入的付款属于代理类型时,AutoMapper将其视为"不匹配",然后映射通用付款类型.但由于Payment是一个抽象类AutoMapper炸弹,其中包含"System.InvalidOperationException:无法创建抽象类的实例".例外.
所以问题是:我有没有办法使用AutoMapper将EF POCO代理对象映射到Dtos.
这可能是一个愚蠢的问题!(n00b到AutoMapper和时间短!)
我想使用AutoMapper从EF4实体映射到ViewModel类.
1)如果我打电话
CreateMap<ModelClass, ViewModelClass>()
Run Code Online (Sandbox Code Playgroud)
然后我也需要打电话
CreateMap<ViewModelClass, ModelClass>()
Run Code Online (Sandbox Code Playgroud)
执行相反的操作?
2)如果两个类具有相同的属性名称,那么我是否需要一个CreateMap语句,或者这只是针对"特定/自定义"映射?
有这种情况:
public class Base { public string Name; }
public Class ClassA :Base { public int32 Number; }
public Class ClassB :Base { public string Description;}
public Class DTO {
public string Name;
public int32 Number;
public string Description;
}
Run Code Online (Sandbox Code Playgroud)
我的IList<Base>
地图是:
AutoMapper.Mapper.CreateMap<IList<Base>, IList<DTO>>()
.ForMember(dest => dest.Number, opt => opt.Ignore())
.ForMember(dest => dest.Description, opt => opt.Ignore());
AutoMapper.Mapper.CreateMap<ClassA, DTo>()
.ForMember(dest => dest.Description, opt => opt.Ignore());
AutoMapper.Mapper.CreateMap<ClassB, DTO>()
.ForMember(dest => dest.Number, opt => opt.Ignore())
Mapper.AssertConfigurationIsValid(); //Is OK!
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时,不会映射ClassA或ClassB中的属性:
IList<DTO>= AutoMapper.Mapper.Map<IList<Base>,IList<DTO>>(baseList);
Run Code Online (Sandbox Code Playgroud)
如何映射在 …
我有一个实体:
public class Tag {
public int Id { get; set; }
public string Word { get; set; }
// other properties...
// and a collection of blogposts:
public ICollection<Post> Posts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和模型:
public class TagModel {
public int Id { get; set; }
public string Word { get; set; }
// other properties...
// and a collection of blogposts:
public int PostsCount { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我查询这样的实体(通过EF或NH):
var tagsAnon = …Run Code Online (Sandbox Code Playgroud) 我试图第一次将Automapper引入应用程序,但我一直收到一个错误,说我有一些无效的参数.
我的模特:
namespace StoreGradesLib.Models
{
public class Store
{
[Key]
public int StoreID { get; set; }
[Required]
[MaxLength(120)]
public string StoreName { get; set; }
[Required]
[MaxLength(20)]
public string StoreNumber { get; set; }
[Required]
[MaxLength(120)]
public string ManagerName { get; set; }
[Required]
public long PhoneNumber { get; set; }
[Required]
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
[Required]
public string Postcode { get; set; }
[Required]
public int WallArea { …Run Code Online (Sandbox Code Playgroud) 我更新了我的项目以使用Automapper 3.0.0,现在我的TFS构建没有成功.错误如下:
" ... System.PlatformNotSupportedException:System.PlatformNotSupportedException:此平台上的IMapperRegistry不支持此类型. "
有没有人可以帮我解决这个问题.与此同时,我将恢复到以前的版本,因为那个似乎工作正常.
我在我的ASP.NET MVC网站中使用AutoMapper将我的数据库对象映射到ViewModel对象,我试图使用几个配置文件来映射相同的类型,但使用另一个逻辑.我想通过阅读Matt的博客文章这样做,他说:
真正关键的部分是AutoMapper配置文件.您可以使用配置文件对配置进 也许在一个配置文件中,您可以通过一种方式格式化日期,而在另一种配置文 我在这里只使用一个配置文件.
所以我为一个案例创建了一个配置文件:
public class MyProfile : Profile
{
protected override string ProfileName
{
get
{
return "MyProfile";
}
}
protected override void Configure()
{
CreateMap<DateTime, String>().ConvertUsing<StringFromDateTimeTypeConverter>();
}
}
public class StringFromDateTimeTypeConverter : ITypeConverter<DateTime, String>
{
public string Convert(DateTime source)
{
return source.ToString("dd/mm/yyyy", CultureInfo.InvariantCulture);
}
}
Run Code Online (Sandbox Code Playgroud)
还有另一个案例:
public class MyProfile2 : Profile
{
protected override string ProfileName
{
get
{
return "MyProfile2";
}
}
protected override void Configure()
{
CreateMap<DateTime, String>().ConvertUsing<AnotherStringFromDateTimeTypeConverter>();
}
}
public class …Run Code Online (Sandbox Code Playgroud) automapper ×10
c# ×6
mapping ×2
.net ×1
automapper-2 ×1
automapper-3 ×1
collections ×1
inheritance ×1
lambda ×1
massive ×1
model ×1
poco ×1
viewmodel ×1