Switch branches/tags
Nothing to show
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
Heroic.AutoMapper.csproj
HeroicAutoMapperConfigurator.cs
IHaveCustomMappings.cs
IMapFrom.cs
IMapTo.cs
Readme.md
license.txt

Readme.md

Heroic.AutoMapper

Heroic.AutoMapper

Now with .NET Core support!

Be a hero! Cleaner configuration for AutoMapper! Just implement one of the mapping interfaces in your view model, and you're set!

Note that you can use Heroic.AutoMapper with any type of project. It no longer depends on ASP.NET!

Installation

Install via nuget:

PM> Install-Package Heroic.AutoMapper

Configuration

Just call HeroicAutoMapperConfigurator.Configure during your application's startup.

ASP.NET MVC 5 and Web API 2

For "classic" versions of ASP.NET, you should call HeroicAutoMapperConfigurator in your Global.asax:

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            HeroicAutoMapperConfigurator.LoadMapsFromAssemblyContainingTypeAndReferencedAssemblies<HomeController>();
        }
    }

Console Application Setup

For a simple console application, you might do something like this:

using Heroic.AutoMapper;

public class Program 
{
	public static void Main() 
	{
		HeroicAutoMapperConfigurator.LoadMapsFromCallerAndReferencedAssemblies();

		Console.WriteLine("All set!");
	}
}

ASP.NET Core

For an ASP.NET Core application, you should configure Heroic.AutoMapper in your Startup class, like so:

//..
using Heroic.AutoMapper;

namespace YourNamespace
{
    public class Startup
    {
        //.. snip

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader().AllowCredentials());

            app.UseAuthentication();

            app.UseMvc();

			HeroicAutoMapperConfigurator.LoadMapsFromAssemblyContainingTypeAndReferencedAssemblies<WidgetModel>();
        }
    }
}

Usage

Want to map your view model from a domain model? Just implement the IMapFrom<T> interface!

	public class CustomerRiskViewModel : IMapFrom<Risk>
	{
		public string Title { get; set; }

		public string Description { get; set; }
	}

Need to map a model back to your domain instead? You can implement IMapTo<T> instead:

	public class AddCustomerForm : IMapTo<Customer>
	{
		[Required, Display(Name = "Full Name", Prompt = "Full Name (ex: John Doe)...")]
		public string Name { get; set; }

		[Required, DataType(DataType.EmailAddress)]
		public string WorkEmail { get; set; }
	}

But what if you need to customize your mappings? Implement IHaveCustomMappings instead:

	public class ProfileForm : IMapFrom<User>, IHaveCustomMappings
	{
		public string FullName { get; set; }

		public string EmailAddress { get; set; }

		public void CreateMappings(IMapperConfiguration configuration)
		{
			configuration.CreateMap<User, ProfileForm>()
				.ForMember(d => d.FullName, opt => opt.MapFrom(s => s.UserName))
				.ForMember(d => d.EmailAddress, opt => opt.MapFrom(s => s.Email));
		}
	}

Want more?

Check out the rest of the Heroic Framework!