我有两个类(MVC视图模型),它继承自一个抽象基类.
abstract class BaseModel { }
class Car : BaseModel
{
public string Speed { get; set; }
}
class Camper : BaseModel
{
public int Beds { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
并希望使用基类配置AutoMapper,如:
Mapper.CreateMap<BaseModel, DataDestination>();
var someObj = new DataDastination();
Mapper.Map(instanceOfBaseModel, someObj);
Run Code Online (Sandbox Code Playgroud)
在这里我得到错误,因为Automapper没有Car或Camper的配置.尝试使用以下内容配置Automapper:
Mapper.CreateMap<BaseModel, DataDestination>()
.ForMember(dest => dest.SomeProp, mapper => mapper.MapFrom( .... ));
Run Code Online (Sandbox Code Playgroud)
在MapFrom中,我只看到基类的属性!如何配置Automapper以使用BaseClass,以及Car和Camper的特定ForMember表达式?例如,如果它是Car,则从此映射此属性,如果它是Camper,则从其他位置映射此属性.
我想测试方法中的自定义逻辑CreateMap.我不是要测试的映射是否都存在某些类型.
我该怎么做或者我需要知道的课程是什么.我很感激每一个提示文件.自动映射单元测试似乎非常罕见......
public class UnitProfile : Profile
{
protected override void Configure()
{
// Here I create my maps with custom logic that needs to be tested
CreateMap<Unit, UnitTreeViewModel>()
.ForMember(dest => dest.IsFolder, o => o.MapFrom(src => src.UnitTypeState == UnitType.Folder ? true : false));
CreateMap<CreateUnitViewModel, Unit>()
.ForMember(dest => dest.UnitTypeState, o => o.MapFrom(src => (UnitType)Enum.ToObject(typeof(UnitType), src.SelectedFolderTypeId)));
}
}
Run Code Online (Sandbox Code Playgroud) 如果object a有一个名为'Id' b的属性,而对象有一个名为'ID'的属性,AutoMapper会正确映射这两个属性(不进行.ForMember(...)调用)吗?
我想使用以下内容将页面的业务对象列表映射到视图模型对象的分页列表:
var listViewModel = _mappingEngine.Map<IPagedList<RequestForQuote>, IPagedList<RequestForQuoteViewModel>>(requestForQuotes);
Run Code Online (Sandbox Code Playgroud)
分页列表实现类似于Rob Conery在此处的实现:http: //blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
如何设置Automapper来执行此操作?
我正在尝试实体框架代码第一个CTP4.假设我有:
public class Parent
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Child
{
public int Id { get; set; }
public string Name { get; set; }
public Parent Mother { get; set; }
}
public class TestContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
}
public class ChildEdit
{
public int Id { get; set; }
public string …Run Code Online (Sandbox Code Playgroud) 我一直在寻找一些用于映射object-object的scala流畅API,类似于AutoMapper.Scala中有这样的工具吗?
我一直在研究如何使用继承,AutoMapper但我正努力让它完全运行Linq.这是我的代码:
我在这里定义了我的映射:
CreateMap<Article, ArticleDetailsViewModel>()
.Include<Article, ArticleNewsItemDetailsViewModel();
CreateMap<Article, ArticleNewsItemDetailsViewModel>();
Run Code Online (Sandbox Code Playgroud)
ArticleDetailsViewModel是一个基类ArticleNewsItemDetailsViewModel.
现在问题在于,如果我有:
CreateMap<ArticleNewsItem, ArticleNewsItemDetailsViewModel>();
Run Code Online (Sandbox Code Playgroud)
视图模型中的所有属性都将自动映射,因为它们与Linq对象相同.但是,因为我使用Article => ArticleNewsItemDetailsViewModel映射这是不可能的,相反,我必须将每个定义为:
.ForMember(x => x.Property1, opt => opt.MapFrom(src => src.ArticleNewsItem.Property1)
Run Code Online (Sandbox Code Playgroud)
我想过将所有属性从ArticleNewsItemDetailsViewModel一个新的视图模型中移出并让该类成为一个属性ArticleNewsItemDetailsViewModel,只要这两个对象之间存在映射,它就可以工作,但感觉不是很干净.
有什么方法可以避免这样做吗?
我有一个叫另一个服务的服务.这两个服务都使用"相同的类".这些类被命名为相同且具有相同的属性但具有不同的命名空间,因此我需要使用AutoMapper将其中一种类型映射到另一种类型.
不,这很简单,因为我所要做的就是CreateMap<>,但问题是我们有大约数百个类,我手动需要编写CreateMap<>它,并且它的工作连接到我.没有任何自动CreateMap功能.因此,如果我说CreateMap()然后AutoMapper通过组织工作并找到所有类并自动执行CreateMap这些类和它的子类等等...
希望有一个简单的解决方案,或者我想一些反思可以解决它...
我使用EF Code First创建了具有彼此集合的类.实体:
public class Field
{
public int Id { get; set; }
public string Name { get; set; }
public virtual List<AppUser> Teachers { get; set; }
public Field()
{
Teachers = new List<AppUser>();
}
}
public class AppUser
{
public int Id { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string UserName => Email;
public virtual List<Field> Fields { get; set; }
public AppUser()
{
Fields …Run Code Online (Sandbox Code Playgroud) 我不知道如何IValueResolver在新版本的AutoMapper中使用新界面.也许我在以前版本的AutoMapper中使用它们不正确......
我有很多模型类,其中一些是使用sqlmetal从几个数据库服务器上的几个数据库生成的.
其中一些类具有字符串属性,PublicationCode用于标识订阅,商品或发票或其他任何内容所属的发布.
该发布可以存在于两个系统(旧系统和新系统)中的任何一个系统中,因此我在目标模型类上有一个bool属性,用于指示发布是在旧系统还是新系统中.
使用AutoMapper的旧版本(<5?),我使用的ValueResolver<string, bool>,其采取了PublicationCode作为输入参数,并且返回一个bool指示出版物(旧的或新的系统)的位置.
使用AutoMapper的新版本(5+?),这似乎不再可能.新的IValueResolver需要我拥有的源模型和目标模型的每个组合的唯一实现,src.PublicationCode需要将其解析为dst.IsInNewSystem.
我只是试图以错误的方式使用值解析器吗?有没有更好的办法?我想使用解析器的主要原因是我更喜欢将服务注入到构造函数中,而不必DependencyResolver在代码中使用等(我使用的是Autofac).
目前,我通过以下方式使用它:
// Class from Linq-to-SQL, non-related properties removed.
public class FindCustomerServiceSellOffers {
public string PublicationCode { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我拥有的几个数据模型类之一,它包含一个PublicationCode属性).此特定类映射到此视图模型:
public class SalesPitchViewModel {
public bool IsInNewSystem { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这两个类的映射定义是(其中expression是IProfileExpression),删除了不相关的映射:
expression.CreateMap<FindCustomerServiceSellOffers, SalesPitchViewModel>()
.ForMember(d => d.IsInNewSystem, o => o.ResolveUsing<PublicationSystemResolver>().FromMember(s => s.PublicationCode));
Run Code Online (Sandbox Code Playgroud)
解析器:
public class PublicationSystemResolver : ValueResolver<string, bool>
{
private readonly PublicationService _publicationService; …Run Code Online (Sandbox Code Playgroud) automapper ×10
c# ×7
.net ×2
asp.net-mvc ×1
automapper-5 ×1
linq ×1
mapper ×1
mapping ×1
scala ×1
unit-testing ×1