Check out the Latest Articles:
Unobtrusive Property Updates

I’m currently working on a project where I need to make specific changes to an existing object without touching the non-changing properties. I started going down a road that I kindof liked, but ended up having to change gears. I have a feeling I’ll come back to this class sometime later on. Also, I’d LOVE to hear some feedback on this class from the community. For those reasons, I’m posting it here:

    public interface IObjectPropertyChangerActor<out T>
    {
        IObjectPropertyChangerActor<T> With(Action<T> action);
        IObjectPropertyChangerActor<T> And(Action<T> action);
    }

    public class ObjectPropertyChangerActor<T> : IObjectPropertyChangerActor<T>
    {
        readonly T _objectToChange;

        public ObjectPropertyChangerActor(T objectToChange)
        {
            _objectToChange = objectToChange;
        }

        #region IObjectPropertyChangerActor<T> Members

        public IObjectPropertyChangerActor<T> With(Action<T> action)
        {
            return MakeTheChange(action);
        }

        public IObjectPropertyChangerActor<T> And(Action<T> action)
        {
            return MakeTheChange(action);
        }

        #endregion

        IObjectPropertyChangerActor<T> MakeTheChange(Action<T> action)
        {
            action.Invoke(_objectToChange);
            return this;
        }
    }

    public static class ObjectChangingExtensions
    {
        public static IObjectPropertyChangerActor<T> Change<T>(this T objectToChange)
        {
            return new ObjectPropertyChangerActor<T>(objectToChange);
        }
    }

Then, you use it like this:

    public class Car
    {
        public string Color { get; set; }
        public string Model { get; set; }
        public string Make { get; set; }
        public string Year { get; set; }
    } 

    public class given_an_object_property_changer_context
    {
        [Test]
        public void it_should_change_the_object_properties_as_expected()
        {
            var myCar = new Car
                {
                    Color = "Blue",
                    Make = "Chevy",
                    Model = "Cruze",
                    Year = "2011",
                };

            myCar.Change()
                .With(x => x.Color = "Red")
                .And(x => x.Year = "2012");

            ExpectThatTheChangesWereMade(myCar);
        }

        void ExpectThatTheChangesWereMade(Car myCar)
        {
            Assert.AreEqual(myCar.Color, "Red");
            Assert.AreEqual(myCar.Year, "2012");
        }
    }

I’m still considering this thing’s usefulness. My current problem is creating the list of changes from a complex object. Useful or not, I enjoyed my stroll through nerd-land (and it gave me an excuse to put my car on my blog).

Business Day Calculator

A client needed functionality to find out what date is X business days after a given date. Before the code was put into production, the requirements changed and we didn’t need the code anymore. I felt it a shame to throw away something so useful, so I got permission to post it here. Feel free to use it if you need it.

public class BusinessDayCalculator : IBusinessDayCalculator
    {
        private readonly IHolidayProvider _holidayProvider;

        public BusinessDayCalculator(IHolidayProvider holidayProvider)
        {
            _holidayProvider = holidayProvider;
        }

        public DateTime AddBusinessDays(DateTime startDate, int businessDays)
        {
            var holidays = _holidayProvider.GetHolidays(startDate) ?? new List<Holiday>();

            var listOfBusinessDays = new List<DateTime>();
            var currentBusinessDayNumber = 1;
            var currentDayNumber = 1;

            while (currentBusinessDayNumber <= Math.Abs(businessDays))
            {
                var dateCandidate = startDate.AddDays(currentDayNumber * Math.Sign(businessDays));

                var notWeekend = dateCandidate.DayOfWeek != DayOfWeek.Saturday
                                 && dateCandidate.DayOfWeek != DayOfWeek.Sunday;

                var notHoliday = !holidays.Any(x => x.Date == dateCandidate);

                if (notWeekend && notHoliday)
                {
                    currentBusinessDayNumber++;
                    listOfBusinessDays.Add(dateCandidate);
                }

                currentDayNumber++;
            }

            return listOfBusinessDays.Last();
        }
    }

public interface IHolidayProvider
    {
        List<Holiday> GetHolidays(DateTime startDate);
    }
Encrypted Code: How to Get Your Code Completely Re-Written

In the past, I have coded with some very bright and talented developers. Software development is like art, and I’ve worked along-side some true code artists. Their code is clean, readable, and expressive. Because of the care that they put into the design of their code, other developers can open the code base and, with [...]

Exceptional Code

When do you throw an exception? I never asked myself that question until a few weeks ago when some guys on my team started wondering aloud. I suppose I never asked because I just threw exceptions whenever I felt like it. But the question on the minds of my collegues has got me thinking too. [...]

Getting the “Web Control” Warm Fuzzy in MVC

I recently spoke at a Codecamp in Pittsburgh (fine city by the way) about moving from ASP.NET Webforms to ASP.NET MVC. I even wrote an accompanying blog article if you’re interested. As I was presenting, I realized I didn’t leave enough time to talk about the “web control” story in MVC. I promised on the [...]

Take Back Control: Moving From Webforms to MVC

I made a major paradigm shift earlier this year in terms of programming methodologies. See, I’ve been programming own my own (for the most part) since I was 9 years old, but I’ve never paid attention to how other people coded. I got my start writing RPG’s with my childhood friend and went on to [...]