0

I have a Separate Business Layer project (Class library) and it has a entity class like this.

public class Company
{
public int Id{get;set}
public Name string {get;set;}
}

I've reference this dll to MVC project and used this company class as model class for the view. Is this a good practice to use Entity class as model or is there any good way to implement this.

3 Answers3

0

Your MVC project must contain separate Model than entities. The purpose of model is to store the logic and all the validations. Your entity and model class may or may not be same. But all the validations and logic must be contained in model class for MVC applications. This can help you to change your entity or model depending upon your needs. They will remain loosely coupled.

The best practices for mvc applications that have been in use are available in MSDN

https://blogs.msdn.microsoft.com/aspnetue/2010/09/17/best-practices-for-asp-net-mvc/

https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/overview/understanding-models-views-and-controllers-cs#understanding-models

Varun Setia
  • 165
  • 1
  • 11
0

It is usually a bad practice for middle to big size .net MVC projects to use Model entities in the view. Consider creating separate models for the View. And using Automapper to cast your Business layer models to View layer Models.

Here is some useful information about difference between Models and ViewModels: https://cpratt.co/entities-are-not-models/

vlod11
  • 21
  • 4
0

If you are planning for multi tire application.. please find my comments below.

  1. ASP.NET MVC designed based on "Convention over Configuration" pattern. So don't try mix the concepts. I wont harm the project, but will cause major headache during maintenance.

  2. what you have done is a safe practice, by moving out the entity out of web project and keep it as a separate class library. ("Separation of concern" - SOLID principles.)

  3. In model, do the client side validation, and with Entity, perform the business validations and data handling from persistent stores (databases)

References:

Adersh M
  • 596
  • 3
  • 19