Then, we call that Configure() method from the Global.asax file (or elsewhere on application startup).īut how does it actually create that map? AutoMapper uses reflection to look at the properties of the source and destination classes (in the example above, the source class is the Team entity and the destination class is the TeamDTO data transfer object). Often, my team will do this in a static AutoMapperConfiguration class: public static class AutoMapperConfiguration Of course, with that kind of power comes some setup.
Ef select to new dto with icollections code#
Once we've got AutoMapper set up, the mapping code becomes super simple: public TeamDTO GetByID(int id) AutoMapper is a conventions-based mapping system that allows you to copy values from an instance of one class to an instance of another class using pre-defined maps. There must be a better way, and there is: AutoMapper. That sucks to write, it sucks to read, it sucks to maintain. Now we'd end up with a ton of mapping code, like this: public TeamDTO GetByID(int id) Voila! Mapping Entities to DTOs with AutoMapperĮxcept, we weren't quite done. Now we could simply use the TeamDTO and PlayerDTO classes to move data back and forth from our service to the client systems and vice versa. Because we want to use Entity Framework for the data-access layer and WCF for the service layer, we figured we could just use the created Entity Framework Code First classes as DataContracts over WCF. This hub will allow multiple other projects to query for and receive certain data. One of the projects that my team is working on is a major WCF service that will serve as a hub for our company-specific data.