1

I've created a table called Orders in my SQL Server 2008 db. This table has columns which include dateCreated of type Date, CustomerId, InvoiceTypeId, InvoiceId etc. I also have Invoice, Customer and InvoiceType tables.

I've created a View in my database where I join all these tables together, selecting only the few columns I'm interested in.

In my MVC3 Visual Studio project I've created a ADO.NET Entity Data Model to represent the db view. I've also created a very simple controller. Finally I created a razor view with a WebGrid to display my orders. It works fine.

Is this a approach reasonable?

Lets say I now wish to add to my razor page the ability to add a start date and a end date which can be used to filter the contents of the WebGrid, how might I do this?

I'm guessing there are two ways of doing this.

  1. Load all the data from the db view into the entity model. Then the filtering code would be added perhaps in the razor code. The db should not need to be read each time the filter is changed since all the data is already in the model.

  2. add to the db view some extra sql code, something like WHERE (dateCreated BETWEEN @StartDate AND @EndDate) and then have the model read from the db each time we change the filter. This would be good if there are a large amount of records in the Orders table and we are normally only interested in viewing perhaps the last week or few days. Is this possible to do with the MVC3 approach? How can it be done?

1 Answer 1

2

I wouldn't recommend your approach. Your database view is a only a projection. You can do projections with LINQ. With LINQ you can do various projections and they are easy to maintain.

1) Load all the data from the db view into the entity model. Then the filtering code would be added perhaps in the razor code. The db should not need to be read each time the filter is changed since all the data is already in the model.

Recommended approach would be to build a LINQ query based on your filter criteria and the columns you want to display. Filtering would be done in the database. Let database do what it can do best(Filtering) and let Razor do what it is good at(Presentation).

2) add to the db view some extra sql code, something like WHERE (dateCreated BETWEEN @StartDate AND @EndDate) and then have the model read from the db each time we change the filter. This would be good if there are a large amount of records in the Orders table and we are normally only interested in viewing perhaps the last week or few days. Is this possible to do with the MVC3 approach? How can it be done?

Again date filters can be applied using LINQ and filtering would be done in database.

Sample code

var query = db.Invoices;

if (!string.IsNullOrWhiteSpace(model.CustomerName))
{
     query = query.And(i => i.Customer.Name.StartsWith(model.CustomerName));
}

if (model.InvoiceTypeId != null)
{
     query = query.And(i => i.InvoiceTypeId == model.InvoiceTypeId.Value);
}

return query.Select(i => new InvoiceDto { i.Amount, Customer = i.Customer.Name });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.