Josh Schwartzberg high-fives the CLR

and spouts random buzzwords

  • ASP.NET MVC for the php/asp noob

    I was talking to a friend today, who's foremost a php developer, about his thoughts on Umbraco and he said "Well they're apparently working feverishly on the new version of Umbraco, which will be MVC... which i still don't know what that means, but I know you like it."

    I ended up giving him a ground up explanation of ASP.NET MVC, so I'm posting this so he can link this to his friends and for anyone else who finds it useful.  The whole goal was to be as simple as possible, not being focused on proper syntax.

    Model-View-Controller (or MVC) is just a pattern that is used for handling UI interaction with your backend.  In a typical web app, you can imagine the *M*odel as your database model, the *V*iew as your HTML page, and the *C*ontroller as the class inbetween. 

    MVC handles your web request different than your typical php/asp app.
    In your php/asp app, your url maps directly to a php/asp file that contains html, mixed with database access code and redirects.
    In an MVC app, your url route is mapped to a method on a class (the controller).  The body of this method can do some database access and THEN decide which *V*iew (html/aspx page) should be displayed;  putting the controller in charge and not the view... a clear seperation of concerns that provides better reusibility and generally promotes cleaner code., a quick example:
    Let's say you hit the following url in your application:

    To avoid tedious configuration, MVC uses a lot of conventions by default. For instance, the above url in your app would automatically make MVC search for a .net class with the name "Product" and a method named "ShowItem" based on the pattern of the url.  So if you name things properly, your method would automatically be called when you entered the above url.  Additionally, it would automatically map/hydrate the "int id" parameter that was in your querystring, matched by name.
    public class Product : Controller
        public ViewResult ShowItem(int id)
            return View();

    From this point you can write the code in the body of this method to do some database access and then pass a "bag" (also known as the ViewData) of data to your chosen *V*iew (html page) to use for display.  The view(html) ONLY needs to be worried about displaying the flattened data that it's been given in the best way it can;  this allows the view to be reused throughout your application as *just* a view, and not be coupled to HOW the data for that view get's loaded..

    public class Product : Controller
        public ViewResult ShowItem(int id)
            var database = new Database();
            var item = database.GetItem(id);
            ViewData["TheItem"] = item;
            return View();

    Again by convention, since the class' method name is "ShowItem", it'll search for a view named "ShowItem.aspx" by default, and pass the ViewData bag to it to use.

           var item =(Item)ViewData["TheItem"]
           <h1><%= item.FullProductName %></h1>

    They aren't... here are some other frameworks you may have heard of that use the same pattern in a their own way:

    • Ruby On Rails
    • Grails
    • Spring MVC
    • Struts
    • Django




  • Monitoring settings in a configsection of your app.config for changes

    The usage:

    public static void Main()
        using(var configSectionAdapter = new ConfigurationSectionAdapter<ACISSInstanceConfigSection>("MyConfigSectionName"))
            configSectionAdapter.ConfigSectionChanged += () =>
                Console.WriteLine("File has changed!  New setting is " + configSectionAdapter.ConfigSection.MyConfigSetting);
            Console.WriteLine("The initial setting is " + configSectionAdapter.ConfigSection.MyConfigSetting);

    The meat:

    public class ConfigurationSectionAdapter<T> : IDisposable
        where T : ConfigurationSection
        private readonly string _configSectionName;
        private FileSystemWatcher _fileWatcher;
        public ConfigurationSectionAdapter(string configSectionName)
            _configSectionName = configSectionName;
        private void StartFileWatcher()
            var configurationFileDirectory = new FileInfo(Configuration.FilePath).Directory;
            _fileWatcher = new FileSystemWatcher(configurationFileDirectory.FullName);
            _fileWatcher.Changed += FileWatcherOnChanged; 
            _fileWatcher.EnableRaisingEvents = true;
        private void FileWatcherOnChanged(object sender, FileSystemEventArgs args)
            var changedFileIsConfigurationFile = string.Equals(args.FullPath, Configuration.FilePath, StringComparison.OrdinalIgnoreCase);
            if (!changedFileIsConfigurationFile)
        private void ClearCache()
        public T ConfigSection
            get { return (T)Configuration.GetSection(_configSectionName); }
        private System.Configuration.Configuration Configuration
            get { return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); }
        public delegate void ConfigChangedHandler();
        public event ConfigChangedHandler ConfigSectionChanged;
        protected void OnConfigSectionChanged()
            if (ConfigSectionChanged != null)
        public void Dispose()
            _fileWatcher.Changed -= FileWatcherOnChanged; 
            _fileWatcher.EnableRaisingEvents = false;


  • My short answer to "If no other engineering discipline does it first, why should I TDD?"

    I agree Software Development by the truest form of the word, is in fact Engineering, but not all parallels can be made to its hardware counterparts, especially in its current state of maturity.   We can build high amounts of complexity with few people in a very short period of time.  All of which relies on other systems that are relatively unproven and unstable, it's hard to have a proper engineering process in these conditions.

    This is why we need better code that can safely react to change.  In my opinion, "better" code in statically typed languages is inherently easier to test.   Its more isolated/abstracted and cohesive for its purpose.. and so are its supporting unit tests.   This ensures small and large changes alike have minimum impact on the existing, unrelated code... without a nasty ripple effect.

    TDD is just one ritual to writing code that I loosely term "better", it's definitely not an end-all.  Clearly it's easy to assume writing more tests is the only correlation in less buggy software, but what the paper(s) don't show in numbers are the increased agility in the software process as every day brings a new change to the design that can be made more safely. 

    There is still a large human aspect to this whole process, and given short-term cost-benefit analysis', many times the code quality suffers; building rigid, new untested features that only cause growing pains and degrades the testing ritual that were initially put in place.  That said, the cost-benefit analysis' do have to be made, and I wouldn't say that all bugs are worth fixing or can be fixed for a timely release.   But you have to ask what the long-term cost is to a buggy system that becomes difficult to test or change without fear.

    I'd like to hear everyones opinion on the matter.


  • Silverlight BringIntoView() extension method (with OnGotFocus behavior)

    It all started because I couldn't find a way to automatically scroll any element into view in Silverlight (a feature
    that exists in WPF).  I take that back, I could get the job done with a ListBox's ScrollIntoView(ListBoxItem item)
    method, but I hardly wanted everything on my screen to be wrapped as a ListBoxItem; it feels as dirty as it sounds.  

    Anyways, here is the code.

    /* Extension Methods */
    public static class FrameworkElementExtensions
        private const int ScrollPadding = 10;
        public static void BringIntoView(this FrameworkElement frameworkElement)
            var parent = VisualTreeHelper.GetParent(frameworkElement);
            while(parent != null)
                parent = VisualTreeHelper.GetParent(parent);
                var scrollViewer = parent as ScrollViewer;
                if(scrollViewer != null)
        public static void BringIntoViewForScrollViewer(this FrameworkElement frameworkElement, ScrollViewer scrollViewer)
            var transform = frameworkElement.TransformToVisual(scrollViewer);
            var positionInScrollViewer = transform.Transform(new Point(0, 0));
            if (positionInScrollViewer.Y < 0 || positionInScrollViewer.Y > scrollViewer.ViewportHeight)
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + positionInScrollViewer.Y - ScrollPadding);


    Bonus Behavior! (Behaviors?!? Here is an Introduction).
    This behavior was created ensure that as a user tabs through the screen, scrolling automatically takes place.

    Note: If you want to use behaviors in Silverlight, install the Blend 3 SDK and reference

    /* Behavior class  */
    public class BringIntoViewOnFocusBehavior : Behavior<FrameworkElement>
        protected override void OnAttached()
            AssociatedObject.GotFocus += OnGotFocus;
        protected override void OnDetaching()
            AssociatedObject.GotFocus -= OnGotFocus;
        private void OnGotFocus(object sender, RoutedEventArgs e1)


    <!-- XAML usage of custom behavior -->
                    <behaviors:BringIntoViewOnFocusBehavior />


  • MVC/JQuery meets X-Mas Music Project

    My friend Dave and I embarked on a project a few weeks ago, initially just meant to be an audio CD for our family, that included some rockin' versions of everyone's christmas favorites.  As we started to record it, we got more and more excited about how it might turn out.  Being the uber code master that I am, I opted to make a website... then in a blink, a friend who is incredible at Flash offered to help. This turned out to be a great experience for all of us, and the feedback has been tremendous. 

    I'm very proud to share with you (make sure your speakers are on) 

    The code-behind
    I wanted to have a spot for friends, family, and haters to post their honest ramblings, so I decided to go with the much-loved JQuery/ASP.NET MVC combo; what a joy as always.  One bug I ran into, that seems to have been noticed before me, is the ability to cache the root page of your ASP.NET MVC site using the OutputCache attribute.  According to this thread, the mvc team will have a fix soon.  For the time-being, I ended up caching at the repository level by just sticking the data response from disk into the HttpContext cache to ensure I only read and parse my xml file every 10 seconds.

    Nothing else too interesting came up. I added some spam protection, input validation (client and server-side), and HtmlEncoding to protect any hijacks.  All in all, another great experience.  I would share the code, but I have a certain Oxitephobia.

    In the hopes (and delusions of grandeur) of this thing being picked up as even a semi-viral hit.  I wanted to make sure the site wouldn't go down.  I placed the flash file, the four mp3 files, the images, and the css file on a CDN.  I initially started with Amazon S3... but at 15cents/gig compared to SimpleCDN's 6cents/gig I had to switch.  I've been very happy with my experience, and they even include $15 worth of free credit when you first sign-up.  A friend of mine suggested I use the YSlow plugin to analyze my site; this ended up with the following configuration:

    • HTTP Compression enabled on CDN (via url configuration) and IIS (c'mon, if you haven't done this yourself already, DO IT NOW)
    • Set content expiration on all files on CDN to have 10 year expiration headers (I named the files themselves with version numbers to allow for forcefully expiring if needed)
    • JQuery and SWFObject scripts hosted via google
    • All scripts at the bottom
    • XHTML 1.0 Strict validated by W3

    This ended me with an A score with YSlow.  I felt accomplished.

    Parting thoughts
    I hope you enjoy it, it's definitely meant to be funny - so don't take it too seriously.  Feel free to pass it along to your friends and family!


  • Hiring! Tampa Bay Developer

    Are you a highly motivated .NET code monkey developer in the Tampa Bay area with a passion for software and the ability to learn quickly?



    Come join our top-notch team of developers who utilize the latest technologies (JQuery, NHibernate, CruiseControl, Nant, Resharper, etc.) to build the essential tools for the nation's largest and fastest-growing audiovisual firm.

    The ideal candidate will have:

    • Minimum 2 years experience developing applications.
    • Minimum 1 year experience with C# in ASP.NET and/or Windows Forms.
    • Strong experience with MS SQL Server.
    • Solid skills and knowledge of HTML/DHTML, JavaScript and XML.
    • Excellent written and verbal communication skills.
    • Ability to learn quickly, as well as think and work independently.
    • Passion for learning new technology and implementation techniques
    Apply now and come play with us


  • NHibernate querying without mapping inverse relationships

    In my neverending quest to keep my domain minimal, I ran into a query that I wanted to perform with NHibernate that seems to be impossible without adding an additional property and hbm mapping definition.

    Note: I still consider myself rather new to NHibernate, so this might have an obvious answer.

    Let's say I have following two classes:
    public class School : EntityBase
         public string Name { get; set; }
         public IList<Student> Students { get; set; }

    class Student : EntityBase

    public string Name { get; set; }

    The School.hbm.xml file contains this in the body:
    <bag name="Students">
    key column="SchoolId"/>
    one-to-many class="Student"/>

    The database representation of the above looks something like this:
    Db representation

    Without making any changes to my classes/mappings, I can perform this SQL query to retrieve all Students who's name contains 'Powers' that belong to a school who's name contains 'Middle':
    SELECT * FROM Students
    .Name LIKE '%Powers%'
    Students.SchoolId IN (SELECT School.Id FROM Schools WHERE Schools.Name LIKE '%Middle%')

    So - Here is the question, how can I do this in an HQL query without making any changes to my domain (which would be specifically adding a School property to the Student class and defining the relationship in the Student.hbm.xml file)?