Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Several layers doesn't lead to maintenance nightmare, moreover the less layers you have - the easier to maintain them. And I'll try to explain why.</p> <p><strong>1)</strong> Transfer Model shouldn't be the same as Data Model</p> <p>For example, you have the following entity in your ADO.Net Entity Data Model:</p> <pre><code>Customer { int Id Region Region EntitySet&lt;Order&gt; Orders } </code></pre> <p>And you want to return it from a WCF service, so you write the code like this:</p> <pre><code>dc.Customers.Include("Region").Include("Orders").FirstOrDefault(); </code></pre> <p>And there is the problem: how consumers of the service will be assured that the <code>Region</code> and <code>Orders</code> properties are not null or empty? And if the <code>Order</code> entity has a collection of <code>OrderDetail</code> entities, will they be serialized too? And sometimes you can forget to switch off lazy loading and the entire object graph will be serialized.</p> <p>And some other situations:</p> <ul> <li><p>you need to combine two entities and return them as a single object.</p></li> <li><p>you want to return only a part of an entity, for example all information from a <code>File</code> table except the column <code>FileContent</code> of binary array type.</p></li> <li><p>you want to add or remove some columns from a table but you don't want to expose the new data to existing applications.</p></li> </ul> <p>So I think you are convinced that auto generated entities are not suitable for web services. That's why we should create a transfer model like this:</p> <pre><code>class CustomerModel { public int Id { get; set; } public string Country { get; set; } public List&lt;OrderModel&gt; Orders { get; set; } } </code></pre> <p>And you can freely change tables in the database without affecting existing consumers of the web service, as well as you can change service models without making changes in the database.</p> <p>To make the process of models transformation easier, you can use the <a href="https://github.com/AutoMapper/AutoMapper" rel="nofollow">AutoMapper</a> library.</p> <p><strong>2)</strong> It is recommended that View Model shouldn't be the same as Transfer Model</p> <p>Although you can bind a transfer model object directly to a view, it will be only a "OneTime" relation: changes of a model will not be reflected in a view and vice versa. A view model class in most cases adds the following features to a model class:</p> <ul> <li><p>Notification about property changes using the <code>INotifyPropertyChanged</code> interface</p></li> <li><p>Notification about collection changes using the <code>ObservableCollection</code> class</p></li> <li><p>Validation of properties</p></li> <li><p>Reaction to events of the view (using commands or the combination of data binding and property setters)</p></li> <li><p>Conversion of properties, similar to <code>{Binding Converter...}</code>, but on the side of view model </p></li> </ul> <p>And again, sometimes you will need to combine several models to display them in a single view. And it would be better not to be dependent on service objects but rather define own properties so that if the structure of the model is changed the view model will be the same.</p> <p>I allways use the above described 3 layers for building applications and it works fine, so I recommend everyone to use the same approach.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload