stopcode.net

Dave Knipper Was Here

Autostart Automapper within a WCF Service Application

Chances are you know what Automapper is, but if not, here's the description from their site:

"AutoMapper is a simple little library built to solve a deceptively complex problem - getting rid of code that mapped one object to another. This type of code is rather dreary and boring to write, so why not invent a tool to do it for us?"

Well, they did invent tool.

Whenever you use the tool to map entities, it must be initialized. To initialize it, you must call code similar to this: This issue is that you don't want to call this map creator everytime you are about to perform a mapping between entities. Usually this is fairly straightforward, you just call map creator somewhere where your application starts. It's a little trickier in a WCF application. We want this only called once upon first service method call on a fresh application pool refresh. Here's how:

1. Setup your entities. You probably have them setup already, but here's an example anyway: 2. Setup your service behavior. This is where the magic takes place the initializes the automapper. 3. Setup your service. You'll see the a class decoration on the service class. This is basically how the service knows how to acheive the goal we are striving for upon construction. That's it!