Seeking Density in Architectural Abstractions

I’ve found myself making mental leaps about coding more quickly by cross pollinating the input data.

Take architectural abstractions. They’ve always grated on me. The better I got at writing code, the more I thought they were a waste of time…most of the time.

Yesterday I was reading the latest in a series of Ayende posts about Limiting your abstractions.

Today I had time to kill during jury duty and read a Ribbon Farm post about Dense Writing.

Click.

What’s always bothered my architectural abstractions is that tend to become brain-off copy/paste best practices that are adding more noise than value to the code base.

I like the idea that you should limit your abstractions in your code base. Oren says “you get a dozen (tops!)” :-) .

The point is that you cannot abstract everything. You actually need to make fact-informed decisions and iterate to new decisions.

Simply declaring IDataAbstraction<T> doesn’t make it so.

If you try to hide EF and NHibernate behind your abstraction you will be unable to optimize. For example, should you Eager load complex properties of an entity or not. Sometimes you should, sometimes you should not. The only code that knows when to do the right thing, is the calling code. Your abstraction prevents you from optimizing when and where necessary.

Finding yourself in this predicament, you have a few options.

1. Write your IFetchingStrategy<T> and map that against EF and NHibernate. You’re wasting your life. You’ve got an app to build.

2. Put a method on your abstraction that maps precisely to your current concrete O/RM. Your abstraction is now garbage. Your implementations become littered with “throw not implemented exceptions” or worse, will simply do nothing creating very subtle bugs. You can no longer swap implementations.

3. Accept reality and use the O/RM you chose. Doing string replace on ISession to DBContext will be trivial to reworking the code around the subtleties of the new O/RM.

Similarly, swapping out SQL Server for CouchDB for RavenDB for HyperGraphDB is not going to be trivial simply because you whipped together some IDataBase<T>. These technologies have subtle, and not so subtle, differences that contribute to a decision about whether or not to use them in your project. You can’t hide them behind an abstraction “in case you were wrong”.

Either you are castrating the tool, meaning you might as well have chosen something else, or your abstraction is an illusion and you’re wasting time with Empty Calorie Abstractions.

Now all that sounds awful unless you get the odd idea in your head that you can have more than one database in your system. Then all these decisions are much less important. But that’s another story.

An aside, the writing on RibbonFarm demonstrates that I need to work on my writing. The entire site is worth reading if only for the mind-expanding properties of the dense writing.

FubuMVC, Validation, and Re-Hydrating the View

Last week, I started experimenting with FubuMVC. About two months ago, I met three of the Fubu guys down in Austin and they sparked my curiosity about FubuMVC. Last month I took Udi Dahan’s excellent SOA course and asked him about FubuMVC in light of his views on SOA. His response was to challenge me to give FubuMVC a try and find out.

I started working my way through the beginner material on FubuMVC when I struck upon an issue present in any web framework: when you POST data and hit a problem, how do you re-hydrate the view with the data the user entered and show the user about what went wrong.

I recently came up with a solution that I was happy with in ASP.Net MVC3. However, the solution relied on a base class and FubuMVC steers us toward a compositional approach to development. I ran across these stack overflow posts and they meshed very well with my solution for ASP.Net MVC3.

The crux of the idea is that the Input Model for the POST is symmetrical with the View Model for the GET. I took the idea just a bit further.

public class FooEditRequestModel
{
    public int FooId { get; set; }
}

public class FooEditViewModel : IRedirectable
{
    public int FooId { get; set; }

    [Required]
    public int BarId { get; set; }
    public IEnumerable<Bar> Bars { get; set; } //reference/lookup data

    public FubuContinuation RedirectTo { get; set; }
}

public class FooEditInputModel
{
    public int FooId { get; set; }

    [Required]
    public int BarId { get; set; }
}

I defined a RequestModel for the GET, a ViewModel which might contain reference data, and an InputModel which is POSTed back to the server.

The reference data is the kicker. I really don’t want to post all the reference data back to the server when most of my posts will be fine. On more complex pages, it’s just difficult to do. However, once on the server, it’s tricky to marshall the user’s input data over to the GET method. I also don’t want my GET method to have to deal with the possibility of Input model data showing up as parameters.

Here’s some non working code I threw together to see how I liked my idea.

public FooEditViewModel Execute(FooEditRequestModel request)
{
    var foo = db.GetFooById(request.FooId);
    var bars = db.GetBars();
    var model = new FooEditViewModel() {FooId = foo.FooId, BarId = foo.BarId, Bars = bars};
    return model;
}

