Today, I am going to explain how to use Mapster in asp.net 5 or MVC 6 application. How to a configuration.
It's very simple and it hardly takes only 2 min to configure Mapster in Asp.Net 5 application.
Let's start step by step configuration.
1. Create new Web Application from visual studio 2015 using Asp.Net 5 template.
2. Once a solution is created and ready for work then install Mapster from NuGet package or Package Manger Console
3. Once Mapster installed. Open a startup.cs page from the root.
4. Add Mapster reference on Startup.cs page.
5. Now Goto constructor of Startup.cs page and add Mapster configuration. Refer image for more detail.
6. Once done configuration on startup.cs page then you are ready to use Mapster for object mapping.
7. Let's see an example how to use Mapster after configuration.
8. Added following classes on solution.
1. Under Models folder added Student.cs model
2. Under Services folder added IStudentService.cs and StudentService.cs
3. Under ViewModels folder added StudentViewModel.cs
4. Under Controller folder added StudentController.cs
9. Very simple to use Mapster for object mapper. Just see in StudentController on Index method were student service call the GetAll method which returns all students data.
but before we move ahead I assume you know about Dependancy Injection in Asp.Net 5 which is inbuilt in the framework.
For more detail please refer screenshots.
You can download solution from my repository.
https://github.com/kalpeshsatasiya/MapsterObjectMapper
For more details about Mapster please visit https://github.com/eswann/Mapster.
It's very simple and it hardly takes only 2 min to configure Mapster in Asp.Net 5 application.
Let's start step by step configuration.
1. Create new Web Application from visual studio 2015 using Asp.Net 5 template.
2. Once a solution is created and ready for work then install Mapster from NuGet package or Package Manger Console
install-package Mapster
3. Once Mapster installed. Open a startup.cs page from the root.
4. Add Mapster reference on Startup.cs page.
Using Mapster
5. Now Goto constructor of Startup.cs page and add Mapster configuration. Refer image for more detail.
TypeAdapterConfig.GlobalSettings.Default.PreserveReference(true);
6. Once done configuration on startup.cs page then you are ready to use Mapster for object mapping.
7. Let's see an example how to use Mapster after configuration.
8. Added following classes on solution.
1. Under Models folder added Student.cs model
2. Under Services folder added IStudentService.cs and StudentService.cs
3. Under ViewModels folder added StudentViewModel.cs
4. Under Controller folder added StudentController.cs
9. Very simple to use Mapster for object mapper. Just see in StudentController on Index method were student service call the GetAll method which returns all students data.
but before we move ahead I assume you know about Dependancy Injection in Asp.Net 5 which is inbuilt in the framework.
public IEnumerableIndex() { var students = _studentService.GetAll(); return students.Adapt , IEnumerable
>(); }
For more detail please refer screenshots.
You can download solution from my repository.
https://github.com/kalpeshsatasiya/MapsterObjectMapper
For more details about Mapster please visit https://github.com/eswann/Mapster.
0 comments:
Post a Comment