<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Validating with a Service Layer</title><link>http://www.asp.net</link><pubDate>Mon, 06 May 2013 17:07:18 GMT</pubDate><generator>umbraco</generator><description>Comments for Validating with a Service Layer</description><language>en</language><atom:link href="http://www.asp.net/rss/comments/27584" rel="self" type="application/rss+xml" /><item><title>Comment Posted by tavanaonline</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Tue, 03 Aug 2010 20:47:43 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-000000009736</guid><description><![CDATA[ <p>Is there a way for using DataAnnotations instead while keeping validation in service layer like the above?</p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/tavanaonline.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by meme</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Fri, 13 Aug 2010 01:35:44 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-000000009844</guid><description><![CDATA[ <p>Stephen, </p><p></p><p>Thanks for this, it is informative and clear. Very useful for my purposes. </p><p></p><p>However, either I don&#39;t fully understand this approach, or it seems flawed.</p><p></p><p>The problem I have is that following this approach can seemingly lead to Exceptions from the EF entity itself when MVC attempts to bind posted values to the properties of the EF entity. </p><p></p><p>i.e. ConstraintException from the EntityObject derived class (e.g. from null values) before code execution has even got inside the Post Action (e.g. your ProductControllers Create) block. </p><p></p><p>[AcceptVerbs(HttpVerbs.Post)]</p><p>        public ActionResult Create([Bind(Exclude = &quot;Id&quot;)] Product productToCreate)</p><p>        {    </p><p></p><p>So am looking into what to do in these cases, obviously can&#39;t rely on JScript alone to avoid these exceptions, and do not want to change to more weak typing for the Post Action parameters (and then have to do the form values to typed Model mapping by hand), so not sure what to do really. </p><p></p><p>Any ideas, or have I missed something in the post itself? </p><p></p><p>Seems like a pretty big problem for this approach unless there is a decent solution. </p><p></p><p>Is this because you were using &quot;code first&quot; and so have simpler entities without such &quot;validation&quot; functionality built in? </p><p></p><p>(I am going schema first atm, and have not really used code first).</p><p></p><p>Many Thanks</p><p></p><p></p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/meme.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by jonjenkins</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Tue, 14 Sep 2010 01:46:53 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000010220</guid><description><![CDATA[ <p>This works well, but how does it handle validating IEnumerable collections in the post method? I tried it and noticed that it failed to display the ModelState error on the view since the field name in a collection contains the [index] prefix, but the service layer simply validates on the plain field name with no index associated. Any tips?</p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/jonjenkins.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by vherz</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Thu, 16 Sep 2010 12:42:19 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000010265</guid><description><![CDATA[ <p>&quot;_validatonDictionary&quot; seems to be a typo.</p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/vherz.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by nicequy</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Sat, 29 Jan 2011 13:14:06 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000011838</guid><description><![CDATA[ <p><a rel="nofollow" href="http://forums.asp.net/t/1486130.aspx" target="_blank">forums.asp.net/</a></p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/nicequy.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by mhfaust</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Sun, 10 Apr 2011 06:59:23 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000012747</guid><description><![CDATA[ <p>There are collections of products (&lt;product&gt;) and numerous members and arguments typed as &quot;product&quot;, but no &quot;product&quot; class is defined anywhere. Where would this be, what would it look like??</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/mhfaust.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by BatsIhor</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Mon, 11 Apr 2011 07:50:40 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000012757</guid><description><![CDATA[ <p>I may be wrong but, if you move repository logic to the service layer why did you leave at the controller repository creation? I think it will be better if controller will know only about service and it will work only with service. service will know only about repository and work with it.. It will be according to the SOLID!</p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/BatsIhor.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by frankhoffy</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Mon, 09 May 2011 12:35:30 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000013110</guid><description><![CDATA[ <p>The main drawback I see with this approach is that client-side validation is no longer possible, because validation always happens on the submit.</p><p></p><p>It also means that as the customer fixes the data in the offending fields, they will still show up in red until they re-submit.  This also holds true for the WPF world.</p><p></p><p>I&#39;d like to see how this model is implemented in the WPF world as well.  I guess you have to maintain your own error collection and implement IDataErrorInfo.</p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/frankhoffy.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by ryanw51</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Thu, 25 Aug 2011 13:25:07 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000014055</guid><description><![CDATA[ <p>Excellent post.  This was a major help in cleaning complex validation out of my controllers.  Now, it&#39;s all tucked away in the service layer just like it should be.</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/ryanw51.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by prasanth.net</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Wed, 14 Sep 2011 05:49:51 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000014188</guid><description><![CDATA[ <p>Good article</p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/prasanth.net.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by starforce</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Fri, 23 Sep 2011 15:34:14 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000014253</guid><description><![CDATA[ <p>Was half helpful but would have been better if there was a demonstration of the Product Class. I am assuming that the Product class would be located in the Modeler and would represent the View</p>]]></description><enclosure length="0" type="image/png" url="http://i2.asp.net/avatar/starforce.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by koistya</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Tue, 19 Jun 2012 14:42:40 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000015804</guid><description><![CDATA[ <p>Good article. Thanks. Though don&#39;t like how this service class passes validation errors back to the controller with IValidationDictionary. I would make it work more like this:</p><p></p><p>&lt;a rel=&quot;nofollow&quot; target=&quot;_new&quot; href=&quot;https://gist.github.com/2955801&quot; target=&quot;_blank&quot;&gt;https://gist.github.com/2955801&lt;/a&gt;</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/koistya.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by koistya</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Tue, 19 Jun 2012 16:32:50 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000015805</guid><description><![CDATA[ <p>&lt;a rel=&quot;nofollow&quot; target=&quot;_new&quot; href=&quot;https://gist.github.com/2956368&quot; target=&quot;_blank&quot;&gt;https://gist.github.com/2956368&lt;/a&gt;</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/koistya.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by jselesan</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Mon, 25 Jun 2012 15:35:02 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000015846</guid><description><![CDATA[ <p>Very usefull post, but I have a problem. I&#39;m trying to use an IoC framework to inject the business services ojects into my controllers. The problem is that I cannot refer to ModelState to configure my IoC container (Castle Windsor). Did you think on this scenario?</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/jselesan.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by alireza6282</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Tue, 25 Dec 2012 10:47:32 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000017063</guid><description><![CDATA[ <p>hi, this is good idea for DI pattern and ...</p><p>only one problem,</p><p>why we can render validation on client same annotation attributes in MVC 4?</p><p></p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/alireza6282.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by desmond80in</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Mon, 21 Jan 2013 20:54:53 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000018222</guid><description><![CDATA[ <p>Thank you very much for the article. This was very informative. In my current MVC application I have not implemented the validation part of the service and the first thing I&#39;m  going to do it to implement that. </p>]]></description><enclosure length="0" type="image/png" url="http://i1.asp.net/avatar/desmond80in.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item><item><title>Comment Posted by koistya</title><link>http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validating-with-a-service-layer-cs</link><pubDate>Mon, 06 May 2013 17:07:18 GMT</pubDate><guid isPermaLink="false">00000000-0000-0000-0000000019048</guid><description><![CDATA[ <p>Stephen, how would you make this work with a dependency injection? ...when this.ModelState of the Controller is unknown yet when service class is constructed</p><p></p><p>Working on implementing a similar architecture here:</p><p></p><p>&lt;a rel=&quot;nofollow&quot; target=&quot;_new&quot; href=&quot;https://github.com/kriasoft/site-sdk&quot; target=&quot;_blank&quot;&gt;https://github.com/kriasoft/site-sdk&lt;/a&gt;</p>]]></description><enclosure length="0" type="image/png" url="http://i3.asp.net/avatar/koistya.jpg?forceidenticon=false&amp;dt=635072698800000000&amp;enableAvatar=False&amp;cdn_id=2013-05-10-001" /></item></channel></rss>