Tag: #automapper

AutoMapper Map如果不为空,否则自定义转换

这是我的代码: Mapper.CreateMap() .ForMember(dest => dest.Bar, opt => opt.MapFrom(src => src.Bar == null ? new BarViewModel() : src.Bar)) 基本上,“BarViewModel”有一个无参数的ctor,可以在类中设置属性。 所以我想对AutoMapper说: 如果值为null,则使用ctor作为类。 否则使用您已有的映射 以上是给我一个C#编译器错误。 而且我猜测演员阵容也不会奏效。 那么有一个AutoMapper技巧吗? 最糟糕的情况我可以删除该属性的映射,只是这样做: var mapped = Mapper.Map(src); if (mapped.Bar == null) mapped.Bar = new BarViewModel(); 但这有点难看。 想法?

如何让AutoMapper不缓存映射对象?

当AutoMapper遇到已经映射的对象时,它似乎再次使用该对象,而不是尝试重新映射它。 我相信它是基于.Equals() 。 我有一棵正在映射的树。 所以,一个具有一些属性的节点和子节点。 不止一个节点具有相同的.Equals()值,因为它基于Id属性。 节点的子节点是不同的,我需要重新映射,但它使用缓存的映射值。 有没有办法关闭缓存的映射? 我能想到的只是实现一个新的转换器,但这完全违背了使用AutoMapper的目的。 这是一个如何重现的例子。 void Main() { var source = new List { new Tag { Id = 1, Name = “Tag 1”, ChildTags = new List { new Tag { Id = 2, Name = “Tag 2”, ChildTags = new List { new Tag {Id = 3, Name […]

Automapper可以映射分页列表吗?

我想使用以下内容将页面的业务对象列表映射到视图模型对象的分页列表: var listViewModel = _mappingEngine.Map<IPagedList, IPagedList>(requestForQuotes); 分页列表实现类似于Rob Conery在此处的实现: http : //blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/ 如何设置Automapper来执行此操作?

使用Automapper映射ViewModel之后我应该测试什么?

我试图测试控制器的Index操作。 该操作使用AutoMapper将域Customer对象映射到视图模型TestCustomerForm 。 虽然这有效,但我担心测试我从Index操作中获得的结果的最佳方法。 控制器的索引操作如下所示: public ActionResult Index() { TestCustomerForm cust = Mapper.Map(_repository.GetCustomerByLogin(CurrentUserLoginName)); return View(cust); } 它的TestMethod看起来像这样: [TestMethod] public void IndexShouldReturnCustomerWithMachines() { // arrange var customer = SetupCustomerForRepository(); // gets a boiler plate customer var testController = CreateTestController(); // act ViewResult result = testController.Index() as ViewResult; // assert Assert.AreEqual(customer.MachineList.Count(), (result.ViewData.Model as TestCustomerForm).MachineList.Count()); } 在CreateTestController方法中,我使用Rhino.Mocks来模拟客户存储库并将其设置为从SetupCustomerForRepository返回客户。 通过这种方式,我知道当Index操作调用_repository.GetCustomerByLogin(CurrentUserLoginName)时,存储库将返回目标客户。 […]

从视图模型到域模型的最佳映射位置在哪里?

从视图模型到域模型的映射的最佳位置在哪里? 通过映射,我的意思是从我的EditGrantApplicationViewModel到GrantApplication对象。 让我们说我有以下动作方法(部分代码): [HttpPost] public ActionResult Create(EditGrantApplicationViewModel editGrantApplicationViewModel) { if (!ModelState.IsValid) { return View(“Create”, editGrantApplicationViewModel); } return View(“Index”); } 我是否需要将editGrantApplicationViewModel传递给服务层方法并在方法中进行映射?

用于双向映射的简单约定自动映射(来自/来自ViewModels的实体)

更新:这个东西已经演变成一个很好的项目,请访问http://valueinjecter.codeplex.com 检查一下,我刚写了一个简单的自动播放器,它从属性中获取具有相同名称和类型的一个对象的值并将其放入另一个对象中,并且可以为您可能需要的每种类型添加exception(ifs,switch) 那么告诉我你怎么看? 我这样做了所以我可以这样做: Product –> ProductDTO ProductDTO –> Product 它是如何开始的: 我在Dropsow的Inputs / Dto / ViewModels中使用“object”类型,因为我向html发送了一个IEnumerable ,我收到一个选定键的字符串数组 public void Map(object a, object b) { var pp = a.GetType().GetProperties(); foreach (var pa in pp) { var value = pa.GetValue(a, null); // property with the same name in b var pb = b.GetType().GetProperty(pa.Name); if (pb == null) […]

需要加速自动化…需要32秒才能完成113个对象

嗨我有一些自动映射器的主要问题,它很慢。 我不知道如何加快速度。 我正在使用nhibernate,流利的nhibernate和asp.net mvc 3.0 [Serializable()] public class Test { public virtual int Id { get; private set; } public virtual string Name { get; set; } public virtual string Description { get; set; } public virtual DateTimeDate { get; set; } public virtual IList Reminders { get; set; } public virtual IList Reminders2 { […]

entity framework+ AutoMapper(实体到DTO和DTO到实体)

我在使用EF和AutoMapper时遇到了一些问题。 = / 例如 : 我有2个相关实体(客户和订单),他们是DTO课程: class CustomerDTO { public string CustomerID {get;set;} public string CustomerName {get;set;} public IList Orders {get;set;} } class OrderDTO { public string OrderID {get;set;} public string OrderDetails {get;set;} public CustomerDTO Customers {get;set;} } //when mapping Entity to DTO the code works Customers cust = getCustomer(id); Mapper.CreateMap(); Mapper.CreateMap(); CustomerDTO custDTO = […]

自动映射 – 字典到对象映射不起作用?

我正在尝试将字典转换为对象。 我尝试了以下但它不起作用: public class FormField { public string FieldName { get; set;} public string FieldValue { get; set;} } var formData = new List { new FormField {FieldName = “date”, FieldValue = “2017-09-14”}, new FormField {FieldName = “name”, FieldValue = “Job blogs”}, new FormField {FieldName = “isenabled”, FieldValue = “true”} }; public class MyViewModel { […]

在c#中映射两个类

我有两节课 public class foo1 { public int id; public string image_link; public string sale_price; } 和 public class foo2 { public int Id; public string ImageLink; public string SalePrice } 属性值仅因下划线和案例而异。 我需要映射这两个类。 现在我正在尝试这样的事情及其工作: //var b = object of foo2 var a = new foo1{ a.id = b.Id, a.image_link = b.ImageLink, a.sale_price = b.SalePrice } 我听说过AutoMapper,但我还不清楚我将如何使用它,或者忽略其中的案例或下划线的选项。 […]