General Electric
General Electric

Reputation: 1236

Add query to model in asp.net mvc

My question is, how do I add a custom query to a model in asp.net mvc , all the tutorial I had seen they make the query in the controller.

So far in my reading everything says that the controller should no be aware of the database, all the queries should be made in the model but I can not seem to find any example of this.

I try to do it but I do not have the DbContext available in the model, so how should I do it?

Upvotes: 0

Views: 1816

Answers (2)

Mrchief
Mrchief

Reputation: 76218

There are multiple ways to achieve this and each has its own pros and cons. Here are a few appraoches:

1) Domain Model Pattern
The author of the domain model pattern, Martin Fowler, provides this definition (Fowler, 2003):

An object model of the domain that incorporates both behavior and data.

Using this pattern, your domain models would define behaviors, which may or may not translate into DB queries.

2) Repository Pattern

Use a repository to separate the logic that retrieves the data and maps it to the entity model from the business logic that acts on the model. The business logic should be agnostic to the type of data that comprises the data source layer. For example, the data source layer can be a database, a SharePoint list, or a Web service.

As @Mehrdad has pointed out, using this pattern frees up the controller DB concerns and all your DB logic remains in one place.

3) Command Query Separation pattern (my favorite)

It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.

Note: this is CQS and NOT CQRS pattern

The difference between CQS and Repository pattern is that with Entity Framework, DbContext already wraps up repository pattern for you (DbContext being the Unit of Work and DbSets being the repositories). So creating another repository layer is redundant. What CQS gives you is fine grained control over your queries/commands and allows you to extend them via decorators which can handle additional logic without polluting your core business logic. Here are some great links about CQS:

This answer is a great example of how repository pattern can get be used with CQS together.

All of this can be quite overwhelming, so I suggest that you take your time, implement Proof-OF-Concept projects using these patterns and decide which one suits your overall architecture better.

Upvotes: 1

Nick Mehrdad Babaki
Nick Mehrdad Babaki

Reputation: 12535

You usually can use Repository pattern for that for example if you have an user entity:

public class UserRepository:IUserRepository{
  public List<User> GetUsers()
  {
     //Your code and query here
  }
  public void AddUser(User user)
  {
     //Your code and query here
  }
}

Then you pass this class to your UserController and call it's functions. As you can see I also added IUserRepository so you can use it if you do Dependency Injection

Upvotes: 0

Related Questions