Git Product home page Git Product logo

automapperselfconfig's Introduction

AutoMapper Self Config

DEV MASTER BLEEDING NUGET
CI status Release Build status MyGet CI NuGet CI

Updated to AutoMapper 5 in version 1.0.0

I help create mappings for AutoMapper based on interfaces.

Based on code presented on Pluralsight by @matthoneycutt. He blogs at http://trycatchfail.com/blog.

From version 0.10.0 makes use of AutoMapper new instance based API. Use version 0.9.0 if need to to support AutoMapper 4.1.1

Usage

The library provides 3 interfaces and some helper methods.

Install with the following nuget command:

Install-Package AutoMapper.SelfConfig

IMapFrom

public interface IMapFrom<T>
{}

This is placed on a class to mark that an AutoMapper configuration be created mapping from T to the current class.

IMapTo

public interface IMapTo<T>
{}

This is the reverse of IMapFrom. This is placed on a class to mark that an AutoMapper configuration be created mapping to T from the current class.

The advantage with the 2 way mapping is you can place all mappings on say your view models and leave your ** domain models** free of mapping clutter while still keeping them close at hand with the files you working on rather than off in a seperate mapping Profile.

IHaveCustomMapping

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

UPDATE: Parameter type changed from IConfiguration to IMapperConfiguration from version 0.10.0 UPDATE: Parameter type changed from IConfiguration to IMapperConfiguration from version 1.0.0

This is for when more complex mappings are required and allows you to directly interact with the AutoMapper IConfiguration.

Example Usage

public class Source
{
    public int Nr { get; set; }
    public string Name { get; set; }
}

public class TestEasy : IMapTo<Source>, IMapFrom<Source>
{
    public int Nr { get; set; }
    public string Name { get; set; }
}

Then the mappings are created as such:

var types = new Type[] { typeof(TestEasy) };
MappingLoader.LoadStandardMappings(types);

This would create a 2 mappings in AutoMapper TestEasy -> Source and Source -> TestEasy.

If you need finer control of the mappings above AutoMappers defaults you can use IHaveCustomMApping interface:

public class TestConfig : IHaveCustomMappings
{
    public int Number { get; set; }
    public string Name { get; set; }

    public void CreateMappings(IMapperConfigurationExpression configuration)
    {
        configuration.CreateMap<Source, TestConfig>().ForMember(dest => dest.Number, opt => opt.MapFrom(src => src.Nr));
        configuration.CreateMap<TestConfig, Source>().ForMember(dest => dest.Nr, opt => opt.MapFrom(src => src.Number)); ;
    }
}

Loading both the simple and complex maps is now as simple as:

var types = new Type[] { typeof(TestEasy), typeof(TestConfig) };
MappingLoader.LoadAllMappings(types);

Real World Example

Update: MappingLoader changed to MappingConfigFactory at version 0.10.0

The command class below is taken from a real project and uses the My Reflection Library and My ASP.NET Lifecycle Middleware To load up all mappings at startup of a ASP.NET application.

public class AutoMapperSetupTask : IRunAtStartup
{
    public void Execute()
    {
        var seedTypes = new Type[] { typeof(Startup) };
        var assemblies = Reflect.OnTypes.GetAssemblies(seedTypes);
        var typesInAssemblies = Reflect.OnTypes.GetAllExportedTypes(assemblies);
        AutoMapper.SelfConfig.MappingConfigFactory.LoadAllMappings(typesInAssemblies);
    }
}

automapperselfconfig's People

Contributors

dburriss avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.