Note that there are some explanatory texts on larger screens.

plurals
  1. POIs possible simplify generic class?
    primarykey
    data
    text
    <p>Colleagues, In our project we are using AutoMapper to map models.</p> <p>We have a model:</p> <pre><code>public class Location { public string Address { get; set; } } public class Person { public string Name { get; set;} public Collection&lt;Location&gt; Locations { get; set; } } </code></pre> <p>also we have a view model:</p> <pre><code>public class PersonView { public string Name { get; set; } public string Location { get; set;} } </code></pre> <p>To mapping a model to a view model we may define something like the following:</p> <pre><code>Mapper.CreateMap&lt;Person, PersonView&gt;(d=&gt;d.Name, opt=&gt;opt.FromMap(s=&gt;s.Name); Mapper.CreateMap&lt;Person, PersonView&gt;(d=&gt;d.Address, opt=&gt;opt.FromMap(s=&gt;s.Locations.First().Address); </code></pre> <p>BUT: If Locations will not contains elements or is null then we will get an exception.</p> <p>From other side, we may define a function to get a value:</p> <pre><code>Mapper.CreateMap&lt;Person, PersonView&gt;(d=&gt;d.Address, opt=&gt;opt.FromMap(s=&gt; { var item = s.Locations.FirstOrDefault(); if(item == null) { return string.Empty; } return item.Address; }); </code></pre> <p>This expressions hard to read. And I try create a IValueResolver for simplify mapping.</p> <pre><code>public class CollectionItemResolver&lt;TSource, TSourceItem, TResult&gt; where TSource : class where TSourceItem : class { private readonly Func&lt;TSource, IEnumerable&lt;TSourceItem&gt;&gt; _sourceSelector; private readonly Func&lt;TSourceItem, TResult&gt; _selector; private readonly TResult _defaultValue; public CollectionItemResolver(Func&lt;TSource, IEnumerable&lt;TSourceItem&gt;&gt; source, Func&lt;TSourceItem, TResult&gt; selector) : this(source, selector, default(TResult)) { } public CollectionItemResolver(Func&lt;TSource, IEnumerable&lt;TSourceItem&gt;&gt; source, Func&lt;TSourceItem, TResult&gt; selector, TResult defaultValue) { _sourceSelector = source; _selector = selector; _defaultValue = defaultValue; } public TResult Resolve(TSource source) { var items = _sourceSelector(source); if (items == null) { return _defaultValue; } var item = items.FirstOrDefault(); if (item == null) { return _defaultValue; } var value = _selector(item); return value; } } </code></pre> <p>And then use something like this:</p> <pre><code>Mapper.CreateMap&lt;Person, PersonView&gt;(d=&gt;d.Address, opt=&gt;opt.ResolveUsing( new CollectionItemResolver&lt;Person, Location, string&gt;(p=&gt;p.Locations, i=&gt;i.Address))); </code></pre> <p>Is possible simplify generic resolver? For instance do not define the type of nested item?</p> <pre><code>new CollectionItemResolver&lt;Person, string&gt;(p=&gt;p.Locations, i=&gt;i.Address))); </code></pre> <p>Thanks,</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload