views:

431

answers:

4

Does LINQ substitute the presentation model?

I read in the book "ASP.NET MVC in Action" about presentation models.

I wonder why to build a presentation model?!

For instance, projecting a domain object (entity) by creating a new class at runtime via LINQ is in my opinion more comfortable than creating dozens of presentation objects.

So what do you think? Maybe someone of you use both :o

Technologies I would prefere are ASP.NET MVC and NHibernate with LINQ or the Entity Framework.

+3  A: 

Linq does not substitute for the presentation model. Nor does Linq to SQL, if that is what you are referring to (which it probably is). Rather, Linq to SQL maps database tables to C# or VB classes, so that you can work with the data directly in your code.

A presentation model (or View Model in ASP.NET MVC) is a code class that is used to decouple your view from the data model or business classes. The View Model allows you to put things like validation and view logic into it, without it cluttering up your view.

Remember that Domain-Driven-Design (DDD) at its core is really just a method for establishing a common vocabulary (the "ubiquitous language") between you and your customer so that the process of design is made easier and more accurate.

Be sure you check out the NerdDinner tutorial at nerddinnerbook.s3.amazonaws.com/Intro.htm. View Models are discussed in detail at nerddinnerbook.s3.amazonaws.com/Part6.htm

Robert Harvey
I wonder why shall I use presentation objects when I can implement methods which provide projecting for domain objects. One aim of the presentation model is projecting isn't it?
Rookian
If you are referring to ASP.NET MVC, you can put these methods (for projecting data into the view) into the controller. The presentation objects (what we call View Model in ASP.NET MVC) are only needed if you need more finesse over the view.
Robert Harvey
A: 

When I use the repository pattern with specification pattern, why should I use a presentation model?

What are concrete benefits of this model?

With Linq I am able to create new objects which fit to the view. For example I have a Customer object with an ID, Firstname, Lastname and Address.

In the view I only need the first and last name. So I would create a repository that has a method with a return type of IQuerable (or so) and in the controller I would use something like this: CustomerRepository.GetAll().Select(x=>new {firstname = x.Firstname, lastname = x.Lastname}.ToList(). And with specification pattern I could also add some logic for this call i. e. using selecting (where).

... so and for what a presentation layer :S?

Rookian
Data validation would go into what you are calling the "presentation model." Have you checked out the NerdDinner tutorial on ViewModels yet?
Robert Harvey
up to now I didnt read it completely =)
Rookian
+1  A: 

Rookian, I mean that letting repositories expose wished properties could serve as a presentation model.

In your example "CustomerRepository.GetAll().Select(x=>new {firstname = x.Firstname, lastname = x.Lastname}.ToList()", you're fetching all the properties from the database, and transforms it with Linq to Objects, which is a problem if performance is a concern.

I haven't implemented anything like this myself, but I'm currently using Linq to NHibernate in my repositories.

I guess an API could look like:

IRepository<TEntity>
{
   List<object> FindAll(params Func<TEntity, object>[] properties);
}

where the client uses the API like so:

var presentation = repository.FindAll(x => x.Firstname, x => x.Lastname);

... and the implementation uses the database to only fetch the properties that are needed.

Martin R-L
In my opinion this implementation replaces the presentation model, because you give the repository the responsibility to be able to satisfy the needs for the views. And this is not a really presentation model I think.
Rookian
I still think it's a presentation model, albeit an anonymous one which the client of the repository defines.Also the repository is irresponsible and doesn't have to change if the client (e.g. a controller or service) needs a new property or the presentation model needs to change in any other way.
Martin R-L
+1  A: 

Single responsibility principle.

With presentation model - you are decoupling your model from presentation layer. Your domain model ain't responsible anymore for translating everything to string, formating datetimes etc..

I.e. - there's a problem with binding entities from posted form with Linq2Entities generated classes. Presentation layer solves this easily.

Arnis L.