views:

267

answers:

4

I'm new to the MVC framework and have just run through the NerdDinner sample project. I'm loving this approach over form-based asp.net.

I'd like to spin of a more sizable side project using this same approach. Do you see anything in that project that would prevent me from enlarging the basic structure to a more complex website?

Examples of things that make me wary: 1) The NerdDinner sample accesses a db of only two tables, my db has around 30. 2) The NerdDinner project uses the LinqToSQL classes directly... all the way from the model, through the controller, to the view... is that kosher for a larger project?

Do you see any other parts of the NerdDinner framework that might cause me future grief?

+1  A: 

There's alot of debate around the internet when it comes to the Linq to Sql classes. Some feel that it's not enough abstraction when you use the classes directly, and some feel that that's what they're there for. At work we starting revamping our site, and we're using MVC. The way we decided to go was basically each one of the LINQ to SQL classes implements an interface. IE:

public partial class LinqToSqlClass //generated class
{
   public int Id{get;set;}
}

interface ILinqToSqlClass
{
   int Id{get;set;}
}

public partial class LinqToSqlClass : ILinqToSqlClass
{

}

This is just a very small part of it. We then have a repository that gets you any of these generated class, but only as that of their interface type. This way, we're never actually working directly with the Linq to Sql classes. There are many many different ways to do this, but generally I would say yes, if you're dealing with a large database (especially if the schema may change) or if you're dealing with data that may come from more than one source, definitely don't use the classes directly.

Bottom line is, there's alot of good info in that Nerd Dinner chapter, but when creating your own project, you'll obviously run into issues of your own so take it as you go.

BFree
+1  A: 

I would add a service layer between the repositories and controllers. The service layer will contain all of your business logic leaving your controllers to deal mainly with processing form inputs and page flow.

Within the repositories I map LinqToSql classes and fields to domain models and then use the domain models within the service layer, controllers and views. For a larger system the extra layers will prove their worth in the long run.

Todd Smith
don't you find that converting to/from domain models to LinqToSql classes, not as such an overhead, but you end up writing a lot more code?
David Liddle
@David yes it requires more code to support the mapping layer and I wouldn't suggest it unless there was a benefit for doing so. Another option would be to use nHibernate but my team wasn't ready for that (steep learning curve). But for our project the extra mapping layer, which was easy to write, payed for itself when we ended up with 3-4 bounded contexts in our design.
Todd Smith
A: 

The Nerd Dinner text makes the claim that the MVC framework can equally well accommodate other common data abstractions. (It's true.) It sounds like your organization already has one it likes. A good learning strategy would probably be to adapt one to the other.

le dorfier
+2  A: 

I agree with others that the model should be the only place you use linq2sql and my little addendum to that is only use linq2sql in models in small projects. For larger sites it might be worth the overhead to create a separate Web Service project that does all the talking to the database and utilize the web service in your Model.

I never fully checked out the Nerd Diner example but other best practices include Typed Views and using a datamodeler that allows for easy validation (see xval or the DataAnnotations model binder). To me these are 2 of the most important best practices/

Stephen Walter has alot of excellent tips on his website that are worth checking out and taking into account when setting up a new MVC project.

Martijn Laarman
FYI: xVal is designed to help with the web client side of things and not for your server side domain model validation.
Todd Smith
Actually it's designed to help with both client and server side validation of the Modelstate see http://blog.codeville.net/2009/01/10/xval-a-validation-framework-for-aspnet-mvc/. It's all about Data validation and not business rules validation even though you could get a long way with the CustomValidation Attribute from DataAnnotations for business rules.
Martijn Laarman