user18046
user18046

Reputation: 25

mapping regular class objects to MVC model

we are converting asp.net to MVC. we have a framework and it returns regular class objects or datasets. what is the best way to map class object to MVC model? please let me know.

for example,

//framework class
public class ISTApplicant
{
   public ISTApplicant GetApplicant()
   { 
       ISTApplicant objApplicant = new ISTApplicant();
       return objApplicant;
   }

   public Dataset GetApplicants()
   {
       return ds;
   }
}

MVC Model

public class ApplicantModel
{
    public string CustomerNo { get; set; }
    public string LastName { get; set; }
    public List<ApplicantModel> LstApplicantModel { get; set; }
}

Controller class

public class ApplicantController : Controller
{
    //how can i assign framework object to my model, something like this.  
    public ActionResult GetApplicant()
    {  
        ApplicantModel model = myframework.Applicant.GetApplicant();
        return mode;
    }

    public ActionResult GetApplicants()
    {
         //if it is dataset what is the best way to handle?
         LstApplicantModel list =  myframework.GetApplicants();
         return list;
    }
}

you might see some errors since I typed it.

Upvotes: 1

Views: 1764

Answers (2)

Mauro Bilotti
Mauro Bilotti

Reputation: 6262

There are a few things in your code that will not compile. Also, be aware of the indent spaces in order to make your code more readable.

Assuming that you have created an action in your controller properly and ISTApplicant has the same fields than ApplicantModel you can do something like this:

In your Startup.cs file:

Mapper.Initialize(cfg => {
    cfg.CreateMap<ISTApplicant, ApplicantModel>();
});

Then, in your controller:

public ActionResult GetApplicant() 
{
    ISTApplicant db = myframework.Applicant.GetApplicant();
    ApplicantModel model = Mapper.Map<ApplicantModel>(db);
    return model;
}

The above example is using Automapper. Please look how to configure it and how to add the dependency on each controller properly.

I didn't modify the example by adding the list property that you mentioned because you should create another action to do this with similar logic involved.

Hope it helps!

Upvotes: 1

Ben Wesson
Ben Wesson

Reputation: 639

This is exactly the problem that AutoMapper solves: https://automapper.org/

If you define your new MVC models to use the same property names & types as your existing framework models then you can easily transform one to the other, otherwise it's simple enough to define explicit mappings for individual properties.

The documentation at the above link is pretty good, so no need to repeat it here, but it's worth pointing out that mapping configurations are directional in terms of 'from' and 'to' object types.

For example if you are returning objects from a database and then submitting updated versions from the MVC application, you will need separate mappings to go from type A to type B and also from type B back to type A.

Upvotes: 1

Related Questions