public FooEditViewModel Execute(FooEditInputModel input)
{
    var model = new FooEditViewModel();

    try
    {
        db.UpdateBarId(input.FooId, input.BarId);
        model.RedirectTo = FubuContinuation.RedirectTo<SomeOtherRequestModel>();
    }
    catch (MyValidationBusinessRuleOrDBExceptions e)
    {
        //Auto Mapper the properties needed for the request
        var request = input.MapTo<FooEditRequestModel>();
        model = Execute(request);
        model = input.MapTo(model); //Auto Mapper the input data to rehydrate the view
    }
    return model;
}

I’m using some FubuMVC patterns here, so you’ll kinda have to accept that this is possible if you’re coming from another web server stack. Both my GET and POST methods return the view model. I taught FubuMVC that any method the takes a class with “Request” in the name is a GET and any method that takes a class with “Input” in the name is a POST.

The GET is pretty standard. The POST is where the fun begins.

First, I’m not happy with the Try/Catch. I’d rather wrap that up with a Fubu Behavior. But let’s move on for a second.

If all goes well, I’m going to update my Foo and redirect to wherever using the IRedirectable interface. A lot of FubuMVC POST examples return FubuContinuation to handle the redirection, but I wanted to be able to return my view model directly from the POST to avoid having to find a way to get the data over to the GET method.

If my request goes sideways, I *should* be able to get all the data I need from my InputModel in order to build a RequestModel. With that RequestModel, I can simply execute the GET and obtain a ViewModel. Now I can just use AutoMapper to copy over the InputModel properties over the ViewModel and I should be golden. FubuMVC really shines here. In my ASP.Net version of this, the action method returns a ViewResult which I had to crack open in order to find and modify the view model within.

Now back to that try/catch I don’t like. The FubuMVC examples I’ve seen show how to set up validation failure handlers to do something when the InputModel doesn’t validate.

It seems to me there are 4 general concepts of correctness for POSTing an InputModel.

  1. The data should be in the right shape (i.e., required fields are present and fields are of the right type, length, etc). This is handled by FubuValidation or Model.IsValid in ASP.Net MVC.
  2. The data should meet all business rules. If you GET the page a 2:55PM and submit at 3:01PM, a business rule stating that a particular BarId is not valid after 3 would invalidate the request.
  3. The data should pass all concurrency checks when submitted to the database. In addition, the database simply being unavailable could fail the request here.
  4. The data and database are in great shape, the user is authorized to make the request, but the user is POSTing data they are unauthorized for. Image a user who is allowed to POST BarIds 1-5. The user is smart and knows that BarId 15 exists and uses Firebug to alter the form before clicking submit.

In the last case, I prefer to send the user to a “Fail Whale” type of generic error page and issue a priority 1 alert to OPS. Either, someone is hacking the system, or we have a serious bug in our UI that is presenting invalid options to users. Either way, someone should deal with the problem quickly.

In the other cases, it’s nice to present the form back to the user telling them what’s wrong and letting them make a choice. The PRG pattern above allows for this.

Using FubuMVC, I can imagine ways to get this into the behavior chain based on our conventions and have it working seamlessly. I’ve been growing increasingly wary of inheritance, but I can see defining our classes like this.

    public class FooEditRequestModel
    ...
    public class FooEditInputModel : FooEditRequestModel
    ....
    public class FooEditViewModel : FooEditInputModel, IRedirectable
    ....

Now our behaviors could know exactly which methods to execute and which data to copy when doing it’s work.

Boy…This was a lot of work.

It looks like the Fubu team is working hard to get some similar behavior using symmetrical models baked into FubuMVC. For “completeness”, they probably should.

After going through this exercise, I had to ask myself how the Fubu team themselves have managed to get by without all this. The answer is that they mostly POST via ajax.

I read about AjaxContinuations when I was first starting to dig into FubuMVC, but I ignored it as an oddity. Of course I want to POST my entire page back to the server, because…well, because….because that’s what’s done.

Now imagine my form POSTed via ajax. Immediately, my “re-hydrate” the ViewModel problem evaporates. All my thought on symmetrical models seems meaningless. My how do I wrap all this in a Behavior/Try/Catch simplifies as well.

In addition I gain some interesting choices for the Concurrency Violation / DB Down case. I can auto-repost or I can tell the user who changed what data or I can just do the normal “submit again” messaging.

Of course I have some work to do on the client like tie error messages back to fields and follow urls that come in via the ajax response. It turns out the Fubu team has been working on all that stuff, but that wouldn’t be too hard to cook up yourself either.

