When talking about the Model View ViewModel (MVVM) , Model View Controller (MVC) or any other presentation patterns Model some people refer to it as the Database Model, others as the Domain Model. Well, it is actually referred as a Presentation Data Model.
Your database or domain types could have many properties that the View does not care about and it will be a waste of resources as well as a security risk to have all that data traveling back and fourth the network even if it is an intranet.
Another problem with using Database or Domain data in your MVVM is that any change to the database or domain will require to update all the clients and that could be very costly.
Let's assume for example that we have the following very simple Domain Model:
and the following Database Model:
Note: I created the Database Model using the following NHibernate mapping:
<?xml version="1.0" encoding="utf-8"?> <!--Generated with CyberBizSoft's NHibernate Helper on 12/16/2009 11:30:06 PM --> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CyberBizSoft.SSL.Domain"
namespace="CBSI.SSL.Domain"> <class name="Store" table="SSLStores" lazy="false" > <id name="ID" unsaved-value="0" column="ID" type="System.Int64"> <generator class="identity" /> </id> <property name="Name"/> <property name="Description"/> <component name="Contact" class="CBSI.Common.Domain.SingleContact,
CyberBizSoft.Domain" > <component name="Address" class="CBSI.Common.Domain.Address, CyberBizSoft.Domain"> <property name="Street" column="Street" /> <property name="City" column="City" /> <property name="State" column="State" /> <property name="ZipCodeNumber" column="ZipCodeNumber" /> <property name="Country" column="Country" /> </component> <component name="Phone" class="CBSI.Common.Domain.Phone, CyberBizSoft.Domain"> <property name="FullNumber" column="PhoneNumber"/> </component> <component name="Fax" class="CBSI.Common.Domain.Phone, CyberBizSoft.Domain"> <property name="FullNumber" column="FaxNumber"/> </component> <component name="EMail" class="CBSI.Common.Domain.Url, CyberBizSoft.Domain"> <property name="URL" column="EMailAddress"/> </component> <component name="Web" class="CBSI.Common.Domain.Url, CyberBizSoft.Domain"> <property name="URL" column="WebAddress"/> </component> </component> </class> <!--Copyright (c) 2001 - 2009 by CyberBizSoft, Inc. All rights reserved.--> </hibernate-mapping
Let's say that we have to create an interface only to change the Name and Description of this Store. Why would we have to bring all the other information into the client if we only need the Store's ID, Name & maybe Description?
Preferably we could create a very simple presentation data model (StoreNameModel) which we could adorn with presentation specific attributes,
class StoreNameModel { [Key] public long ID { get; set; } [Required] public string Name { get; set; } public string Description { get; set; } }
map the data from the Domain Store in the service to the StoreNameModel
public IQueryable<StoreNameModel> Stores() { return from c in Repository.All orderby (c.Name) select new StoreNameModel() { ID = c.ID, Name = c.Name, Description = c.Description }; }
and then project it into the ViewModel in the client.
Update (4/23/10)
I found this post by Deepesh Mohnani about the WCF RIA Presentation Model
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.