samedi 28 janvier 2017

How to meet this requirement?

I am not sure if the question right fits here.(Sorry!!)

I am in need to write WebApi controllers. The task is to migrate from exisitng normal mvc controller to WebApi controllers.

In this projects, almost all the beauty of the programming is used like:-

  • Design Patterns - Repository + UOW, Factory + nUnit
  • Dependency Injection
  • SOLID

This is my model in the MyApp.Domain layer

public class Customer
{
   //please note that we are using Repository pattern & so no Data Annotations like Required, Key are not used here. 
   public int CustomerID { get; set;}

   public string CustomerName { get; set;}

   public string EmailAddress { get; set;}
   // & so on
}

In the MyApp.UI layer, exists ViewModel for validation & then passing the model to Service layer that. So this is how my MVC controllers looks like

  public class CustomerVM
  {
     [Required]
     public int CustomerID { get; set;}  // & so on the other properties.

  }


  public ActionResult Registration(Customer VM)
  {
         if(Modelstate.IsValid)
         {
            //call service layer 
         }
         else
         {

         }
  }

Now my immediate task is to migrate this controller to WebApi controller. Henceforth, I created a separate project as MyApp.WebApi

Now my doubt is how should I pass the model to this WebApi controller.

I am thinking to detach the ViewModel from UI layer to separate project as MyApp.ViewModels and put all the viewmodels in this layer & reference the dll in UI layer & WebApi layer.

 public string POST([FromBody]CustomerVM customer)
 {
    if(Modelstate.IsValid)
    {
        //call the other service layer which will take care of DB handling
       return "Success";
    }
    else
    {
       return "Error";
    }   
 }

Is this the right way to do & any other right way to do this??

Aucun commentaire:

Enregistrer un commentaire