For the last couple of hours I’ve been trying to come up with a scenario where it was unacceptable to POST via ajax. For web “applications”, dependence on javascript is standard. I wouldn’t want to try and build a complex app without it. For e-commerce, I can see not forcing javascript on users. However, it turns out that they are mostly posting data that can’t fail an authorization check. By that I mean if they spoof another valid ProductId on their form, so what, they just found a new way to fill out their shopping cart. The only places you may have an issue is when they submit CC info or Address information. But these places are limited compared to a web “application” where most of the pages involve modifying data in some way.

I’m going to try to POST via ajax most of the time.

C# Partition List into List of Lists

[Edit: See the comments to see why this post is totally unnecessary. Thanks Matt T!]

I found myself in an odd situation. I had a List of objects that I needed to break into a List of Lists to make display a little more, well, sane. I did a web search and found a couple entries detailing how to break a list into fixed sized chunks.

However, my situation was such that each partition could vary in size.

I have a class that looks roughly like this:

        private class Score
        {
            public int ScoreTypeId { get; set; }
            public double Score { get; set; }
            public int DivisionId { get; set; }
        }

Some divisions have 11 “ScoreTypes”, some have 10, some have 7, etc. So I needed a way to break up my single list (the result of a db query) into chunks to give to my View code.

I came up with this:

    public static class IEnumerableExtender
    {
        public static IEnumerable<IEnumerable<T>> Partition<T, TResult>(
                 this IEnumerable<T> list, Func<T, TResult> partition)
        {
            var elements = list.Select(partition).Distinct();
            foreach (var item in elements)
            {
                yield return list.Where(x => partition(x).Equals(item));
            }
        }
    }

So now I can call it like this:

var final = list.Partition(x => x.DivisionId );

I’m pretty happy with this. I played around partitioning by different elements and even a bit with partitioning by multiple elements. It kept on producing expected results. :-)

p.s.

That MoreLinq project by Jon Skeet mentioned in the StackOverflow post looks interesting. I didn’t get a chance to go through it yet.

Selling Value for Money

One recurrent theme in business is the disconnect between the “product team” and the Sales/Marketing team. I’ve repeatedly seen product teams working long hours to make insane deadlines because the Sales team has undersold their value.

Sane rules of sales go out the window when it comes to selling software, and probably any kind of service business that primarily involves brain work like advertising or design.

The salesperson goes out and makes a deal in which the customer gets 100 hours worth of value for 40 hours worth of dollars. The salesperson celebrates another successful closing and moves on.

Congratulations, you’ve just brought the business one step closer to bankruptcy.

Worse still, management will look at the situation and thinks the salesperson is great because they brought in revenue and the product team is terrible because they are complaining about doing a little work.

I suppose the idea is that an FTE is “free” after 40 hours so it’s a good deal. “We’ll make Joe FTE work 100 hours in one week and the customer gets the project on budget. Win-Win.” Except Joe is talented and has options. If Joe walks, how much does it cost to recruit and train a new Joe. How’s the deal looking now?

Worse yet, the business missed out on 60 hours worth of revenue. Not only have you lost money now, you’ve also told the market that your services aren’t worth much. Often the idea of the first deal is that “next time” we’ll make up for it. Good luck selling 100 hours of work for 160 hours of revenue. More likely, the customer will balk and point out that last time “it only cost 40 hours”, so they probably won’t even want to pay 100 hours.

Whoops. Your business is now in a death spiral.

And yet, for real goods, it’s so much more obvious this is a bad deal. I could be the #1 Lexus salesman in the country if I sold any vehicle for $20,000. That will never happen because the sales manager won’t approve the deal.

I wish more managers would veto bad deals in service based companies.

NullOr Extension Method

I find myself writing code like this a lot:

        public static void DoSomething(Foo foo)
        {
            var thing = foo == null ? null : foo.Thing;
        }

I thought about adding an operator like ??? to go with ?? and ?, but you can’t do that in c# and it would probably be confusing to the next programmer anyway.

So how about an extension method to wrap that up:

    public static class ObjectExtensionMethods
    {
        public static TResult NullOr<T, TResult>(this T foo, Func<T, TResult> Func)
        {
            if (foo == null) return default(TResult);
            return Func(foo);
        }
    }

And now you can write:

        public static void DoSomething(Foo foo)
        {
            var value = foo.NullOr(f => f.Property);
        }

Not a lot less typing, but a bit clearer and you’re less likely to screw up.

Null Check with Extension Methods

I mentioned that I got an idea while writing the post on extension methods. I realized that you can null check using this technique.

It get’s annoying writing this code over and over:

        public static void ImportantMethod(string value)
        {
            if (value == null)
                throw new ArgumentNullException();
        }

