Skip to main content

A DD4T.net Implementation - Domain Models vs View Models

I have been talking so far about strongly typed models for DD4T .net in the sense of mapping a Tridion Schema into an identical View Model, where each Schema field is represented by a property of the model class.

In real life thought, this one to one mapping is never enough -- never shall such mapping be sufficient in a real implementation. There will always be a need for more state, more business logic, more information in a model than what there is by mapping Tridion Schema fields.

The problem becomes more complicated when we try to reuse models in different contexts. In this situation, we might need some additional information for one context where the model is displayed, while in another context, we should have other information available.

There are two ways of solving this issue, which I'm detailing below.

A. Add extra information to the model

In the existing Tridion Schema --> View model, we add all extra information, properties, or business logic needed for each situation where we need to render that View model.

This approach is easy and messy. So even it time to market is short, the end model ends up being polluted with properties that don't necessarily pertain to the model itself. When the model is used in different contexts, some properties will have no values or will be meaningless for a specific context.

B. Use different Domain models and View models

This approach is much cleaner because it maintains the separation of the Tridion Schema model class (now called a Domain model) and the different contexts it needs to be used in (called View models).

The Tridion Schema --> Domain model class does not change. It is created once, in the beginning of the project and has the same fields and structure as the counter part Tridion Schema.

But, for each context where we need to render the Domain model, we create a specific View model class that contains all specific information, state, or business logic needed for the said context.

The advantage is a cleaner and proper separation of the Tridion Schema mapped Domain model and the specific context View model. Reuse of the Domain model is guaranteed in its purest form, maintaining at the same time the Tridion concept of reusing Components (content) with different Component Templates (design).

The Domain model itself is stateless and contextless, providing nothing but the actual data (i.e. the content) for the specific renditions of it. The View models provide the additional information needed and provide context for the content.

As example, let's consider the Device domain model class:

    public class Device : ModelBase
    {
        public string Brief { get; set; }
        public Banner Banner { get; set; }
        public IList<EmbeddedParagraph> Body { get; set; }
        ...
    }


When needed to display the Device in a Full Detail context, we define a View model DeviceFullDetail that takes the Device domain model and decorates it with additional state, or business logic:

    public class DevicesFullDetail
    {
        public Device Device { get; private set; }
        public bool IsNew { get; private set; }

        public DevicesFullDetail(Device device)
        {
            Device = device;

            //Additional logic for Device
            IsNew = device.Metadata.UpdateDate > DateTime.Now.Subtract(TimeSpan.FromDays(1));
        }
    }


Finally, the Razor view uses the DeviceFullDetail View model to render the Device:

@model DevicesFullDetail

@if (Model.IsNew)
{
    @Model.Device.Brief
}

Should we need another context, we can simply create another View model using the same Device domain model class and populate it with the additional attributes needed in there.



Comments

Anonymous said…
What would be a good place to create the DevicesFullDetail instance and set it to be used i in the view, within the DD4T viewmodel creation/processing process?
Mihai Cădariu said…
That would be in a Component controller. Typically I have a ResolveModelAndView method that creates the Domain model object from the IComponentPresentation, then I create each View model object I need for the actual view rendering.
Anonymous said…
Hi Mihiai, I am fairly new to DD4T. I assume your CTs metadata specify different controller/actions, and you prepare the correct viewmodel in the specific controllers' actions? Or do you use a different way?
Mihai Cădariu said…
I build the models as you describe

Popular posts from this blog

Running sp_updatestats on AWS RDS database

Part of the maintenance tasks that I perform on a MSSQL Content Manager database is to run stored procedure sp_updatestats . exec sp_updatestats However, that is not supported on an AWS RDS instance. The error message below indicates that only the sa  account can perform this: Msg 15247 , Level 16 , State 1 , Procedure sp_updatestats, Line 15 [Batch Start Line 0 ] User does not have permission to perform this action. Instead there are several posts that suggest using UPDATE STATISTICS instead: https://dba.stackexchange.com/questions/145982/sp-updatestats-vs-update-statistics I stumbled upon the following post from 2008 (!!!), https://social.msdn.microsoft.com/Forums/sqlserver/en-US/186e3db0-fe37-4c31-b017-8e7c24d19697/spupdatestats-fails-to-run-with-permission-error-under-dbopriveleged-user , which describes a way to wrap the call to sp_updatestats and execute it under a different user: create procedure dbo.sp_updstats with execute as 'dbo' as...

I Have Gone Dark

Maybe it's the Holidays, but my mood has gone pretty dark. That is, regarding the look and feel of my computer and Tridion CME, of course. What I did was to dim the lights on the operating system, so I installed Placebo themes for Windows 7 . I went for the Ashtray look -- great name :) My VM looks now like this: But, once you change the theme on Windows, you should 'match' the theme of your applications. Some skin easily, some not. The Office suite has an in-built scheme, which can be set to Black , but it doesn't actually dim the ribbon tool bars -- it looks quite weird. Yahoo Messenger is skinnable, but you can't change the big white panels where you actually 'chat'. Skype is not skinnable at all. For Chrome, there are plenty of grey themes. Now i'm using Pro Grey . But then I got into changing the theme of websites. While very few offer skinnable interfaces (as GMail does), I had to find a way to darken the websites... Enter Stylish -- a pl...

REL Standard Tag Library

The RSTL is a library of REL tags providing standard functionality such as iterating collections, conditionals, imports, assignments, XML XSLT transformations, formatting dates, etc. RSTL distributable is available on my Google Code page under  REL Standard Tag Library . Always use the latest JAR . This post describes each RSTL tag in the library explaining its functionality, attributes and providing examples. For understanding the way expressions are evaluated, please read my post about the  Expression Language used by REL Standard Tag Library . <c:choose> / <c:when> / <c:otherwise> Syntax:     <c:choose>         <c:when test="expr1">             Do something         </c:when>         <c:when test="expr2">             Do something else         </c:when...