Posts

How many times does a day occur within a date range?

Given two dates I need to know how many times each day of the week occurs within that range, so that I can calculate a value where the factor varies by day.  So I wrote the following code which returns an array of integers, position 0 will tell you how many Sundays there are, position 6 will tell you how many Saturdays, and so on. int [] CountDays(DateTime firstDate, DateTime lastDate) { var totalDays = lastDate.Date.Subtract(firstDate.Date).TotalDays + 1 ; var weeks = ( int )Math.Floor(totalDays / 7 ); var result = Enumerable.Repeat < int > (weeks, 7 ).ToArray(); if (totalDays % 7 != 0 ) { int firstDayOfWeek = ( int )firstDate.DayOfWeek; int lastDayOfWeek = ( int )lastDate.DayOfWeek; if (lastDayOfWeek < firstDayOfWeek) lastDayOfWeek += 7 ; for ( int dayOfWeek = firstDayOfWeek; dayOfWeek <= lastDayOfWeek; dayOfWeek ++ ) result[dayOfWeek % 7 ] ++ ; } return result; }

ASP MVC Silverlight control not appearing–404 not found

Just a quick tip in case you have deployed your ASP MVC app with silverlight controls in it which are not appearing.  I experienced this recently when deploying to IIS6.  The solution is Open the IIS manager app (Start->Admin->Internet Information Services (IIS) Manager) Expand the local computer node Then expand the Websites nodes Right-click your website and select “Properties” Click the “Http Headers” tab At the bottom of the page click the “MIME Types” button Click the...

Creating a pooled lifetime manager for Unity

My application has a child container per request, this is because there are a lot of services used in each request which depend upon an IUnitOfWork.  I thought it would be nice if I could define a pool of these IUnitOfWork instances so that any cost involved in creating them is reduced, they can just be reused in a round-robin fashion.  Well, more accurately, the object space (EcoSpace) on which they depend can anyway. A pool can now be registered like so… // Must be called once, when the container is created container.AddNewExtension < PooledLifetimeExtension > (); // To register a pooled item container.RegisterType < ISomeItemThatIsExpensiveToCreate, SomeItemThatIsExpensiveToCreate > ( new PooledLifetimeManager()); The number 10 specifies how many instances at once may be stored in the pool.  If an instance requests an item from the pool when it is empty it will get a new instance, it’s only at the point where an instance is returned to the pool that the

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