user8280126
user8280126

Reputation:

MVC Core Bind ViewModel to Model

We have Customer transactional table with multiple lookup tables with foreign keys. We want to see the 3 table joined together and bind the ViewModel back to the Model and reverse. I heard repositories should not access ViewModels.

(a) What is the proper software protocol for doing this? I do not want to use AutoMapper yet. Should I create a data access object or service, and can someone write a quick sample for me below? Should I create another folder called Data Service in MVC? I bought 3 MVC books and none of them discuss DTO or binding models <--> viewmodels. Thanks,

Repository:

void GetByCustomerTransactionId()
{
   var result = from ct in CustomerTransaction
    join pt in ProductType on pt.ProductTypeId equals ct.ProductTypeId 
    join ss in Status on s.StatusId equals ct.StatusId 
    select new all fields
}

Models:

public class CustomerTransaction
{
    public int CustomerTransactionId{ get; set; },
    public int ProductTypeId {get; set; }, //joins to ProductTypeTable
    public int StatusID {get; set; },  //joins to StatusTypeTable
    public string DateOfPurchase{ get; set; },
    public int PurchaseAmount { get; set; },
}

public class ProductType
{
    public int ProductTypeId{ get; set; }
    public string ProductName { get; set; },
    public string ProductDescription { get; set; },
}

public class StatusType
{
    public int StatusId{ get; set; }
    public string StatusName{ get; set; },
    public string Description{ get; set; },

}

ViewModel:

public class CustomerTransactionViewModel
    {
        public int CustomerTransactionId{ get; set; },
        public string ProductName {get; set; }, //joins to ProductTypeTable
        public string ProductDescription {get; set; }, 
        public string StatusName {get; set; },  //joins to StatusTable
        public string DateOfPurchase{ get; set; },
        public int PurchaseAmount { get; set; },
    }

Upvotes: 0

Views: 1420

Answers (2)

JC Cabili
JC Cabili

Reputation: 16

 public class CustomerTransactionViewModel : IViewModel
{
    public int CustomerTransactionId{ get; set; },
    public string ProductName {get; set; }, //joins to ProductTypeTable
    public string ProductDescription {get; set; }, 
    public string StatusName {get; set; },  //joins to StatusTable
    public string DateOfPurchase{ get; set; },
    public int PurchaseAmount { get; set; },
  // Every view model should always have mapper from dbmodel to vm.
  private void MaptoEntity(Entity e)
  {
    this.CustomerTransactionId = e.ID
    .....
    // this is also a repository that mapping db to view model.
    var prod = new ProductTypeViewModel().Load(e.ProductID);
    this.ProductName = prod.ProductName;
    this.ProductDescription = prod.ProductDescription;
    // so on.......
  }

  public bool Load(int id)
  {
   // Call data from DB.
    var entity = dbcontext.Entity.Find(id);
   // Map you from DB.
    this.MaptoEntity(entity)
  }
}

Better structure is create a Interface IViewModel that will require all the implementations.

Now you can call

var customerViewModel = new CustomerTransactionViewModel();
customerViewModel .Load(1);

Upvotes: 0

Herman
Herman

Reputation: 2978

Have you look into AutoMapper is to map database object to view model. Is pretty neat and clean way.

If you do not want to use the AutoMapper, you might want to consider using static method.

var student = new Student { FirstName = "John", LastName = "Doe" };
var mdoel = student.ToDTO();

public static StudentViewModel ToDTO(this Student student)
{
    var model = new StudentViewModel();
    model.FullName = $"{student.FirstName} {student.LastName}";
    return model;
}

Hope this help you

Upvotes: 0

Related Questions