I had considered using a NotNull<T> object to take care of null checking.

But with extension methods like this:

    public static class ObjectExtensionMethods
    {
        public static void NullCheck<T>(this T foo)
        {
            NullCheck(foo, string.Empty);
        }

        public static void NullCheck<T>(this T foo, string variableName)
        {
            if (foo == null)
                throw new ArgumentNullException(variableName);
        }

        public static void NullCheck<T>(this T foo, string variableName, string message)
        {
            if (foo == null)
                throw new ArgumentNullException(variableName, message);
        }
    }

You can just write this:

        public static void ImportantMethod(string value)
        {
            value.NullCheck();
        }

Much nicer. The overloads can facilitate whatever messaging level you desire.

This is all probably a moot point with Code Contracts in .net 4.0. To get Code Contracts working in VS2010, you have to download the code from DevLabs. That caught me off guard because the code contracts namespace is available by default in VS2010, but the actual code analysis was not.

Still, in the right situations I’d like to work on avoiding null altogether with the Null Object Pattern or immutable classes.

Avoiding Negative Branching Tests

I try to avoid ! tests in “if” blocks if there is a clearer way to express the idea in positive manner. Thanks to Larry M for turning me on to this concept.

In the post on extension methods, I had an extension method of string called HasValue. What’s the use of this?

            //negative logic:

            if (!string.IsNullOrWhiteSpace(s))
                return;

            //becomes:

            if (s.HasValue())
                return;

I think the second form is much more readable in that you have one less “twist” to think about.

I’ve encountered if checks in code that were just tortuous:

            if (!foo.IsAlreadyUndone())
                return;

That sort of thing just makes my brain hurt and ensures that maintenance will introduce bugs.

To get radical on my first example, I’ll sometimes go as far as removing any function calls from inside if blocks:

            var ok = s.HasValue();
            if (ok) return;

This may seem extreme, but it’s much harder to misunderstand what’s going on.

And yes, I will use simple variables like “bool ok;” instead of “bool stringHasAValue;” because the clarity of intent is there. If this thing is ok, get out of the function. I can use this all over the code and the reader knows that nothing interesting is happening. We’ve done a check and determined we can short circuit this method. Now we can look below and determine what is interesting about this method.

Cool Feature of Extension Methods

One cool and useful feature of extension methods is the fact that a null instance can call the method.

So say you write some code like this:

using System;

namespace FizzBuzz
{
    public class ExtensionsDemo
    {
        public static void TestString()
        {
            var s = "hello";
            var ok = s.HasValue();

            s = null;
            ok = s.HasValue();
        }
    }

    public static class StringExtensionMethods
    {
        public static bool HasValue(this string value)
        {
            return !string.IsNullOrWhiteSpace(value);
        }
    }
}

You would expect the second call to HasValue would blow up because the string is null. But the extension method is on the class not the instance so it goes through with no problem. Very handy. In fact, while typing this post I just thought of a very good use for this…coming soon.

On a side note, I think string.IsNullOrWhiteSpace is new for C# 4.0. I just found that writing the code sample for this post. Otherwise, I had to do a null check before doing a trim and then checking the length of the string.

Visual Studio Code Snippets

I’m still crying over this. Just go in VS2008, type “for”, and hit tab twice. The try alt-K, alt-X and pick anything under the c# menu.

If you knew this before and didn’t tell me, you’re probably the same person who laughed while I manually typed out using directives.

http://www.switchonthecode.com/tutorials/csharp-tutorial-visual-studio-code-snippets

Quickly Creating Using Namespace Directives

If you’re not watching TekPub videos, you’re doing yourself a disservice.

I was watching an awesome video from the Mastering ASP.NET MVC 2 series, and i noticed the presenter doing something interesting.

I haven’t been too big on visual studio shortcut keys, so when I saw this it just made me cringe. If you type a class name that you don’t have a using statement for, but Visual Studio knows about the class, it will suggest using directives to you and allow you to easily add them to the using directive section at the top of the class file.

Say you want to copy a file and you type the word “File” and realize you don’t have a reference.

Now press Ctrl-. (period). You get a pop-up like this:

quickly insert using directives with ctrl period

quickly insert using directives with ctrl period

Now cringe like I did and think about all the time you wasted scrolling up to the top of the file and typing in the using manually. Oh but wait, you already know that File is in System.IO. What about all the times that you didn’t know what namespace the class was in and you had to go to MSDN or Google to figure that out.

I always thought people I saw doing this were using ReSharper and I didn’t want to “get addicted” to a third party tool I might not be able to use at the office.

Breathe deep and move on.