Posts

ECO atomic operations

I’ve just created a simple class which I thought I’d share. Account1.Balance += 10 ; Account2.Balance -= 10 ; In this case we might expect the second line to throw an exception if the adjustment is not permitted, but the first line has already executed.  Obviously this isn’t a problem because we simply wouldn’t update the database, but sometimes you want an operation to occur in memory as an atomic operation; like this using (var atomicOperation = new AtomicOperation(EcoSpace)) { Account1.Balance += 10 ; Account2.Balance -= 10 ; atomicOperation.Commit(); } And so that is exactly what I wrote.  The class uses the UndoService to create/merge/remove undo blocks so it is even possible to nest atomic operations. public class AtomicOperation { readonly IEcoServiceProvider EcoServiceProvider; readonly IUndoService UndoService; readonly IUnitOfWorkValidator UnitOfWorkValidator; bool IsActive; string UndoBlockName; pu

ASP MVC CheckListBox

I needed to present the user with a list of objects from which they could select multiple items.  There is a MultiSelectList class in ASP MVC so I looked into how to use that.  It would seem that to use this class we need to use Html.ListBox.  I think this is a poor choice because it requires the user to hold down the Control key to select additional options, and it is too easy to deselect all of your values accidentally by clicking the control accidentally without the Control key held down. What I really wanted was something like a CheckListBox, a list of items with a check box next to them, so that’s what I have implemented.  Here is an example of how to set up the view data for my CheckListBox extension. public ActionResult Index() { var availableItems = new List < MyItem > (); availableItems.Add( new MyItem( " A " , " One " )); availableItems.Add( new MyItem( " B " , " Two " )); availableItems.Add( new MyIt

Unity.BuildUp–Ambiguous constructor

Sometimes you have no control over the instantiation of your classes and therefore cannot use Unity. For this reason the BuildUp method was added to Unity in order to either call the method marked with [InjectionMethod] or to set all properties marked with [Dependency].  Unfortunately in the latest build this is broken.  For some reason BuildUp searches for a suitable constructor even though the instance has already been created.  In my case I have two constructors both with only 1 parameter so I get an ambiguous constructor exception. So, I created this helper method to call the InjectionMethod on the instance… public static class UnityContainerHelper { public static void CallInjectionMethod( this IUnityContainer unityContainer, object instance, params ResolverOverride[] overrides) { if (instance == null ) throw new ArgumentNullException( " Instance " ); var injectionMethodInfo = instance.GetType().GetMethods().Where(x =&

ASP MVC encoding route values

I’ve recently been using ASP MVC 2 to develop a business application.  As you may already know the ASP MVC routing system works with URLs which look like this http://localhost/Client/Details/IBM In standard ASPX apps the URL would look more like this http://localhost/Client/Details.aspx?code=IBM The first URL obviously looks much nicer than the 2nd, however it comes at a cost.  What if the “code” of your object is something like “N/A” for “Not applicable”?  You end up with a URL that looks like this http://localhost/AbsenceCode/Details/N/A What we really need is to have ASP MVC encode the value “N/A” as “N%2FA”.  The problem is that even if it did do this then by the time ASP MVC receives the value it has already been decoded, so we get a “400 bad request” error.  This is a real pain, because in business we can’t really tell a customer “You can only use characters A to Z, 0 to 9, underscore and minus in your codes” because they will tell you that they have used the code N/A