Posts

Showing posts from December, 2016

Custom Validation

Example validating within a data layer  I was having a conversation with a new .net colleague the other day and he indicated that he had learned about custom validators and was all excited about validating some properties within DTO's .  The validator pattern he was using however was designed for the Web app and not the data layer where most of our models and all of our DTO's resided.  So I showed him an alternative way to accomplish the same thing without corrupting his data layer by pulling in System.Web.  Below are both methods. Here is the method he initially wanted to plop into the data project  - more aptly designed to work with a  web application  - since this method of course depends on System.Web - it would not be compatible with data layer.  Both methods accomplish the same thing. namespace MyProject.ModelValidation {    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]    public sealed class ValidFutureDate : Valid

Linq - Linked Tables on Common identifier

Lets say you have Table A and Table B and a Table which has a sole function of joining the two - not so uncommon.  You want all the records from Table A and corresponding records from Table B but you don't want to include your join table. Using Entity Framework you could do something like this. The highlighted row is what I want to draw your attention to.  See that instead of a true join I have selected the collection of TableB records in Table A     A.TableB public IQueryable < Network > getjoinedData( int Id)         {             var myRequest = ( from A in db.TableA                              from B in A.TableB                              join C in db.TableC  on B.programId equals c.programId                              where A.active == true                              && B.active == true                               && C.active == true                              select n);                 return myR

Catch me catch me if you can - Errors on MVC

There are so many articles about the various ways to handle errors that you probably don't need another one. I am just going to focus on some key points and some gotchas! The 404 Error (Page or Route not found -                     is not as much an error as an exception and it is handled differently) This is why we typically reference a special page for 404 errors in our config     <customErrors mode="On" defaultRedirect="error" >              <error statusCode=" 404 " redirect="~/error/error_notfound"/>     </customErrors> Make sure in your App_Start FilterConfig file that you are adding the filter for HandleErrorAttribute    filters.Add(new HandleErrorAttribute()); I have implemented a custom Error handler  as well so I have a filter included for that file       filters.Add(new CustomErrorHandling());     public class CustomErrorHandling : HandleErrorAttribute,  IExceptionFilter {     public overr