Posts

Showing posts from July, 2013

Service Locator vs Dependency Injection

While I've been exclusively using DI since I wrote articles like this one or this one (and several others on the subject of Service Locators), I think I finally figured out how to explain why DI is better: it's because it forces a design change in classes. Start with the normal mess, where your method directly initializes a concrete class, let's say in the constructor: public class DoesSomethingThatRequiresLogging { public DoesSomethingThatRequiresLogging() { logger = new FileLogger(@"c:\temp\log.txt"); } // private FileLogger logger; } Your methods happily call logger.Log(...) and all is good with the world. However, at some point you have to test this, or reuse it, or change it to log to a database table instead of a file. Ouch. The first solution is the use of a service locator. Create the logger outside of the constructor and use it as required: // in Main ServiceLocator.Register(new FileLogger(@"c:\temp\lo...

Posting dynamic Master / Detail forms with Knockout

Image
I have the following dynamic form in my GitHub Inventory project : I created the form with KnockoutJS , using some ideas from this article . The detail view looks like this: <table id="acquisition_items" class="jtable"> <thead> <tr> <th>Product Name</th> <th>Quantity</th> <th>Price</th> <th>Value</th> <th><button data-bind="click: addItem">[+]</button></th> </tr> </thead> <tbody data-bind="foreach: items"> <tr> <td> <input data-bind="value: ProductName" type="text" value="" /> </td> <td> <input data-bind="value: Quantity" type="text" value="" /> </td> <td> ...