Automapper "skip" configuration option - automapper

I am looking for a version of the automapper dll that contains the "skip" configuration option as detailed in the link below:
I have downloaded both ver: 1.1 & 2.0 from github but cannot find this feature.
Automapper: Ignore on condition of

There's a Condition option now:
CreateMap<Source, Dest>()
.ForMember(dest => dest.SomeValue, opt => opt.Condition(src => src.Bool));

Related

Automapper raised Error mapping types on upgrade to 11.0.0 (List to Dictionary)

I have a problem on map of property with type List to Dictionary.
this code was success on Automapper 10.1.1 and error raised after upgrade to 11.0.0.
CreateMap<ProcessActorEntity, ProcessActorViewModel>()
.ForMember(dest => dest.Roles,
opt => opt.MapFrom(src => src.ProcessRoles.ToDictionary(x => x.Role.Id, x => x.Role.Title)));
Roles is a List of role entity.
ProcessRoles a is Dictionary.
It's work on AutoMapper V10.1.1, and Not work on V11.0.0
Error mapping types ... Destination Member: Roles
I found the error on the run-time:
In Automapper V10.1.1, no error raised, if Role be null
But in Version V11+, Automapper raised error, is Role is null.
Role is null and It's our fault. but Automapper has different approach in versions.
Role is null and Automapper can not access the Id and Title members.

Automapper map one field differently

I have a class that I want to map to another. All the fields have the same name in both classes, except one.
Is there a quick way to do that, without having to use ForMember for all fields?
Thank you.
Found a solution :
cfg.CreateMap<MasterA, MasterB>().ForMember
(dest => dest.IdOld, opt => opt.MapFrom(src => src.Id));

How Affect Resource Type Declared In Another Puppet Module

I include the class nova::compute::libvirt and this class defines a Package resource like so:
package { 'libvirt-nwfilter':
ensure => present,
name => $::nova::params::libvirt_nwfilter_package_name,
before => Service['libvirt'],
tag => ['openstack', 'nova-support-package'],
}
The problem is that the RPM is in a YUM repo that is not enabled enabled=0.
I could solve this issue by changing nova::conpute::libvirt so that Package resource looked like this:
package { 'libvirt-nwfilter':
ensure => present,
name => $::nova::params::libvirt_nwfilter_package_name,
before => Service['libvirt'],
tag => ['openstack', 'nova-support-package'],
install_options => ['--enablerepo', 'redhat_updates'],
}
But I'd like to not have to modified a module I got from puppet forge because the next time someone else setups up a puppet master they might forget to make the modification. Is there something I can do from the class that includes nova::compute::libvirt?
Resource collectors afford the possibility of overriding attributes of resources declared elsewhere. The syntax would be:
Package<| title == 'libvirt-nwfilter' |> {
install_options => ['--enablerepo', 'redhat_updates']
}
That alternative avoids modifying the repository definition, and it does not require introducing any new ordering relationships. Beware, however, that collectors always realize any matching virtual resources. Beware also that this approach is very powerful, and thus very easy to abuse to get yourself in trouble. With great power comes great responsibility.
You can solve this problem by enabling the redhat_updates yum repo with a yumrepo resource, and then specifying a metaparameter for it to be applied before the class.
yumrepo { "redhat_updates":
baseurl => "baseurl",
descr => "Redhat Updates",
enabled => 1,
gpgcheck => 0,
before => Class['nova::compute::libvirt'],
}
https://docs.puppet.com/puppet/latest/types/yumrepo.html

Issue while Automapper Upgrade to 5.1.1

We were using the automapper v5.0 in our code. In our create map statement, we had a statement ->
m.ResolveUsing((src, o, context) => (CodeObject)context.Options.Items["test"]))
When we upgraded the Automapper version to 5.1.1, we are getting build error in the above statement.
Can you please suggest how to correct this with latest version.
Finally i have got the answer after going through the automapper source code. We can do it this way:
m => m.ResolveUsing((src, o, destMemb, context) => (CodeObject)context.Items["test"]))

Issue with ignoring base class property in child classes mappings using Automapper

I have a scenario where I would like to ignore some properties of classes defined in base class.
I have an initial mapping like this
Mapper.CreateMap<Node, NodeDto>()
.Include<Place, PlaceDto>()
.Include<Asset, AssetDto>();
Then I customised it more like this to ignore one of the properties defined in base class NodeDto
Mapper.CreateMap<Node, NodeDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore());
However when I try to map, Place to PlaceDto or Asset to AssetDto, the ChildNodes property does not get ignored. So I ended up doing soething like this
Mapper.CreateMap<Node, NodeDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore());
Mapper.CreateMap<Place, PlaceDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore());
Mapper.CreateMap<Asset, AssetDto>()
.ForMember(dest => dest.ChildNodes, opt => opt.Ignore());
Since I have lots of child classes for NodeDto, the above process is cumbersome, and I would like to know if there is a better approach?
Thanks
Nabeel
It gets even more cumbersome if you then decide that you want to ignore not just 1, but 2, 3 or maybe even more properties from the base class. It might not help you much in this case and I'm sure 9 months on you've probably found a solution already, but for the benefit of anyone else stumbling across this question an extension method could reduce some of the complexity.
public static class MappingExtensions
{
public static IMappingExpression<Node, NodeDto> MapNodeBase<Node, NodeDto>(
this IMappingExpression<Node, NodeDto> mappingExpression)
{
// Add your additional automapper configuration here
return mappingExpression.ForMember(
dest => dest.ChildNodes,
opt => opt.Ignore()
);
}
}
Which you would then call thus:
Mapper.CreateMap<Node, NodeDto>()
.MapNodeBase()
.Include<Place, PlaceDto>()
.Include<Asset, AssetDto>();
sorry but, no, there isn't any other way, that's how automapper works
There is a better way. For our project we created mappings.xml file with following structure.
<mappings>
<mapping name="EntityOne">
<configuration name="Flat">
<ignore name="ChildCollectionOne"/>
<ignore name="ChildCollectionTwo"/>
<ignore name="ChildCollectionThree"/>
</configuration>
<configuration name="Full">
<include name="ChildCollectionOne" configuration="Flat" type="One"/>
<include name="ChildCollectionTwo" configuration="Flat" type="Two"/>
<include name="ChildCollectionThree" configuration="Flat" type="Three"/>
</configuration>
</mapping>
</mappings>
Special class AutoMapperUtilis is used for parsing data form xml and configuring Automapper according to given rules.
Call is:
AutoMapperUtil.Init(typeof(EntityOne),typeof(EntityOneDto), AutoMapperUtilLoadType.Flat);
After that all required mappings are automatically loaded, and specified ChildCollections are ignored.
With this mappings descriptions we can choose between Flat or Full configuration depending of our Use Case. We are using AutoMapper for mapping between nHibernate entities and Dto`s that are used with Ria Services, and we are very happy with this solution.

Resources