automapper

Automapper many-to-many stackoverflowexception

一世执手 提交于 2019-12-04 10:16:48
I'm getting a stack overflow for the following mapping: Mapper.CreateMap<Parent, ParentViewModel>() .ForMember(x => x.Children, o => o.MapFrom(x => x.Children.ConvertToChildrenViewModel())); Mapper.CreateMap<Children, ChildrenViewModel>() .ForMember(x => x.Parents, o => o.MapFrom(x => x.Parents.ConvertToParentViewModel())); I understand why this is happening, clearly an infinite loop here. How am I supposed to get this to work in automapper? I need parents to know about their children and their children to know about their parents. Would I have to create another ViewModel for Children.Parents

Automapper map custom collections

最后都变了- 提交于 2019-12-04 10:11:07
Hello. I have a list that looks like this one: public class PagedList<T> : List<T> { public PagedList(IEnumerable<T> collection) : base(collection) { } public int TotalItems { get; set; } public int CurrentPage { get; set; } public int PageSize { get; set; } //some other properties } and used in repository for paging public PagedList<TEntity> GetPaged(int page) { var pagedEntities = some_query; return pagedEntities.AsPagedList(totalResults, page, pageSize); } The same PagedList is also used in asp mvc view models for paging. Is it possible to map this collections using Automapper with all the

AutoMapper Map to different type based on an enum?

血红的双手。 提交于 2019-12-04 09:33:11
I'm starting to implement AutoMapper , first I managed to integrate it with Castle.Windsor, which I'm already using. Now I have a Post entity which I want to map to either a LinkPostModel or an ImagePostModel . Both inherit from PostModel 1) This is what I have so far: public class PostModelFromPostEntityConverter : ITypeConverter<Post, PostModel> { private readonly IPostService postService; public PostModelFromPostEntityConverter(IPostService postService) { if (postService == null) { throw new ArgumentNullException("postService"); } this.postService = postService; } public PostModel Convert

How to Initialize AutoMapper Profiles in referenced project DLLs in ASP.Net webapp

限于喜欢 提交于 2019-12-04 09:02:11
Struggling a little on how to use automapper in my project class libraries (dlls). See my structure of my overall solution below. The WebApp fires up, and in Global.asax App Start, the AutoMapper.Configure() method is called to add the mapping profiles. For now I am just adding the Services.AutoMapperViewModelProfile. But I need to somehow account for the profiles in each of the WebStoreAdapters (BigCommerce and Shopify in the example below). I was hoping not to add references to each WebStoreAdapter in WebApp, just for the sake of being able to add the profiles during the AutoMapperConfig. If

How to Transfer DataAnnotation metadata to ViewModel with AutoMapper with Betty's Approach

大兔子大兔子 提交于 2019-12-04 08:54:02
I need clarification on how to implement Betty's code solution to transferring data annotation metadata to ViewModels with AutoMapper (see here ). Or if you have a better way, please share that. Maybe the implementation of Betty's answer is obvious to someone who knows AutoMapper well, but I'm new to it. Here is a simple example, what do I add to this code to make Betty's solution work: // Data model Entity public class User1 { [Required] public int Id { get; set; } [Required] [StringLength(60)] public string FirstName { get; set; } [Required] [StringLength(60)] public string LastName { get;

Can automapper ignore destination if it is not null / only change null fields

拥有回忆 提交于 2019-12-04 07:38:42
Background: I'm working on a webservice that I want to allow input that has a null field to mean, "don't do an update". The input object is very similar but not identical to the database model, so we're using automapper to do the transforms. So in the case of an update, I'd like to be able to take the existing values, use them to override any of the null fields in the input, and then save that to do the whole update. So is there a way to make automapper only put values into the destination if the destination field is null? Yes, it can, but you probably wouldn't want to go through the hassle.

Automapper - Dictionary to Object Mapping not working?

戏子无情 提交于 2019-12-04 07:34:17
问题 I'm trying to convert a dictionary to an object. I've tried the following but it doesnt work: public class FormField { public string FieldName { get; set;} public string FieldValue { get; set;} } var formData = new List<FormField> { new FormField {FieldName = "date", FieldValue = "2017-09-14"}, new FormField {FieldName = "name", FieldValue = "Job blogs"}, new FormField {FieldName = "isenabled", FieldValue = "true"} }; public class MyViewModel { [Required] public DateTime Date { get; set; } =

abp学习(二)

萝らか妹 提交于 2019-12-04 06:29:20
翻译下首页截图的标签: DDD Base Classes 介绍: 应用程序代码库的分层是一种被广泛接受的技术,可帮助降低复杂性并提高代码重用性。为了实现分层架构,ASP.NET样板遵循域驱动设计的原则。 Domain Driven Design Layers 域驱动设计 (DDD) 中有四个基本层: 表示层:为用户提供接口。使用应用程序层实现用户交互。 应用程序层:在演示文稿层和域层之间起中介作用。协调业务对象以执行特定的应用程序任务。 领域层:包括业务对象及其规则。这是应用程序的核心。 基础设施层:提供通用技术功能,主要使用第三方库支持更高层。 总结:可以点进去看,这里只写了很少的一部分,具体页面还要一个很大的图片,并且配有讲解。 Repositories 存储库模式"使用类似于集合的接口访问域对象,在域和数据映射层之间进行中介"(马丁·福勒)。 实际上,存储库用于对域对象(实体和值类型)执行数据库操作。通常,每个实体(或聚合根)都使用单独的存储库。 在ASP.NET样板,仓库类实现 IRepository<TEntity, TPrimaryKey> interface。 ABP可以自动创建为每个实体类型的默认库。 您可以直接 inject IRepository<TEntity> (or IRepository<TEntity, TPrimaryKey>).

Automapper - Inheritance mapper not working with type converter

我与影子孤独终老i 提交于 2019-12-04 05:50:12
问题 Can't use Mapping Inheritance and TypeConverter together. I have this code: /* BaseClassTypeConverter.cs */ public class BaseClassTypeConverter : ITypeConverter<SourceClass, BaseClass> { public BaseClass Convert(ResolutionContext context) { if (context == null || context.IsSourceValueNull) return null; var src = (SourceClass)context.SourceValue; return new BaseClass() { CommonAttr = src.SourceAttr }; } } /* AutoMapperConfig.cs */ public static class AutoMapperConfig { public static void

Cannot map from ViewModel to ApplicationUser in AutoMapper 5

女生的网名这么多〃 提交于 2019-12-04 05:11:53
问题 I have a Student class inherited from ApplicationUser base class (ASP.NET Identity) and there is a ViewModel of it called StudentViewModel as shown below: Entity Classes: public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>, IUser<int> { public string Name { get; set; } public string Surname { get; set; } //code omitted for brevity } public class Student: ApplicationUser { public int? Number { get; set; } } ViewModel: public class