Purpose
I was recently working on a project where I wanted to create the most granular interfaces possible and then group them into usable objects. My intent was to pass these objects around to shared utilities and services that would focus on the interfaces and thus be reusable by intent instead of specific implementation. For example, we would create an interface each for phone number, email and street address. These interfaces could be used on a customer, vendor, or employee object to ensure a standard implementation of the fields. Then, we could build our communications services to work off the interfaces for communication types and thus be generic to the entity being contacted. By doing this, I would be able to build singularly focused services that dealt with small tasks on data objects with a disregard for their specific implementation beyond the interface.Validation
Being a big fan of data contracts with ability to run self-aware validations(e.g. validations that can only consider the scope of the contract itself without any relationships), I found myself needing a way to have all objects validate based on the requirements of the smaller interfaces, within the CRUD services of the manifested object. I also wanted to ensure the ability to dynamically add validation routines and have them picked up by the calling services without having to couple them together.Solution
By creating a custom attribute and applying it to methods in the interface, I would be able to reflect through the methods of any object and trigger it to self validate using any and all methods that were added to the object from the implemented interfaces. Next, we simply call a data contract extension method that to invoke all validations. Here are the examples.Custom Method Atribute
[AttributeUsage(AttributeTargets.Method, Inherited = false)] public class InterfaceValidation : Attribute { //... }
public static class DataContractExtensions { public static T CallInterfaceValidations<T>(this T obj) { var methods = obj.GetType().GetMethods(); object[] parameters = null; foreach (var method in methods) { var attributes = method.GetCustomAttributes(typeof(InterfaceValidation), true); if (attributes != null && attributes.Length > 0) { try { method.Invoke(obj, parameters); } catch (Exception) { throw; } } } return obj; } }
[InterfaceValidation]
void ValidateSomething();
converse outlet, herve leger, hollister clothing, lululemon, valentino shoes, oakley, vans, nike air max, bottega veneta, soccer jerseys, mont blanc pens, wedding dresses, vans outlet, celine handbags, nike huaraches, north face outlet, insanity workout, ray ban, mcm handbags, baseball bats, hollister, instyler, p90x workout, ghd hair, nike trainers uk, chi flat iron, iphone cases, nfl jerseys, mac cosmetics, nike roshe run, gucci, babyliss, nike air max, soccer shoes, asics running shoes, beats by dre, north face outlet, ferragamo shoes, converse, abercrombie and fitch, hermes belt, hollister, reebok outlet, new balance shoes, ralph lauren, longchamp uk, jimmy choo outlet, lancel, timberland boots, louboutin
ReplyDeletecanada goose outlet, pandora uk, louis vuitton, louis vuitton, karen millen uk, toms shoes, ugg,ugg australia,ugg italia, ugg uk, moncler uk, louis vuitton, montre pas cher, wedding dresses, canada goose, pandora jewelry, moncler, canada goose outlet, juicy couture outlet, moncler, canada goose, canada goose, moncler, louis vuitton, canada goose uk, ugg, moncler, hollister, thomas sabo, moncler outlet, ugg,uggs,uggs canada, ugg pas cher, coach outlet, pandora jewelry, replica watches, swarovski crystal, supra shoes, louis vuitton, moncler outlet, swarovski, links of london, canada goose outlet, juicy couture outlet, marc jacobs, doudoune moncler, canada goose jackets, pandora charms
ReplyDeletecanada goose jackets
ReplyDeleteadidas trainers
toms shoes
oklahoma city thunder jerseys
north face outlet
true religion outlet
coach outlet
nhl jerseys
north face jackets
moncler jackets
hzx20161221