Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In this particular case, I might just write an extension method for User in my presentation layer.</p> <pre><code>public static class UserPresentationExtensions{ public bool IsAdmin(this User user){ return permissions.SomeLinq(ToFindIfTheyAreAnAdmin); } } </code></pre> <p>View would look something like this:</p> <pre><code>@model User &lt;fieldset&gt; &lt;legend&gt;User: @Model.Name&lt;/legend&gt; @if(user.IsAdmin()){ User is an admin } &lt;/fieldset&gt; </code></pre> <p>To avoid importing namespace repeatedly, would use web.config for that:</p> <pre><code>&lt;configuration&gt; &lt;system.web.webPages.razor&gt; &lt;pages pageBaseType="System.Web.Mvc.WebViewPage"&gt; &lt;namespaces&gt; &lt;add namespace="MyProject.PresentationExtensions" /&gt; ... </code></pre> <hr> <blockquote> <p>You have a point, however lets say you want to add validation to your models, so you decide to add a [Required] attribute to your Name property. Although then if you use a shared model then your data layer needs to know about the annotations, and if your data layer had to have some attributes your UI layer would need to know about them. In the simplest of situations you are right, sometimes it is easier to just have 1 model being the truth, and just access it in a different way, but for most complex projects you will find yourself dirtying both sides to try and save a few lines of code.</p> </blockquote> <p>I said - in this particular case. This approach applies only if mapping isn't really worth it and there's only 1-way communication (you just need to render it).</p> <p>When it comes to receiving posts, it gets bit different. Most "one size fits all" approach would be applying so called <a href="http://codebetter.com/jeremymiller/2008/10/23/our-opinions-on-the-asp-net-mvc-introducing-the-thunderdome-principle/" rel="nofollow">Thunderdome principle</a>. That is:</p> <blockquote> <p>All Controller methods take in one ViewModel object (or zero objects in some cases) and return a single ViewModel object (one object enters, one object leaves). </p> </blockquote> <p>However, quite often I prefer going extension/html helper way and just pass according arguments to action like this:</p> <pre><code>public void BatheCat(int id /* cat id */, int bathId, string shampoo){ ... } </code></pre> <p>If parameter count gets out of control (I don't bother while it's &lt;= 3), I just encapsulate them (<a href="https://gist.github.com/1416345" rel="nofollow">here's an example</a> from my project).</p>
    singulars
    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. VO
      singulars
      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