Conditional Mapping in AutoMapper 2. Is this possible?

8.152 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Shapper

ungelesen,
09.02.2012, 08:48:3909.02.12
an automapp...@googlegroups.com
Hello,

I am mapping a list as follows:

.ForMember(d => d.Items, o => o.MapFrom(s => s.Items.Select(x => x > 10 }).ToList())

But I would like to map it only if s.Count > 4.

Can't I add a conditional mapping in Automapper 2? Something like:

.ForMember(d => d.Items, o => o.MapFrom(s => s.Items.Select(x => x > 10 }).ToList()).When(o => o.Items.Count() > 4))

Thank you,
Miguel

Sunny

ungelesen,
09.02.2012, 08:55:5009.02.12
an automapp...@googlegroups.com
.ForMember(opt => {
opt.Condition(s=>s.Count>4);
opt.MapFrom(s=>s.Items.Select(x=>x>10));
});

Cheers,
Sunny

> --
> You received this message because you are subscribed to the Google Groups
> "AutoMapper-users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/automapper-users/-/vHEXGAvIjooJ.
> To post to this group, send email to automapp...@googlegroups.com.
> To unsubscribe from this group, send email to
> automapper-use...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/automapper-users?hl=en.

--
Svetoslav Milenov (Sunny)

Artificial Intelligence is no match for natural stupidity.

Shapper

ungelesen,
09.02.2012, 09:08:3809.02.12
an automapp...@googlegroups.com
You mean the following:

  .ForMember(d => d.Items, opt => { opt.Condition(s=>s.Count>4); opt.MapFrom(s=>s.Items.Select(x=>x>10)); });

I keep getting an exception because of null. It only works if I use:

  .ForMember(d => d.Items, opt => opt.MapFrom(s=>s.Items == null ? null : s.Items.Select(x=>x>10)));

Any idea what I am missing? How to translate this do conditional?

Thank You,
Miguel

Sunny

ungelesen,
09.02.2012, 09:35:1009.02.12
an automapp...@googlegroups.com
try this:

.ForMember(opt => {
opt.Condition(s=> s != null && s.Count>4);


opt.MapFrom(s=>s.Items.Select(x=>x>10));
});

Cheers,
Sunny

> --
> You received this message because you are subscribed to the Google Groups
> "AutoMapper-users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/automapper-users/-/BrjEN81qfVIJ.

Shapper

ungelesen,
09.02.2012, 09:47:4609.02.12
an automapp...@googlegroups.com
Sunny,

If I don't add a d => d.Items I get the error "No Overload for method ForMember takes 1 arguments".

But if I add it I still get an error when source is null ...

Shapper

ungelesen,
09.02.2012, 10:02:5709.02.12
an automapp...@googlegroups.com
I tried another example on my application. The following works fine:

.ForMember(d => d.Files,
  o => o.MapFrom(s => s.Files == null ? null : s.Files.Select(x => new FileModel { Data = x.InputStream }).ToList())
);

And the following:

.ForMember(d => d.Files, o => {
    o.Condition(s => s.Files != null);
    o.MapFrom(s => s.Files.Select(x => new FileModel { Data = x.InputStream }).ToList());
});

Gives an error:

  "Value cannot be null.\r\nParameter name: source"

Any idea? I have no idea what I am missing.

Thank You,
Miguel




Sunny

ungelesen,
09.02.2012, 12:12:1909.02.12
an automapp...@googlegroups.com
class MapFrom
{
public List<string> Data { get; set; }
}

class MapTo
{
public string MappedData { get; set; }
}

And use:
.ForMember(d => d.MappedData, opt => opt.MapFrom(s => SafeMap(s)));

private static string SafeMap(MapFrom mapFrom)
{
return mapFrom != null && mapFrom.Data != null
? mapFrom.Data.First()
: null;
}

Cheers

> --
> You received this message because you are subscribed to the Google Groups
> "AutoMapper-users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/automapper-users/-/uBoSrVpH3CkJ.

Fabio Miceli

ungelesen,
10.02.2012, 06:57:0410.02.12
an automapp...@googlegroups.com
grazie infinite

con il tuo suggerimento ho risolto il mio problema.


.ForMember(dest => dest.commesse, o => o.MapFrom(obj => obj.Commesse == null ? null : obj.Commesse))


a risentirci
____________________________________________________________________
2012/2/9 Sunny <slo...@gmail.com>

Jimmy Bogard

ungelesen,
16.02.2012, 00:01:0816.02.12
an automapp...@googlegroups.com
Instead of MapFrom, use ResolveUsing. I improved the error message in 2.1 which I really need to drop :P

Sunny

ungelesen,
16.02.2012, 08:38:0016.02.12
an automapp...@googlegroups.com
For educational purposes: what are the benefits of using ResolveUsing
over MapFrom?

Thanks

Jimmy Bogard

ungelesen,
16.02.2012, 09:06:3616.02.12
an automapp...@googlegroups.com
MapFrom is intended for redirecting source members - things like ForMember(dest => dest.Foo, opt => opt.MapFrom(src => src.Bar)). MapFrom has all of the null-checking that flattening has, so it can be thought of as redirecting the flattening algorithm.

ResolveUsing is for pretty much anything else, any additional custom logic beyond member access. It's a Func<> instead of an Expression<Func<>>, so you don't get null checking.

Sunny

ungelesen,
16.02.2012, 10:29:3916.02.12
an automapp...@googlegroups.com
Still - for practical purposes, what would be the difference between:


private MyClass GetFromString(string key)...(does basic lookup in dict lets say)

....
.ForMemeber(d=>d.MyClassProp, opt=>opt.MapFrom(s=>GetFromString(s.MyClassKey)))

and

.ResolveUsing(d=>d.MyClassProp,
opt=>opt.MapFrom(s=>GetFromString(s.MyClassKey)))

I know that the right thing to do is actually to set mapper <string,
MyClass>, but for various reasons I can not do that. So it leaves me
with the option of using one of the 2 choices above.

So, what would be the difference? Why should I prefer one or the other?

Thanks

Jimmy Bogard

ungelesen,
16.02.2012, 10:42:5516.02.12
an automapp...@googlegroups.com
Well, the first one wouldn't/shouldn't work. It's just about redirecting a member.

The MapFrom in ResolveUsing is about redirecting what is supplied to the resolution function. It doesn't make as much sense in the ResolveUsing with a Func, so that should probably be removed as it's confusing. It's more meant for class-based custom value resolvers.

Sunny

ungelesen,
16.02.2012, 10:50:4916.02.12
an automapp...@googlegroups.com
Actually .MapFrom(...) (the first one) works for me in 2.0.0.232.

And the second example I provide was wrong, I meant compare

.ForMemeber(d=>d.MyClassProp, opt=>opt.MapFrom(s=>GetFromString(s.MyClassKey)))

and

.ForMemeber(d=>d.MyClassProp,
opt=>opt.ResolveUsing(s=>GetFromString(s.MyClassKey)))

Fabio Miceli

ungelesen,
16.02.2012, 12:03:5616.02.12
an automapp...@googlegroups.com
grazie infinite
a presto

2012/2/9 Shapper <mdm...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/automapper-users/-/vHEXGAvIjooJ.

Jimmy Bogard

ungelesen,
16.02.2012, 21:53:0916.02.12
an automapp...@googlegroups.com
The first one is all Expression-based, and the second is Func-based. The first will give you built-in null checking, the second one won't, you're completely on your own.

So you can do opt => opt.MapFrom(src => src.SomeProp.Way.Down.Here.Somewhere) and each level will get checked for nulls (as it already does for flattening).
Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten