First things first, let’s setup our Profile and call our configure from the global.asax. After reading blogs and sample code, I believe this is the best practice while using AutoMapper. I try to only post best practices, but sometimes I slip up or have a brain fart. So let’s rig this thing up.
Step 1: Create an AutoMapperConfigurator
public class AutoMapperConfigurator
{
public static void Configure()
{
Mapper.Initialize(x => x.AddProfile<MyProfile>());
}
}
Note: I put my AutoMapperConfigurator under Helpers –> Configuration
Step 2: Create the MyProfile class (as mentioned above), which inherits from AutoMapper.Profile
public class MyProfile : Profile
{
public const string VIEW_MODEL = "MyProfileNameHere";
protected override string ProfileName
{
get { return VIEW_MODEL; }
}
protected override void Configure()
{
CreateMaps();
}
private static void CreateMaps()
{
//Create Maps here…we'll get to this in a second
}
}
Step 3: Plug into global.asax
protected void Application_Start()
{
AutoMapperConfigurator.Configure();
}
Okay, we’re all setup, let’s make some maps. I will be mapping the two objects(FeedbackForm & INotificationMessage), which are listed below. The FeedbackForm is part of my view model on my MVC project & INotificationMessage is part of my domain model.
Here is the FeedbackForm:
public class FeedbackForm
{
public string SenderName { get; set; }
public string SenderEmail { get; set; }
public string Subject { get; set; }
public string MessageBody { get; set; }
}
And my INotificationMessage:
public interface INotificationMessage
{
string Subject { get; set; }
string MessageBody { get; set; }
}
private void CreateMaps()
{
//Mapper.CreateMap<FeedbackForm, INotificationMessage>() //BEFORE JIMMY’S COMMENT BELOW
CreateMap<FeedbackForm, INotificationMessage>(); //AFTER JIMMY’S COMMENT
}
UPDATED 12/22/09 8:00PM
According to Jimmy, I should use just CreateMap<> above instead of Mapper.CreateMap and since he’s the creator of AutoMapper, I guess I’ll trust him :) Thanks Jimmy!
That’s it! Done! If my property names didn’t match up exactly, there would be more work to do, but since they do, we’re done! For a GREAT tutorial on AutoMapper, see Jimmy Bogard on dnrTV.
Now let’s use it in our controller!
Step 5: Create Mapper Interface
public interface IFeedbackMapper
{
T Map<T>(FeedbackForm model);
}
Step 6: Implement Mapper Interface
public class FeedbackMapper : IFeedbackMapperThe reason we’re doing this and not just using Mapper.Map in the FeedbackController is that we’re trying to keep all the using statements to be our namespaces and that’s it. Everything else should be wrapped by our code so we’re not depending on 3rd party or Microsoft implementations.
{
public T Map<T>(FeedbackForm model)
{
return Mapper.Map<FeedbackForm, T>(model);
}
}
Step 7: Inject the IFeedbackMapper into the FeedbackController constructor
private readonly IFeedbackMapper _feedbackMapper;
public FeedbackController(IFeedbackMapper feedbackMapper)
{
_feedbackMapper = feedbackMapper;
}
Note: You can inject the feedbackMapper using your favorite IoC tool. See my StructureMap posts for more info or the StructureMap site.
Step 8: Use the _feedbackMapper
[AcceptVerbs(HttpVerbs.Post), ValidateModel(typeof(FeedbackForm)), ValidateAntiForgeryToken]
public ViewResult Index(FeedbackForm form)
{
if (!ModelState.IsValid)
return View("index", form);
var message = _feedbackMapper.Map<NotificationMessage>(form);
//send message via notification service or something and return Success View
}
Step 9: Add more stuff to the mapper and map away!
Please let me know your thoughts! Thanks for reading!