CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Kyle Baley - The Coding Hillbilly

"We are stuck with technology when what we really want is just stuff that works" -- Douglas Adams
  • Overview and review of S#arp Architecture

    This has been germinatin' for a while until I could: a) take a look at the project in adequate detail, and b) I could talk to the project's founder, the gallantly-clad and adequately-shod Billy McCafferty, to answer some questions and address my concerns.

    S#arp Architecture is, and let me see if I can put this into my own words, a solid architectural foundation for rapidly building maintainable web applications leveraging the ASP.NET MVC framework with NHibernate. There, now, that sounds hillbilly-ish and all marketable.

    Ok, I stole that from the project's home page. Which I say because I would word it a little differently. Specifically, I'd take out the word "rapidly" because as much as the framework will help, if you're building an MVC app that requires NHibernate, I'm not so sure you *should* be doing it rapidly. At least not using the same definition of "rapidly" that Microsoft does in its demos.

    But enough semantics. You are all clearly awaiting my opinion of the framework otherwise you wouldn't still be reading this far (which is why I bury all the meat of my posts behind pre-amble: to weed out the people with more common sense).

    At the time of writing, the framework is geared more toward the NHibernate part of the equation than the MVC part. There is little in there that is specific to MVC apps save for an auto-binder class and a TransactionAttribte, which I hope I'll get to in a bit. Other than that, the framework deals more with data access management.

    Which is a good thing given the amount of boilerplate code involved with data access. There are a couple of pieces I'll highlight here that I like.

    NHibernate Session Management

    This was the first thing I noticed that got me all hot and bothered. NHibernate session management was always something I had only a peripheral grasp of at best. I think best practice is to put it in an HTTP module and register it in the web.config. But the way I've actually done it is to wait until the first request for a session, then add it to the HTTP Context, a practice I'm not quite sure I like anymore.

    With S#arp Architecture, session management is reduced to the following code in your global.asax.cs Init method:

    NHibernateSession.Init(new WebSessionStorage(this), null);

    That's it, kiddies. Instant NHibernate Session management. Of course, you still need the NHibernate config section somewhere. In the code above, the null indicates that it's in the web.config. If you have it in an external file, you can provide the name of it instead.

    What I like about this is that it puts NHibernate initialization where all the rest of my initialization code goes. Container configuration, route registration, NHibernate...ummm...'Nitialization, all from the comfort of global.asax.

    What could be improved:

    • Add an overload that *doesn't* require a config file instead of relying on null to indicate the web.config
    • Change the name. Yes, we're programmers and like our abbrev's but I'm what you might refer to as somewhat of a slightly verbose guy. Initialize is clearer. Or InitializeWith if you want to start an argument with fluent interface pundits.

    GenericDao<T>

    Those of you who programmed before the days of classic ADO probably threw up a little in your mouths reading that but fear not! It's not DAO. It's what some (including myself) often call RepositoryBase<T>. A base class that provides the basics for dealing with repositories: Get, Save, Delete, etc, etc, and so on and so forth.

    Likes: I am *always* cutting and pasting RepositoryBase<T> from one project to the next, regardless of MVC-ness. Never took the time to framework-ize it so this saves me the trouble. This one also comes with added support for non-integer IDs, while still defaulting to integers.

    What could be improved:

    • At the moment, it comes with a lot more than Get, Save, and Delete. The interface also includes Update, SaveOrUpdate, Evict, GetByExample, and two Load methods. All in all, I think it requires much too much knowledge of NHibernate to use all of these. It's more of a way to expose NHibernate functionality than to design an interface that people would use most. But I was encouraged in my conversation with Billy that this will be trimmed down to the very basics.
    • Possibly include some basic Find functionality. Billy and I talked somewhat randomly about this and I have trouble crystallizing any thoughts on an implementation without actually playing with it. The idea would be to balance not requiring a crazy intense knowledge of NHibernate criteria with something flexible enough to handle 80% of the use cases.
    • The name. Personal preference, I know. But if the project espouses Domain Driven Design as it says on the home page, I'd like to see it use the terminology and call this GenericRepository or RepositoryBase or TheRepositoryFromWhichYouShouldInherit or something.

    These two things represent probably the biggest time-saver in my world, particularly if the GenericDao interfaces is cleaned up. There is also a handful of other features.

    PersistentObject<T>

    This is a base class that does a couple of things. It includes an ID property (the <T> part defines the type) and has a property that will tell you if it has been persisted yet, which it determines by checking the ID.

    It also gives you a mechanism to compare domain objects. By decorating certain properties on your object with a [DomainSignature] attribute, you are telling the framework that those properties are used to compare two objects for equality.

    What could be improved:

    • The names. PersistentObject seems to imply it has something to do with how the object is saved when in fact, the class is geared more toward making it easier to compare two domain objects.
    • No interface. I would like to see an IPersistentObject interface that you could implement on your own and bypass the base class. I think this is coming based on our conversation.

    ControllersAutoBindModule

    This is probably the area of the project that gives me most pause. It is pretty tightly bound to NInject. This class provides auto-registration of controllers specifically for NInject. I'm not familiar with NInject but for Windsor, this is pretty easy to do on your own. Here is the code for it:

    _container.Register(
        AllTypes.Of( )
            .FromAssembly( Assembly.GetExecutingAssembly( ) )
            .Configure( c => c.LifeStyle.Transient.Named( c.Implementation.Name.ToLower( ) ) )
        );
    The issue with containers is that there are at least three good ones to choose from. I would not hesitate to use Windsor or StructureMap on any project, and would consider NInject based solely on its reputation. It's one thing to tie a project like this to NHibernate, which is a fairly safe decision. It's another to throw your weight behind one particular IoC container.

    I think there is still some work to do in this area. My understanding is that the project is moving toward the Common Service Locator for this so that you can use whatever container you like but at the moment, this class is something I'd simply ignore.

    Design By Contract

    At the moment, there is some basic support for Design By Contract. It is based almost entirely on this Code Project article.

    It could be I don't understand DbC but this appears to be more of a validation class. That is, in various places, you put code like Check.Require( x < 10 ) and Check.Ensure( y != null ).

    I've included this for completeness. Hillbillies aren't really turned on by validation engines.

    A note on base classes

    I've touched on the fact that you have to inherit from these classes in our own repositories/domain objects. When I first looked at the framework, this raised some red flags. But after comparing it to my projects and getting a sense of the goal of S#arp Architecture, I no longer think it's a bad thing.

    The reason is: Both classes do pretty much everything I would do in my own base classes anyway. The only difference is that I no longer have control over its implementation. But as long as I trust the direction of the framework, and as long as they are kept fairly lean and don't follow the path of your average third-party grid control, I have no issue handing off these responsibilities to someone else. As I mentioned, the GenericDao implementation is a little chatty for my tastes but I believe we'll see some improvement in that area pretty soon.

    Really, I shouldn't be calling this a framework. It's more of a scaffolding for NHibernate/MVC projects, an idea reinforced by Billy who has plans to include a project template and possibly some item templates. Once that became clearer to me, the benefits of these classes shone through a little more.

    The Testing Story

    The project is almost completely covered by tests, which doesn't really matter if you are using it. But it adds to my confidence that the project is going to work as advertised.

    The sample application also includes tests for its Dao/Repository objects. These are, for all intents and purposes, integration tests because they are testing actual database connections and whether you can get and save data from a database. It seems to me that there should be an easy way to create boilerplate tests like this based on your Dao objects. The tests are necessary to have but mechanical to create.

    In fact, since it's so close to Christmas and we have wish lists on our minds, it would be fantastic if, when you create a S#arp project, it also set up a project or folder for these tests and set up the boilerplate code for connecting to a SQLLite database.

    Wrap-up

    I did get a sneak peek of some of the new stuff which includes some MVC niceties. Billy has a pretty clear picture of how it should look and I'm encouraged by the philosophy and community behind it.

    In the end, I'll say the same thing I told Billy: I'd use the framework on a new project and would even move toward it on an existing one. It gets my plumbing 80% of the way there that much faster. It won't handle business logic or even some of the funkier data access edge cases, nor should it. But it does wrap a good chunk of the infrastructure I do in MVC applications in a nice neat package.

    Kyle the Sharpened

  • Pimpin' DevTeach

    Been putting this post off until I could give it the attention it deserves but the merits keep piling up on their own. DevTeach starts in about 3 weeks (yikes, is that all?) and there has been a flurry of activity around it. There is still time to sign up and if you are waffling, here is some information to tip the scales:

    • The Agile Track. And I shan't elaborate
    • This is unconfirmed but my sources tell me Jeff Lebowski (or possibly Eddie Vedder) will be doing the keynote. I sure hope that's true because I'll be very disappointed if it turns out to be a fake that just look like him.
    • Free copies of Visual Studio 2008 Pro, Expression Web 2, and Tech-Ed to EVERY participant

    Despite my hyperbolic tendencies, that last one is not a joke. Over $1000 of software will be given to every attendee. Even if you come only for the regular conference, the net cost to you is approximately fifty bucks. (Of course, we won't mention the hidden fees associated with being in Montreal for a week away from your family/conscience.)

    To get more information, John "Pipes" Bristowe interviewed main DevTeacher, Jean-Rene Roy about the conference. And Dave Woods outlines many more benefits (including the 10% WestJet discount I wish I knew about before I booked my flight.)

    Kyle the DevTaught

  • Coding with a net: An anecdote

    I'm in the early stages of a new project with the dashing and debonair Brian Donahue. On the plus side, despite his misgivings, the codebase is in pretty phenomenal shape all things considered. SVN repository, automated build and tests, ActiveRecord, views reasonably separated into MVP. Even a half-finished CI process which is impressive for a one-man team. Like any codebase, it has its share of issues but all in all, I won't be using this as a sample app for a brownfield application.

    On the minus side, he reads my blog so I'll have to be more subtle in the digs I make toward my workplace.

    Anyway, one of the first things I like to do when I start looking through a steaming pile of excrement that someone calls "codebase" is check out the warnings that are lingering. The code had its share with the vast majority related to an upgrade to the latest version of Rhino Mocks but without changing CreateMock. This was an easy fix as a global search and replace from CreateMock to StrictMock got rid of over 80% of the errors.

    The rest were a flurry of ReSharper refactorings until I got to the final four. Four assemblies reported the error: found conflicts between different versions of the same dependent assembly.

    Double-clicking this error gives a helpful message offering to add a binding section to the assemblies app.config file. I say helpful seriously there because, while I had no intention of keeping that section in the app.config, at least it was able to tell me what the error message couldn't. Namely, which assembly was causing conflicts.

    Or so I thought. The new app.config section seemed to indicate that there were NHibernate conflicts among various projects when I knew for a fact that we had exactly one copy of the NHibernate dll in the entire project.

    AssemblyGraph

    Here's where Reflector comes in. More specifically, here's where it's extensibility comes in. Through the use of the Assembly Graph add-in, I was able to generate a picture of the dependencies. (Click on the image to the right for a bigger version.) As you can see, all referenes to NHibernate are going to the same assembly.

    But as you can also see, there are three semi-related assemblies that aren't being referenced at all. Which means they can safely be removed from the various projects, yesno?

    You probably think that's foreshadowing's cooler cousin, foreboding, but it's not. The answer is yes, I was able to remove them with no ill effects.

    The reason I know there are no ill effects? Unit tests, baby! None o' this "let's take a stroll through the application and see if it still works" nonsense for me*. Remove the reference, run through the build process and watch the magic happen.

    The reason this is important: I'm pretty sure this add-in doesn't account for dynamically loaded assemblies. And while I'm also pretty sure we would have no reason to dynamically load some peripheral NHibernate assemblies, it's nice to have a suite of unit tests backing up that assertion.

    So to re-cap:

    • Unit tests good
    • Automated builds good
    • Reflector good
    • Assembly Graph add-in good
    • Subversion good (I haven't provided explicit evidence of this here but it's worth mentioning.)

    Kyle the Reflectively Assembled

    *Brian, if you're reading, I'm lying. I did actually go through each section and make sure it's working. But back me up if anyone asks.

  • DevTeach presentations explained

    I'm preparing my presentations for DevTeach and have come to realize that there is a price to pay for creativity. Here are the abstracts for three of my presentations.


    Guerrilla Refactoring

    Greetings, comrades. Welcome to the resistance! Our quarry today is a shamefully designed application that grows fat with duplication and crusty with hard-codedness. It feeds on itself behind a shield of corporate deadlines, each one stricter than the last. The cowardly code mocks us with cries of "We don't have time! We don't have time!" while features are grafted onto the application haphazardly with no thought to future reform!

    But fear not! We have studied our enemy and have altered our attack plan accordingly. No more frontal assaults. We must liberate the code in focused skirmishes from its bourgeois oppressors! Patiently, we shall advance, tightening our grip in an ever-widening net of unit tests as we hunt down bugs like the dogs they are!

    So join me, brethren and sistren! With our allies, the Design Pattern Legionnaires and the Dependency Liberation Front, we will train you to take your rightful place in the movement toward a better world, where features are not divided into tasks that are meted out to the "database gal" and the "guy that does the UI because he has PhotoShop installed". Rather, they will be fully and utterly DEMOLISHED by fully armed, multi-functional, and domain-inspired programmers full of KNOWLEDGE AND FURY!!!1!!!

    Viva La Revolucion!

    (adapted, of course, from my original post on the subject)


    Implementing a Brownfield Ecosystem: A Cultural Extravaganza

    So you missed Folklorama again, eh? Of course, you did. It's in Winnipeg. Well, fear not, coders! In this session, we'll throw so much culture at you, you'll think you're back in high school biology.

    A very important aspect of Brownfield applications is shifting the culture of your team. This starts with your project's ecosystem. Many developers in Brownfield applications don't even realize how many hoops they jump through during the course of a day to do simple things like checking in code and building the application. image

    In this session, we'll implement a full-fledged ecosystem for a Brownfield application. Working from the perspective of a single developer in a team, we'll cover topics such as the structure of your version control system, your check-in process, automated builds, and continuous integration. In the process, we'll talk about common pain points and common areas of friction that we can overcome with a few simple tool choices and a couple of mindset tweaks.

    I'll be getting by with a little help from my friends as we simulate a team environment using well-placed and impeccably groomed plants in the audience who will act as other developers in the team, some working with me, others not so much.


    Adding automated tests to an existing codebase

    It may seem like heresy but there are actual real-live applications out there inimage the world that don't have unit tests! No, really! And I know it sounds crazy, but you could be brought on to one of these applications and be tasked with adding automated testing to them. 

    Rare as these circumstances may be, it'll help to be prepared for them. Because it won't be as fun as you are probably imagining right now. For example, how do you automate a test for a web form that connects to a database, displays a form, then posts that data back to the database?

    In this session, we'll look at techniques for integrating tests into an existing codebase that has never had them before. When do you write tests for existing code? How do you test a mammoth method/class? How do you know if you aren't breaking anything else? For answers to all these questions and more, read Michael Feathers' Working Effectively with Legacy Code attend this session.

     


    You see my dilemma. I wrote these in a fit of imagination without stopping to wonder how I would differentiate one from the other. For example, how do I talk about refactoring without mentioning automated tests? How do I implement a brownfield ecosystem without doing same?

     

    In the end, I believe there will be overlap among the presentations. Indeed, I will probably use the same sample application for all three. I've justified it in my head by saying that the overlap is probably a good thing to help people see the interaction 'twixt the various concepts. Plus, it means I can focus on a single element in, say, the refactoring presentation and leave some of the details to the automated tests presentation.

    In any case, here is some clarification on the goal of each presentation:

    Guerrilla Refactoring: How can you refactor an application for maintainability when there is no explicit buy-in from the PM and/or the business (and/or the rest of the development team)?

    Implementing a Brownfield Ecosystem: A walk-through on adding source control, CI, and automated testing to an existing application. I'm planning to use plants in the audience to simulate a team environment to demonstrate what happens to you personally when others don't play by the rules. The "cultural" part comes because this is very much an exercise in changing your team's development culture. It was originally intended as a practical follow-up to Donald's Parachuting into a Brownfield Application presentation but I notice he's not doing it this time around.

    Adding automated tests to an existing codebase: Here, we'll dive deeper into automated testing for an existing app. It will be sort of a continuation of the brownfield presentation which will add the testing framework but not go any further. Admittedly, it will borrow heavily from Feathers' book but I see that as a good thing.

    For completeness, the fourth session I'm doing is on TDD for MVC applications. This one is more disparate than the others and should be a fun one as well, even if the abstract for it is the most boring of the four.

    Kyle the Synergistic

  • Protectionism via self-improvement

    A couple of things have got protectionism on my mind. It comes up surprisingly often because the Bahamas seems to be, by and large, a protectionism nation. That is based solely on personal observation, not on any sort of fact-checking. So I'll assume everyone out there knows the difference between valid journalism and wild blog-induced accusations based on rumours.

    Protectionism is the restriction of trade and industries by imposing regulations and tariffs to protect local companies and local interests. A few industries are already protected here. Lawyers, for example, must be Bahamian as do real estate agents, I think. And rumour has it, the IT industry is under consideration for the same treatment.image

    Some of this is understandable, given the country's small population, proximity to the US, and political history. And with the recent economic downturn, it gets even harder to discuss the issue without emotions running high.

    In any case, whether or not that's true is not something I can talk intelligently about. I am, after all, a foreigner here myself so I don't know that I could be an unbiased judge. I take some comfort in the fact that I am living and spending money here, but working for companies in the US and Canada.

    In any case, one of my goals with the BahaNET user group, which is my current outlet for organizing the software development industry here, is in direct response to this protectionist opinion and it applies anywhere, not just here.

    To wit: If you want to make sure your job doesn't go to anyone else (local or foreign**), then be better at it than anyone else. Don't give the company a reason to look elsewhere and they won't do it.

    This seems to be lost on many people. The natural inclination when someone else gets a job you were after (or that you had) is that they did something underhanded to get it. Instead of thinking, "Maybe I need to upgrade my skills," it's easier to assume that someone has ulterior motives and is working against you so that you can maintain the status quo.

    The decision to hire someone is usually pretty easy when comparing two people. The basic metric is: which one has more skill than the other? If the difference is obvious, then so is the decision. It's only when the candidates are relatively equal in skill level that other factors come into play.

    It's naive to assume this is always the case, of course. Some companies are more political than others. But it certainly tips the odds in your favour if you are good at what you do.

    That's why it's encouraging to see the same people coming out month after month to the BahaNET meetings to see what else is out there and to connect with other developers. Regardless of their actual skill level, it tells me that these people are willing to put some effort into making themselves better. So that when it comes to hiring them versus, say, a Canadian with similar skill level for which you'd need to buy an annual work permit, it's a no-brainer.

    ** Keep in mind that when I refer to a local or foreigner, I'm talking about status, not physical proximity. The "local vs. remote" thing is a whole different issue which I mention to pre-empt any accusations of hypocrisy based on previous posts on working remotely.

    Kyle the Unprotected

  • Sharing code styles in ReSharper

    Have just started a new project and my first task is some Silverlight thingamajig that I'm sure I'll go on about ad nauseum in the coming weeks. But first, let's talk about some ReSharper settings.

    My very first check-in in the code base was a ReSharper code style settings filimagee. This is a way of allowing everyone on the team uses the same settings. The documentation for this feature is here though it's a little out of date. To the right is a screenshot from version 4.1.

    This is somewhat important for me because I have some different ideas about whitespace than most people. And I don't want to impose my will on someone else. (At least not so crudely.) Nor do I want to alter my own personal projects just because everyone else likes to have all their code scrunched up together so that parentheses and curly braces essentially take the place of spaces and you need a &*%$ Little Orphan Annie Decoder Ring to parse out the actual meaning of these god-forsa--...

    Ummm...let's just say I'm a little set in my ways and move on...

    By default, your ReSharper settings are global across all projects. The two other settings are: a) Shared across the team, per solution, and b) Not shared, per solution, stored in a file.

    Both are very useful in teams where developers work on more than one project, such is the case here. The other obvious example is open-source projects. Here's a quick run-down of each.

    Shared across the team, per solution

    Here, the code style settings are stored in a .resharper file in the same folder as the .sln file. This is ideal for a non-intrusive way to enforce coding standards on a project. Developers download the source and go. They may not even know it's using a shared code style.

    MvcContrib uses this mechanism to enforce standards. I'm not on the new project because I've added .resharper files to my global SVN ignore list and I'm too lazy to change it. Plus, I'm not quite sure how it works between ReSharper 4.0 and ReSharper 4.1. I.e. is it able to manage users that use 4.0 and others that use 4.1? And what happens when one user changes the settings inadvertently? As an example, I just updated to the latest MvcContrib, opened it up, and now I have MvcContrib.4.1.resharper checked out for some reason.

    So instead, we're using the next option.

    Not shared, per solution, stored in a file

    With this option, you explicitly tell ReSharper where the settings are stored. It requires a little more work to set up. Unlike the previous option, the developer needs to go into the ReSharper settings and physically select this option and navigate to the file. It takes an absolute path, not a relative one.

    I'm guessing that this is because the settings to tell ReSharper which option to use are stored in the .resharper file, which as I've mentioned, we're not including in source control.

    Both options give you the ability to set standards like naming conventions, spacing, and, theoretically, how to re-order methods, fields, and properties within your class. I say theoretically because this particular one (under Type Members Layout) seems a little eccentric. I had to import the settings from the code style file, then shut down and restart the solution for them to take. And when I make changes to this section, they aren't always reflected in the settings file.

    None of this guarantees that your code will be consistent. After all, not everyone may have ReSharper. Also, I'm pretty sure the settings to auto-format are machine specific so someone could turn them off and then you have to rely on them to do an explicit auto-format before they check in. Furthermore, it appears that formatting profiles are also machine-specific, so even if you create one that runs through all your rules, re-orders things, and otherwise cleans up when you press Ctrl+Shift+Alt+F, that won't get propagated to anyone else's machine.

    Kyle the Styled

  • Epiphany on a plumber

    I told a condensed version of this story on Twitter but it bears repeating in a more rambling format.

    We replaced the reverse osmosis system on our water at home today. Old unit worked well enough I think but was crazy noisy and it was always a crap shoot to be able to get someone out to do maintenance on it. The maintenance schedule itself was up for debate depending on who answered the phone at the place where it was originally purchased.

    We replaced it with a unit recommended to us by our plumber. I like our plumber a lot not because of his skill, of which I have no point of reference to judge, but because of his passion. The guy loves talking shop when he's onsite. Whether it's upping the water pressure in our faucets by removing the flow restrictors (I recommend it for showers, not so much for sinks) or rating the options for new fixtures on our bidet (I bet you think I'm kidding about that one), he always talks with an enthusiasm that's infectious. Which is why I use him.

    So he shows up today and is busting at the seams with excitement, waiting to show me how the system works. As soon as he stepped in the door, he started opening the box like a kid at Christmas. And I should point out that this is not a small man. He's at least 6'4" and, shall we say, "sturdy".

    At this point, his eyes gleaming, he goes into the features and how it works and whatnot. He starts talking about intake valves and de-ionization filters and membranes and sediment and parts per thingy and pressure whatsits and quarter-inch flibberygibbits. All with as much crazed energy as you can stuff into a 6'4" plumber.

    As my eyes glazed over, it occurred to me: Is this what I sound like to users?

    Energy, enthusiasm, and passion are awesome pre-requisites, but let's not forget that we still have to talk to people. As easy as it is to dump "Excellent communication skillz" on your résumé, it pays off in dividends if you actually mean it.

    I'm not exactly an expert at communication. But I'm anal enough about it that my daughter is going to hate me when she hits high school English class. And I do lament the gradual laxing of rules with the proliferation of IM and Twitter.

    I like the power communication has. I love the way you can change perceptions by wording something differently. In my experience, choosing appropriate language is almost as much a predictor of success as the actual code. That's in your written documentation, your formal presentations, and in our everyday interaction with users *and* developers alike.

    To tie this back to my plumber, when he was done his verbal throughput on all things RO, I just looked at him like a good user and said: Whatever, man, just make it work.

    Kyle the Reversed

  • ASP.NET MVC and jQuery can now produce legitimate offspring

    You know this by now but my university English professor is a mean sucker and would hunt me down if I didn't provide context for this post. Microsoft will be shipping jQuery with ASP.NET MVC and Visual Studio. I imagine there will be lots to Google about this in the coming days but you can start at the source: ScottGu, ScottHa, John Ressig.

    I freakin' LOVE this news if for no other reason than I finally have a counter argument to the MVC mainstay complaint "what about my server-side controls?". According to John Ressig's post, Microsoft will be developing controls on top of jQuery. Combine that with the news that Visual Studio will have IntelliSense support for jQuery and you have yourself a happy codin' hillbilly.

    My underlying interpretation of this is that Microsoft will provide reasonable alternatives to the stock server-side controls using jQuery instead. Rob Connery has already provided a pretty detailed tutorial on creating a paged grid in MVC, and he didn't even use jQuery.

    Oh yeah, and this is a bold new direction for Microsoft and they're welcoming Open Source and the community and blah blah blah. Whatever, as long as I have something else to say besides "yeah, well, you smell" to people unwilling to give up their GridView.

    Kyle the Argumentative

  • Can you refactor to MVC?

    Not feeling too subtle today so I've given the punchline away in the title here. After wandering neck-deep into MVP and MVC, I'm now thinking about refactoring brownfield applications toward them. And it occurred to me: is it reasonable to refactor to MVC? Then, is it feasible? Then, is it even possible?

    I've built started three or four MVC applications and love the framework for greenfield apps. But assuming you have a nice, healthy (and I'm using the definition "of great size") web application, would you go through the exercise of converting it to MVC?

    Here are some reasons why I wouldn't.

    First and foremost, your URLs will change. Instead of http://myapp/ProductList.aspx, you have http://myapp/Products/List. Depending on the size of the app, I'm not sure I'd trust a global search and replace for that. Besides which, I'd rather be using one of the helper methods to generate the URLs in MVC.

    Granted, you might be able to get around this with some combination of HttpHandler or HttpModule. Whether or not there is enough benefit to be gained from MVC to go through this exercise probably depends on the size of the app, your deadlines, and just how much blogging material you want to generate.

    This segues into my next point. Unless you plan to refactor every page all at once, you need a way to have WebForms pages sitting along side MVC pages. Which means you need to be able to navigate directly to some .aspx pages in some cases, and to controller actions in others. This might be possible in the same project but it makes my head hurt thinking about it. You'll be giving the routing engine a hernia with all the heavy lifting it would have to do. Either that or, again, implement your own HttpModule before the routing module in the pipeline.

    Alternatively, you'd create a second web project, an MVC one, and move your UI over to it over the span of many moons. Your URLs will really get messed up as you'll be essentially working in two separate web applications.

    All of this presumes your logic is separated enough to withstand such a change. When talking about refactoring to layers in the brownfield book, we recommend doing so incrementally. That is, start by creating a seam and move all the code into it leaving a little bit behind in the UI. Then repeat for the big bulk of code you moved all the way down the line to the data access. (There, now you don't have to read Chapter 8.)

    Refactoring to MVC feels like this would be hard to do. Mind you, it shouldn't really be any harder than refactoring to MVP. Maybe just feels that way because of the brownfieldedness of the apps I have swirling in my head. (Full disclosure: many of which I contributed to.)

    Speaking of MVP, one of the main reasons I think this is too much work is because MVP is such a reasonable alternative. Maybe not 100% idea, but it's a very viable compromise. On the one hand, yes, you still have to deal with the ViewState and the ASP.NET Pipeline and all the happiness that comes from trying to separate concerns in a real application as opposed to the contrived examples in books and blogs that make it look so easy. On the other hand, it's less work to attain and in the end, you'll end up with a more maintainable application.

    So after all this windage about why I wouldn't refactor to MVC...has anyone...y'know...tried it?

    Kyle the Armchair Refactorer

  • ASP.NET MVC Sample Project: Suvius.Flamingo

    When presenting on ASP.NET MVC, I use an application I started nigh on two years ago when I was a wee whelp extolling the virtues of Atlas. It's no longer AJAX-y but it does demonstrate MVC and I've put it online at Google Code mostly because I've been promising I would for many months.

    It's a music catalogue that will extract metadata from all WMA and MP3 files in a folder of your choice and store it in a database. There are some simple controller actions used to query the catalogue in various ways. flamingo

    Check out the ReadMe.txt file for some tips on getting started. Note that you can't just download the code and open it up in Visual Studio. You need to run the build file at least once. There is a batch file that will make this easier the first time (clicktobuild.bat) but bear in mind that this *will* drop and re-create your database. And before you do that, you'll need to create a local-properties.xml file in the confi--...actually, just read the ReadMe.txt.

    There is a unit testing project in there that I added after the Edmonton presentation. The Edmug guys have whipped their city into shape there because when I said MVC is conducive to TDD, they called me out and said "Prove it, Plaid Man". Hardly complete coverage by any stretch but hopefully demonstrative enough.

    If you're semantically inclined, this does indeed mean I built the app without using TDD. There's actually a very good reason for that. You see, a plague of locusts appeared when I was first building it and they told me in locust-ese that if I used TDD, they would become a blight upon the land. I'm sure you see my dilemma.

    Final note on the name. Suvius is just a code name my brothers and I use and I've always liked the sound of it. Flamingo is the national bird of the Bahamas. How do they relate to an online music catalogue application? They don't. I've got a rant I've all but memorized on people's fixation on names but the short version of it is: Amazon, Sun, Java, Oracle, eBay, Facebook, Windsor, and Hibernate.

    I'd tell you to enjoy but that implies some sort of emotional attachment to the project.

    Kyle the Flaming-o

  • The Apple Buying Experience: Bahamian Edition (Part 1)

    Over the summer, the hillbilly sold his Canadian trailer to a pair of lovely ladies who I will assume are sisters even though they look nothing alike. A goodly pile o' the proceeds has been put to good use but I shan't elaborate.

    A nice little chunk of it has been reserved to upgrade the computers in the homestead. And for no other reason than "because I want to see what the hubbub is about", I'm looking seriously at Macs. (Actually, that's not true. I do plan to spend hours playing with Photo Booth.)

    To qualify that rationale, I will point out that my experience with Dell over the years has been good, bordering on excellent. I've had my share of Dells over the years and have ordered two at least bi-annually for a regular client.

    I won't lie by saying they've all worked flawlessly but, and here's the important part, Dell has been efficient at fixing the mistakes. And I mean crazy efficient. The client travels ten months out of the year and is not always in the most convenient location when things go wrong. But Dell has shipped replacement parts to countries I can't even pronounce or spell under some pretty stringent deadlines. Plus they have done warranty work here in the Bahamas on at least three occasions, which is a feat in itself. Compare that to my experience with Sony which was a lot more frustrating than that link lets on.

    Two nights ago, I ordered a replacement Inspiron for my wife through her company. Total elapsed time was about fifteen minutes. Dell ships direct to the Bahamas and calculates shipping, stamp tax, and duty accordingly. (There's no duty on laptops but there is on parts.) Like most international companies, their website experience still isn't quite up to par if you live in the Caribbean (for some inexplicable reason, the Bahamas is considered part of Latin America) but it's better than most.

    But just the same, I want to give a Mac a shot.

    Today, I finally started looking into logistics for getting an iMac out here. I started with the local computer store because I prefer to buy locally when I can. As luck would have it, they are an authorized Apple reseller. After a few minutes on the phone, the salesman came back with a price of just over $3550. Same computer on apple.com: $2318. To put that into perspective, I could fly to Florida, stay overnight in a nice hotel, buy my computer, and fly back for cheaper. That doesn't take into account the savings I'd get on peripherals, like a 1Tb Time Capsule, which the local company wants over $900 for. (To counter the servicing argument, the local company does provide service for Apple. And Dell. We've used them. They're actually pretty good.)

    Next stop: apple.com. That was an easy one. They are very clearly not set up to ship to the Bahamas.

    Third option was to phone Apple directly, which was entertaining. Trista was very sweet and perky until I asked "Is it possible to order a laptop and have it shipped to the Bahamas?" At that point, she got, let's say, "nervous". Actually, let's say, "jittery". Actually, it was downright frightened, like Steve Jobs himself was hovering over her shouldering saying, "You export that MacBook and I'm implanting a device into your body that will give you and the next three generations of your family permanent halitosis. And you know I can do it."

    Seriously, she went from "Perky Saleslady With 43 Pieces of Flair" to "Let me get out the CSR Manual of Canned Responses and read directly from the page on dealing with potential smugglers".

    A couple of weeks ago Scott Reynolds posited that Apple isn't ready for corporate adoption. Based on this experience, I don't believe they are ready for internationalhood. And I'll qualify that by saying at least not at the same level as Dell. I know you can get MacBooks at any local convenience store in Canada and the UK. But Dell makes it almost as easy to do the same in our rather under-populated neck of the woods.

    And I don't necessarily begrudge Apple based on this one experience. The reality is, the Bahamas is a country of 300,000 people. It may not make financial sense to deal with customs and import duties and stamp taxes and bribing government officials in order to reach out to the people. Better to leave it to local retailers who have a better sense of the demand.

    But don't equate an open mind to giving them a free pass. So far, they fit snugly into "mediocre" territory when it comes to the buying experience, and I'm still very early in the process. The only reason I tolerate it is because they aren't the only offenders by any stretch. I'm calling them out specifically because: a) I work with computers, and b) I expected more from them.

    Kyle the Un-macced

  • Do the right thing. Assuming you know what that is.

    I have kind of a love/hate thing going for Roy Osherove's blog. The "hate" part comes because he always challenges my perspective when I least expect it. Some of his posts seem like they are baiting people and it is easy to discount them as biased based on his position with TypeMock.

    But given what I think I know about him, I read these "inflammatory" posts with a different view. That is, as someone who is challenging the view of my personal echo chamber. I read tons of posts extolling the virtues of mock objects and Rhino Mocks. So it hurts my little brain to see someone I respect saying it's okay to keep your current "creative" design and still be able to test it.iStock_000000185043Small

    It starts, "Yeah, whatever. Like I'd ever do that." Then I start mulling it over and grumble to myself, "well, I guess that would have made sense in this past situation", then "actually, that makes sense for quite a few scenarios." Eventually, the train of though leads to my lying on my bed sobbing on the phone with my old employer to apologize for the colourful analogies I made to describe his project when I was unceremoniously let go for rocking the boat too much. Which is an interesting thing to account for when drawing up the timesheet for my current contract.

    Anyway, this Roy-love isn't the real reason for this post but hopefully, you're used to the hillbilly's verbose lead-ins.

    Roy's most recent post is another example of one that seemed bound from the beginning to give me a headache just from the title alone. But luckily, it touches on a subject I've thought about before, particularly when dealing with the fledgling Bahamian software industry.

    I won't paraphrase because chances are, you've read it already but the question it tickled in my mind was, "Are the best practices I've adopted over the last two years practical?"

    I've mentioned this a little before from the perspective of a small application for my family who can't afford a senior developer should I take up shark-baiting in the near future. And it's going to be hard to talk about this without sounding elitist so I'll call up the good will of my 98 previous posts at CodeBetter and hope y'all assume the best.

    I took JP's Nothing But .NET course over a year ago and had an absolute blast. But quite a lot of people struggled with it. Since then, I've made a more concerted effort to learn more about things like mocking, dependency injection, the SOLID principles, etc, etc, and so on and so forth. It hasn't always been easy but it's been tremendously rewarding. Learning all of it has made development fun again. Plus it's allowed me to connect with a ton of other people both as mentor and learner. And as a mentor, I've seen my share of people struggle with it. Even before I started, I saw a lot of people fight to understand things like AJAX calls, NAnt scripts, even CSS.

    By all accounts, these are reasonably bright people. They want to do a good job and are receptive to new ideas. But quite frankly, some of these things are hard. Let no one forget that learning how to properly use mock objects is *not* an easy task and until you "get" them, they will seem like unnecessary overhead. I resisted TDD for many a moon. Even today, it's still not quite second nature. That's mostly because I'm stuck in Livelink-land these days which contains code that would make Michael Feathers shake his head in defeat.

    imgWhen I entertain these thoughts, it's usually a battle between "should we cool our heels a bit until we hit the tipping point" and "should we keep going full tilt until the message starts getting across". As Roy mentions in his point, the learning curve is high. Do we keep pushing the learning so that more people get over it or do we lower the learning curve until most people get it, then raise it a little?

    I'd like to think we can do the first. The second seems like giving up. And worse, if we start to "dumb things down" in actual projects, it will be that much harder to actually lower the learning curve because no one will be pushing the boundaries. No Fluent NHibernate, no MvcContrib (or even ASP.NET MVC), no StructureMap. We've all seen what teaching to the lowest common denominator has done for the North American education system.

    Yes, we can put the onus on programmers to "do a better job" but let's face it, these people have lives. My dad is a land surveyor and my mom was a registered nurse. By all accounts, they were/are very good at their jobs and I remember very few instances of them taking their work home with them or advancing their learning outside of their work. With software development, it is almost expected that if you want to get better, you need to do it on your own time. And when you're done, you face going in to work to find that the rest of your team hasn't done their part and thus, are not amenable to the changes you want to make.

    So how far can we reasonably push people? The work needs to actually get done and it seems a good chunk of programmers still don't put any special effort into making code maintainable over the long term. Do we need to change the message? Or the medium? Are user groups and code camps doing their part or simply enforcing the status quo? Is alt.net making a difference or fragmenting the industry? Or simply being ignored?

    Will be interested in hearing people's comments on this as I actually have an over-arching reason for this line of questioning.

    Lord Tunderin' Jayzus, what a friggen essay this turned out to be. Now I gotta go dig up a couple of halfway-relevant images to balance out this tome. Serves me right for thinkin'.

    Kyle the Introspective

  • Feeling out Bahamas Code Camp possibilities

    What's the first thing you think of when I say "Bahamas in February"? If you said "code", then you're my target audience for this post.

    First, a recap of the BahaNET meeting last Thursday night. We finally hit double-digit attendance for the first time which is very exciting, even if the number includes yours truly. A midway surge of three people added 40% to the numbers to bring us to the coveted "ten people". It was very exciting. Especially considering the topic was version control.

    But that's not what I came here to talk about. Something that's been nagging in the back of my head for some time now is the possibility of an event in the Bahamas, hopefully next February or thereabouts. I don't want to call it a code camp just yet because I'm not sure that's what I want to do. A code camp seems local in scope and I'm still trying to figure out if we have the user base to justify it.

    Another option is to make it more summit-y. I.e. invite people from around the world for a nice little software development love-in. What can we offer that other conferences don't? Well, nothing from the professional development side of things. But just the same, I think we'd have a decent turn-out for some reason. I've had a *lot* of interest in this option. Not sure how much of that will translate to actual plane tickets being booked but it's encouraging nonetheless.

    There is a downside to the latter option that I think is pretty important. We would lose the local focus. I think there is still work to be done within the country before we start inviting software heavyweights down here to talk about functional programming and DDDD.

    Which leads me to another option, and I swear I had this in mind before Kaizen came out. Instead of a code camp where there is presentation after presentation, I'd like to hold two days (minimum) of practical workshops. A workshop would be four hours long and ideally, we'd have at least two or three "tracks", with at least one of them geared very much toward beginners. At least on the first morning.

    I believe this would offer the best of both worlds. The local development community could benefit from some hands-on training and the global community could pass on their knowledge in between margaritas on the beach.

    I do have a medium- to long-term plan for the industry in the Bahamas. Realistically, a full day of 75 minute presentations doesn't fit in with that goal right now. And as much as I would love to invite the world to the Bahamas for Caribbean TechFest so we can debate the pros and cons of IronRuby, that isn't going to help the fledgling software industry in my adopted homeland.

    I've been talking with some people rather vaguely about some of this already for some time and I suppose this is simply a way to try to crystallize things in a semi-permanent format. I'd be eternally grateful to anyone who can provide constructive feedback and/or advice on any of this as February is not quite as far away as I would hope.

    Kyle the Planned

  • MVC vs. MVP: A Hillbilly's Journey

    Yes, I know you've read this before so stop rolling your eyes and skip it if you're not interested. I'm not coming at this from the perspective of an expert imparting knowledge but as a hillbilly who has ignored the question too long. And now that I have to dive into it for the book, all the vagueness that I've been able to shunt aside to a little corner of my brain has surfaced like a long-lost brother beggin' for college money.

    So since I work from home without a lot of peers, my options are to talk it out here or to my daughter, who cries when her favorite singer is kicked off American Idol.

    Pre-requisite reading:

    Martin Fowler on GUI Architectures
    Martin Fowler on Passive View
    Martin Fowler on Supverising Controller
    Martin Fowler on Presentation Model
    Jeremy Miller on the difference 'twixt MVC and MVP

    I've looked through a number of other links but these are the ones I feel provide sufficient background and are suitably authoritative. Plus, I don't want to rehash the definitions of each pattern.

    So as I was waxing eloquent on Passive View and Supervising Controller, the thought crossed my mind, "I'm not seeing a lot of difference 'twixt these and my understanding of MVC. They have the same components and the diagrams look similar enough..."

    Martin Fowler's contrast between the two didn't help much:

    MVP uses a Supervising Controller to manipulate the model. Widgets hand off user gestures to the Supervising Controller. Widgets aren't separated into views and controllers. You can think of presenters as being like controllers but without the initial handling of the user gesture. However it's also important to note that presenters are typically at the form level, rather than the widget level - this is perhaps an even bigger difference.

    The problem stemmed from the way I'd explained the difference in my recent presentations on MVC. Specifically, I said that in MVP, the entry point was the View (aka. the aspx page) while in MVC, it is the Controller. While this is technically accurate, I now think it's a consequence of the *real* difference.

    Another thing blocking my understanding was that I spent too much time reading the theory and not enough time just reviewing my own code. I've implemented Supervising Controller in a web app before and my tag cloud seems to think I've done some work in MVC recently.

    If I had done that, it would have been quite a bit clearer. In the MVP patterns, there is a lot of synchronization and infrastructure code that is absent from MVC. The presenter and the view communicate back and forth a lot.

    For example, consider a button click in an MVP web page. Being a nice, decoupled view, it would probably look like this:

    protected void buttonFerment_Click( object sender, EventArgs e )
    {
        _presenter.Ferment( );
    }

    That is, the view passes the request on to the specific presenter its wired up to. And the presenter in turn would do whatever it needs to, then go back to the view and ask it to update its UI.

    Now let's see what the button click event would look like in an MVC page...

    ...except that you wouldn't actually see a click event in an MVC page. In fact, thinking back to the MVC projects I'm working on, the views don't even have code-behind files.

    MVC seems to favour a purer separation of the View and Controller. From Jeremy's post above:

    [In MVC, there] is typically little or no direct communication between the View and Controller.

    Which is definitely not the case with the flavours of MVP. This also jibes with Chad Myers' explanation on Twitter.

    It sounds like the MVP patterns are better suited for stateful environments (or stateless environments that someone has gone to great efforts to make appear stateful). MVC is shinier for stateless environments because the controller can collect up the model, dump it to the view, then call it a day. The view doesn't so much launch events as it does tell a front controller that it wants to do something. The front controller re-routes the request to whatever controller is supposed to do the actual work. (Thanks to Tom Opgenorth for helping me sort out that bit.)

    Going back to my stock explanation of the differences, I said that the view was the entry point for the MVP patterns and the controller was for MVC. As I mentioned, this is accurate but it's a consequence of MVC having the front controller. Events in MVP are handled by the same page that launched them. In MVC, they are handled "globally" by the routing engine (aka. the front controller).

    Finally, Bil Simser summed it up best when he said

    Mvc lets me focus on the business problem and not how to update a view

    So my ultimate understanding of the difference is that it is based on how intimate the View and Presenter/Controller are. In MVP, they are cosy and familiar, like cousins. In MVC, they are cold and distant, like lawyers.

    Thank ye all for your kind attention while I worked this out in my head. If it has been helpful to anyone else, that's not my fault.

    Kyle the Controlled Presenter

  • BahaNET meeting this Thursday, September 11

    Quick note to announce that Hurricanes Hanna and Ike haven't dampened our resolve! BahaNET is meeting this Thursday, hopefully at the usual place but I haven't quite verified yet.

    Discussion topic will be source control. Specifically, how to set it up, maintain it, and use it effectively. It's a topic that is often overlooked but can become very cumbersome very quickly when not done properly. I'd tell you war stories but I'm guessing I can't top yours.

    There will also be some interesting side topics so if you're in the area, it'll be a good one to attend.

    Hope to see you all there.

More Posts Next page »

Our Sponsors

Proudly Partnered With