I've been looking over how to use Inheritance in AutoMapper but I'm struggling to get it working fully with Linq. Here is my code:
I have defined my mappings here:
CreateMap<Article, ArticleDetailsViewModel>()
.Include<Article, ArticleNewsItemDetailsViewModel();
CreateMap<Article, ArticleNewsItemDetailsViewModel>();
ArticleDetailsViewModel is a base class of ArticleNewsItemDetailsViewModel.
Now here lies the problem, if I had:
CreateMap<ArticleNewsItem, ArticleNewsItemDetailsViewModel>();
All of the properties in the view model would automatically map because they are the same name as their Linq object counterpart. However, because I am using the Article => ArticleNewsItemDetailsViewModel mapping this is not possible, instead I would have to define each one as:
.ForMember(x => x.Property1, opt => opt.MapFrom(src => src.ArticleNewsItem.Property1)
I thought about moving all properties from ArticleNewsItemDetailsViewModel into a new view model and having that class a property within the ArticleNewsItemDetailsViewModel and as long as there is a mapping between those two objects then it will work, but it doesn't feel very clean.
Is there any way to avoid having to do this?
Supposing you have the following classes:
public class Article
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public ArticleNewsItem ArticleNewsItem { get; set; }
}
public class ArticleDetailsViewModel
{
public string Prop1 { get; set; }
}
public class ArticleNewsItemDetailsViewModel : ArticleDetailsViewModel
{
public string Prop2 { get; set; }
public string Prop3 { get; set; }
}
public class ArticleNewsItem
{
public string Prop3 { get; set; }
}
The mapping should look like below:
var res = Mapper.Map<Article, ArticleNewsItemDetailsViewModel>(_article);
Mapper.Map(_article.ArticleNewsItem, res);
Moreover you can create custom type converter to avoid writing these two lines every time you need to map Article to ArticleNewsItemDetailsViewModel.
Apologies if I am over simplifying this in my head but can't you simply add the direct mapping you mention:
CreateMap<ArticleNewsItem, ArticleNewsItemDetailsViewModel>();
To me this is the simplest and cleanest solution...
EDIT
Sorry, I misunderstood. You can't map an object to a nested property without creating a custom map via .ConstructUsing() or .ConvertUsing() methods (or doing it the untidy way)...
Mapper.CreateMap<Article, ArticleNewsItemDetailsViewModel>().ConstructUsing(ConstructItem)
..Then create your method to build the ArticleNewsItemDetailsViewModel...
private static ArticleNewsItemDetailsViewModel ConstructItem(Article source)
{
var newsItem = new ArticleNewsItem
{
Prop1 = source.Prop1,
Prop2 = source.Prop2
};
var result = new ArticleNewsItemDetailsViewModel()
{
ArticleNewsItem = newsItem
};
return result;
}
However I would still recommend re implementing your solution so you are mapping 'like for like'. Here is a good example: http://automapper.codeplex.com/wikipage?title=Nested%20Mappings
Assuming all the required properties are in Article you could create a Custom Value Resolver to do this e.g.
public class ArticleNewsItemResolver : ValueResolver<Article, ArticleNewsItem>
{
protected override ArticleNewsItem ResolveCore(Article source)
{
return Mapper.DynamicMap<Article, ArticleNewsItem>(source);
}
}
...
CreateMap<Article, ArticleNewsItemDetailsViewModel>()
.ForMember(src => src.NewsItem, opt => opt.ResolveUsing<ArticleNewsItemResolver>());
Related
I have a bunch of DTO classes that inherit from this CardBase:
// base class
public class CardBase
{
public int TransId {get; set; }
public string UserId { get; set; }
public int Shift { get; set; }
}
// one of the concrete classes
public class SetNewCardSettings : CardBase
{
// specific properties ...
}
In my MVC project I have a bunch of view models with a AuditVm complex type that has the same properties of CardBase:
public class AuditVm
{
public int TransId {get; set; }
public string UserId { get; set; }
public int Shift { get; set; }
}
public class CreateCardVm : CardVm
{
// specific properties here ...
public AuditVm Audit { get; set }
}
Those view models cannot inherit from AuditVm because each of them already has a parent. I thought I could setup my mapping like below so I would not have to specify the map from AuditVm to the CardBase for every view model that has AuditVm as a complex type. But it is not working. How do I properly map from a complex type to a flatten type with properties on the base class?
Mapper.CreateMap<AuditorVm, CardBase>()
.Include<AuditorVm, SetNewCardSettings>();
// this does not work because it ignores my properties that I map in the second mapping
// if I delete the ignore it says my config is not valid
Mapper.CreateMap<AuditorVm, SetNewCardSettings>()
.ForMember(dest => dest.Temp, opt => opt.Ignore())
.ForMember(dest => dest.Time, opt => opt.Ignore());
Mapper.CreateMap<CreateCardVm, SetNewCardSettings>()
// this gives me an error
.ForMember(dest => dest, opt => opt.MapFrom(src => Mapper.Map<AuditorVm, SetNewCardSettings>(src.Auditor)));
// I also tried this and it works, but it does not map my specific properties on SetNewCardSettings
//.ConvertUsing(dest => Mapper.Map<AuditorVm, SetNewCardSettings>(dest.Auditor));
UPDATE:
here is the fiddle https://dotnetfiddle.net/iccpE0
.Include is for a very specific case--you have two identically-structured class hierarchies you'd like to map, for example:
public class AEntity : Entity { }
public class BEntity : Entity { }
public class AViewModel : ViewModel { }
public class BViewModel : ViewModel { }
Mapper.CreateMap<Entity, ViewModel>()
.Include<AEntity, AViewModel>()
.Include<BEntity, BViewModel>();
// Then map AEntity and BEntity as well.
So unless you have this kind of situation, .Include isn't the right thing to use.
I think your best bet is to use ConstructUsing:
Mapper.CreateMap<AuditVm, CardBase>();
Mapper.CreateMap<AuditVm, SetNewCardSettings>()
.ConstructUsing(src =>
{
SetNewCardSettings settings = new SetNewCardSettings();
Mapper.Map<AuditVm, CardBase>(src, settings);
return settings;
})
.IgnoreUnmappedProperties();
Mapper.CreateMap<CreateCardVm, SetNewCardSettings>()
.ConstructUsing(src => Mapper.Map<SetNewCardSettings>(src.Audit))
.IgnoreUnmappedProperties();
I've also incorporated this answer's extension method to ignore all unmapped properties. Since we're using ConstructUsing, AutoMapper doesn't know that we've already taken care of those properties.
Updated fiddle: https://dotnetfiddle.net/6ZfZ3z
This one takes a little explaining. I have a set of types such that;
public class Child
{
public int ID { get; set;}
}
public class MayHaveChild
{
public Child Value { get; set; }
public int MayID { get; set; }
}
public class MustNotHaveChild { get; set; }
{
public List<MayHaveChild> MayValues { get; set; }
}
In the above scenario, I want any mapping of MayHaveChild to have the values for the Child object, except when I have mapped MustNotHaveChild. E.g.;
When I have
//...some code
MayHave obj = Mapper.Map<MayHaveChild>(childObj);
// I want to be able to access obj.Child.ID
But when I have
//...some code
MustNotHave obj = Mapper.Map<MustNotHaveChild>(notHaveObj);
// I want to be able to access obj.MayValues[0].MayID but
// *not* obj.MayValues[0].Value
I've been through the automapper documention on nesting, polymorphism, lists, etc and I can't find anything that quite matches what I want.
I could solve this by having a inheriting the MayHave class to a MustNotHave variant but this would involve changing quite a lot of existing code. Is there a way to configure Automapper in the manner I need?
I couldn't find a way to configure AutoMapper the way I wanted without going down the inheritance route - though this proved less problematic than I thought. I did something like the following;
public class NoChild : MayHaveChild
{
}
public class MustNotHaveChild { get; set; }
{
// \/--datatype change here
public List<NoChild> MayValues { get; set; }
}
Then, later in the AutoMapper config;
Mapper.CreateMap<MayHave, NoChild>()
.ForMember(c => c.Child, opt => opt.Ignore());
I have the following entity:
public class SampleClass
{
public int Id { get; set; }
public object Args {get; set; }
}
Because Args can be of different types and doesnt need to be queryable, I want to store it in the Database as a json string.
I know the following workaround would solve my problem:
public class SampleClass
{
public int Id { get; set; }
public object Args { get { return Json.Deserialize(ArgsJson); } set { ArgsJson = Json.Serialize(value); } }
public string ArgsJson {get; set; }
}
But this is pretty ugly as it exposes information not related to the model and it contains logic again not related to the model.
What I would like to do, is something like that:
public class SampleClassMapper : EntityTypeConfiguration<SampleClass>
{
public SampleClassMapper()
{
this.Property(e => e.Args).MapAs<string>(arg => Json.Serialize(arg), str => Json.Deserialize(str));
}
}
Is there any cool way of doing so?
(I'm using .Net 4.0 with EntityFramework 5 and Sql Server 2008 if it helps)
The way that you do is the only one available for now in EF. Currently EF Code First don't have any easy way to change the object serialization but this can be done modifying the EDMX file at runtime.
I have a viewmodel that needs to display a certain IEnumerable field as semicolon-separated textbox. At first I thought of using DefaultModelBinder to transform it, but I had trouble thinking how to achieve it in both directions (dto <-> viewmodel).
Nicknames is the field I'm trying to display as one textbox separated by semicolon.
public class Parent
{
public IEnumerable<Child> Children { get; set; }
}
public class Child
{
public IEnumerable<string> Nicknames { get; set; }
}
So I decided to try AutoMapper, I created two ViewModels:
public class ParentViewModel
{
public IEnumerable<ChildViewModel> Children { get; set; }
}
public class ChildViewModel
{
public string Nicknames { get; set; }
}
Then, I created mappings, like this for the children (omitted the other-way conversion for brevity)
Mapper.CreateMap<Child, ChildViewModel>().ForMember(
d => d.Nicknames, o => o.ResolveUsing<ListToStringConverter>().FromMember(s => s.Nicknames);
Then, for the parent, created a naive map (again, omitted the other-way)
Mapper.CreateMap<Parent, ParentViewModel>();
I truly expected the child mappings occur automatically, but they don't, I've already created too much "proper" code to solve a really simple problem which in any other simpler/older non-MVC environment, I'd be done with a long time ago :) How can I proceed and tell AutoMapper to transform the children without writing another "children member resolver".
Have I overthought this and there's a simpler way?
Thank you!
try
Mapper.CreateMap<Parent, ParentViewModel>();
Mapper.CreateMap<Child, ChildViewModel>();
var v = Mapper.Map<Parent, ParentViewModel>(parent);
Found this solution https://stackoverflow.com/a/7555977/1586498, that works for me:
Mapper.CreateMap<ParentDto, Parent>()
.ForMember(m => m.Children, o => o.Ignore()) // To avoid automapping attempt
.AfterMap((p,o) => { o.Children = ToISet<ChildDto, Child>(p.Children); });
The ToISet function is defined in the above link.
Simpler examples 'just work' in LinqPad - so more investigation is required.
A complete listing of a working program:
public class Child{ public string Name {get; set; }}
public class ChildDto{ public string NickName {get; set; }}
public class Parent{ public virtual IEnumerable<Child> Children {get; set; }}
public class ParentDto{ public IEnumerable<ChildDto> Kids {get; set; }}
private static void Main()
{
AutoMapper.Mapper.CreateMap<Parent, ParentDto>().ForMember(d=>d.Kids, opt=>opt.MapFrom(src=>src.Children));
AutoMapper.Mapper.CreateMap<Child, ChildDto>().ForMember(d=>d.NickName, opt=>opt.MapFrom(src=>src.Name));
var pList = new HashSet<Parent>{
new Parent{ Children = new HashSet<Child>{new Child{Name="1"}, new Child{Name="2"}}},
new Parent{ Children = new HashSet<Child>{new Child{Name="3"}, new Child{Name="4"}}},
};
var parentVm = AutoMapper.Mapper.Map<IEnumerable<Parent>, IEnumerable<ParentDto>>(pList);
parentVm.Dump();
}
A deep model is code-generated with lots of arrays (think WCF proxy genererated code based on a wsdl) that needs to be filled with a flattened viewmodel. There are no naming conventions between the 2 models.
The flat model looks for example like this:
public class ViewModel
{
public string Item1 { get; set; }
public string Item2 { get; set; }
}
The deep model looks for example like this:
public class DeepLevel0
{
public DeepLevel1 Level1 { get; set; }
}
public class DeepLevel1
{
public string Prop1;
public DeepLevel2[] Level2 { get; set; }
}
public class DeepLevel2
{
public string Prop2;
public string Prop3;
}
The end mapping result should be the following
DeepLevel0.Level1.Prop1 = ViewModel.Item1
DeepLevel0.Level1.Level2[0].Prop2 = ViewModel.Item2
DeepLevel0.Level1.Level2[0].Prop2 = null;
I really like the validation system in AutoMapper, knowing that you tackled all properties.
I got the following working (but loosing the validation):
Mapper.CreateMap<ViewModel, DeepLevel0>()
.ForMember(d => d.Level1, opt => opt.MapFrom(s =>
new DeepLevel1 {
Prop1 = s.Item1,
Level2 = new[]
{
new DeepLevel2
{
Prop2 = s.Item2,
Prop3 = null
}
}
}));
}
Is there an other better way ?
No I don't think so. You can always switch to using a constructor for the DeepLevel objects which might tidy them up a bit.