dimanche 24 décembre 2017

AutoMapper : Open - Close Princple

I got the code from the given link

AutoMapper Code for Open Close Princple Code

I am trying to use it in my project but I am stuck due to Static API has been removed from AutoMapper version 4.2.0.

For Reference see this

Please can any one help me how to implement the below code's in latest version of automapper.

1) Mapper.CreateMap(TSource,TDestination)

 private void RegisterStandardMappings(IEnumerable<Type> types)
    {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMapForm<>)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select new
                    {
                        Source = i.GetGenericArguments()[0],
                        Destination = t
                    }).ToArray();

        foreach (var map in maps)
        {
            //Need to optimize below line with current version.
            Mapper.CreateMap(map.Source, map.Destination);
        }
    }

2) Getting IConfiguration as it has been changed to IConfigurationProvider

 private void ReverseCustomMappings(IEnumerable<Type> types)
 {
        var maps = (from t in types
                    from i in t.GetInterfaces()
                    where typeof(IHaveCustomMappings).IsAssignableFrom(t)
                          && !t.IsAbstract
                          && !t.IsInterface
                    select (IHaveCustomMappings)Activator.CreateInstance(t)).ToArray();

        foreach (var map in maps)
        {
            //Need to optimize below line with current version.
            map.CreateMappings(Mapper.Configuration);
        }
  }

public interface IHaveCustomMappings
{
    void CreateMappings(IConfiguration configuration);
}

Please need your suggestion's any help with appreciated.





Aucun commentaire:

Enregistrer un commentaire