0

I am new to MVC. According to MVC tutorial, Model are the classes which contains business logic. But in all the example which i referred, Model contains only the declaration (using interface). Why the Model cannot contain definition of business logic. Since i compared with MVVM model, where Model contains definition.

Why model look like this?

public interface IDBModel
{
    void addRecord();
    void deleteRecord();
}

Instead of like below.,

public Class DBModel
{
  void addRecord()
  {
       // Insert logic
  }

  void deleteRecord()
  {
       // Delete logic
  }
} 

Kindly help me to understand the "Model" purpose in MVC and MVVM with some real time examples.

3 Answers 3

1

A model is meant to encapsulate data, making it easier to transfer from different logical areas of your application. The first example you give is incorrect, in that you're defining an interface with methods. You're more likely to see a model that looks like this:

public class Person {
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName() {
        return string.Format("{0} {1}", FirstName, LastName);
    }
}

Notice that I'm using properties as a way to transfer data, but have a method that performs lightweight logic (this could have also been done as a read only property). 90% of the time this is what your models will look like.

3
  • So model contains only the definition, then why we are using interface to declare the function, properties in MVC patterns? Jul 7, 2014 at 6:57
  • The model just contains properties to encapsulate and describe your data. You don't have to use interfaces to describe them (though you could). Jul 8, 2014 at 2:44
  • Based on what you're trying to do, you may want to setup a repository pattern side library to be used from the controller. Jul 8, 2014 at 2:45
1

I would treat the M in MVC more like a view model. It contains all properties and formatting logic needed for the view to display itself. No need to have interfaces for it.

The controller is responsible for building that view model based on the models it receives from the services.

1

I think you misunderstood about the Model.

Worng: Model are the classes which contains business logic (not the business logic).

Models: Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in SQL Server.

Take a look at official ASP.NET MVC Site.

Why model look like this?

Your application may follow certain different patters. other than MVVM.

Real Time Examples / Basic Understandings : Click Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.