0

In order to make my web-application project I used some tutorials and downloaded 2 sample projects. I noticed that in both of them entities were imported from the SQL server into the project AND models were created for each related class. From what I understand, the entities represent the database itself while the models are the ones to check validations (for example) and after validations successfully passed, the data is sent in to the entities which in turn get it into the database (?) .

In my project I have no models at all. I thought that the entities represent the models, therefore why shall I create duplicate classes (rather entities and models which both look alike). All the data annotations are inside the entities classes. I was told that each change done in the tables inside the SQL server would erase my entire work.

I would like to know whether it's true or not, as well as why do I acctually need models when I have entities instead. If models ARE needed, how do you acctually pass their data into the entities?

Edit: I found this post Difference between model and entity which answers most of my question. I would like to know whether my entire entity class's annotations are erased whenever I change a simple thing in the SQL server. Why wouldn't the model class be an exact duplicate of the entity class (besides the data annotations)?

Community
  • 1
  • 1
Skatz1990
  • 212
  • 7
  • 19

3 Answers3

0

The models represents the database entities. Those models shouldn't be responsible for displaying data in views or validating user input - their only usage is to represent some table in a database as a c# object.

Let say you have model:

[Table("Products", Schema = "product")]
public class Product
{
    public long Id { get; set; }
    public string Name { get; set; }
    public long CategoryId { get; set; }
    public string Description { get; set; }
    public string ManufacturerUrl { get; set; }
    public string ImageUrl { get; set; }
    public decimal Price { get; set; }
    public int Stock { get; set; }

    public Category Category { get; set; }
}

Now to make use of it in your views and add some validation if you like then you should create a so called ViewModel for it which could be something like this:

public class ProductViewModel
{
    public long Id { get; set; }
    public string Name { get; set; }
    public int CategoryId { get; set; }
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }
    [DataType(DataType.Url)]
    [Display(Name = "Producer")]
    public string ManufacturerUrl { get; set; }
    [DataType(DataType.ImageUrl)]
    public string ImageUrl { get; set; }
    [DataType(DataType.Currency)]
    public decimal Price { get; set; }
    [Display(Name = "In stock")]
    public int Stock { get; set; }

    public CategoryViewModel Category { get; set; }
}
rosko
  • 464
  • 3
  • 16
  • So whenever the user POSTs some data, it is posted into this model class and the data is sent into the entities which in turn get it to the DB? – Skatz1990 May 05 '14 at 10:01
  • I think here is some good point about entities and view models - http://stackoverflow.com/questions/5330172/domain-entities-dto-and-view-models you should read it. – rosko May 05 '14 at 10:06
0

Suppose your entity is Person. You decide to have the Entity as the Model, instead of a different class.

In your view (editing an existing person), there is a dropdownlist with country names (Person needs a Country entity). So, that means that you need a list of all possible countries, so the user can select one.

How do you pass that list of countries?

Well, if you have a separate class that you use as the Model, you can easily add a new property, put the list of countries in that, and then get that from your view.

So your viewmodel for this example would be:

public class PersonEditModel
{
    public Person PersonToEdit { get; set; } //This is your entity from before

    public List<Country> Countries { get; set; } //Extra data for the view
}

Think of your model as a sort of 'package' that combines your entity with all other needed information that the view requires. In case there is no extra information required, you could forego the Model and keep using the Entity directly.

But most people label this bad practice. What if you need an extra bit of information suddenly? You'd have to rewrite your code to now start implementing a model.

Long story short: If you have no need for extra data in the view, and you're working on a private project; do whatever you think is best. Professionally, I would suggest always using a Model, if only to make sure you can add extra data in the future.

Flater
  • 12,908
  • 4
  • 39
  • 62
  • Thanks for you comment. So instead of hard-codding the optional countries in the HTML code, you acctually add coutries to the list and then use the above example to show the user his optional countries..? – Skatz1990 May 05 '14 at 10:20
0

Because as you rightly pointed out, your entities will be erased and recreated if you ever refreshed your edmx file.

You could actually lose the model layer if you use partial classes and put your business rules in there, but the point of having a model layer is that it is much simpler to write unit tests for models than for entities.

I suggest that after you've finished learning the basics, you go on and learn about Domain Driven Development (DDD) and Test Driven Development(TDD). All you questions will be answered then, because if I bombard you with all of the theory now you'd probably get lost or think it's much more difficult than it actually is..

Captain Kenpachi
  • 6,960
  • 7
  • 47
  • 68