ASP.NET MVC 2 Generated LINQ-to-SQL Classes and Model Validation

I just want to make sure that I understand the best methods for ASP.NET MVC2 with LINQ-TO-SQL correctly.

Please correct me if I am mistaken in any of the following points:

  • LINQ to SQL generates classes for you based on your tables
  • Generated classes are models.
  • These are the models that we should use in representations.
  • If we want to add validation to the models, we will continue the partial class and define the annotation data. Something like this .

As a rule, my question concerns data validation. What I used for is to create a "shadow" model for each of the generated LINQ-to-SQL classes and put the data validation there. Then, in the controllers, I created an instance of the "shadow" model, and also retrieved data (using the repository template). It is mapped from the object to the shadow model and passed to the view. So, something like this (in the controller):

// Car is class generated by LINQ-to-SQL, based on the Car table
// Use repository class to retrieve the data (Car)
Car car = myDatabaseRepository.GetCar(1);

// CarModel is my 'shadow' model, which has all the data validation.
// For example, it makes sure Year is before 2010 (let say).
CarModel carModel = new CarModel();
carModel.Year = car.Year;
carModel.Make = car.Make;
carModel.Model = car.Model;

return View(carModel);

Is it wrong, right (not for pun intended) ? It should only be:

// Car should have been extended (as partial) and data annotation
// should have been in the extended class, rather than a whole separate
// model class.
Car car = myDatabaseRepository.GetCar(1);
return View(car);

Right?

+3
source share
1 answer

, ViewModel (, "" ), UI. , ​​ AutoMapper, , .

. ( + , ). , ViewModels.

+2

Source: https://habr.com/ru/post/1728213/


All Articles