开发者

LINQ vs. presentation model

Does LINQ substitute the presentation model?

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

I wonder 开发者_C百科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.


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


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.


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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