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

Content Delivery Monitoring in AWS with CloudWatch

This post describes a way of monitoring a Tridion 9 combined Deployer by sending the health checks into a custom metric in CloudWatch in AWS. The same approach can also be used for other Content Delivery services. Once the metric is available in CloudWatch, we can create alarms in case the service errors out or becomes unresponsive. The overall architecture is as follows: Content Delivery service sends heartbeat (or exposes HTTP endpoint) for monitoring Monitoring Agent checks heartbeat (or HTTP health check) regularly and stores health state AWS lambda function: runs regularly reads the health state from Monitoring Agent pushes custom metrics into CloudWatch I am running the Deployer ( installation docs ) and Monitoring Agent ( installation docs ) on a t2.medium EC2 instance running CentOS on which I also installed the Systems Manager Agent (SSM Agent) ( installation docs ). In my case I have a combined Deployer that I want to monitor. This consists of an Endpoint and a

Debugging a Tridion 2011 Event System

OK, so you wrote your Tridion Event System. Now it's time to debug it. I know this is a hypothetical situtation -- your code never needs any kind of debugging ;) but indulge me... Recently, Alvin Reyes ( @nivlong ) blogged about being difficult to know how exactly to debug a Tridion Event System. More exactly, the question was " What process do I attach to for debugging even system code? ". Unfortunately, there is no simple or generic answer for it. Different events are fired by different Tridion CM modules. These modules run as different programs (or services) or run inside other programs (e.g. IIS). This means that you will need to monitor (or debug) different processes, based on which events your code handles. So the usual suspects are: dllhost.exe (or dllhost3g.exe ) - running as the MTSUser is the SDL Tridion Content Manager COM+ application and it fires events on generic TOM objects (e.g. events based on Tridion.ContentManager.Extensibility.Events.CrudEven