Thursday, January 28, 2010

Wiring Up Subsonic via StructureMap

This post is a guide to configuring Subsonic with Structuremap to ensure the easiest TDD experience possible.

The first thing I did was generate the code using the SubSonic Linq Templates.

Next, it’s just a matter of telling StructureMap all about how to make an instance of SubSonicRepository<T>.

Here’s the goo that you need to make it all wire up:

        public static void ConfigureStructureMap()

        {

            ObjectFactory.Initialize(x =>

                {

                    x.UseDefaultStructureMapConfigFile = false;

                    x.Scan(scan =>

                    {

                        scan.WithDefaultConventions();

                        scan.TheCallingAssembly();

                        scan.AssemblyContainingType<IRepository>(); //Scans the Subsonic.Core assembly

                    });

                    x.ForRequestedType<IQuerySurface>().TheDefault.Is.ConstructedBy(c => new SomeDB()); //Gen'd by SubSonic

                    x.ForRequestedType<IDataProvider>().TheDefault.Is.ConstructedBy(c => ProviderFactory.GetProvider("A_Name_Of_A_Connection_In_Config"));

                    x.ForRequestedType(typeof(IRepository<>)).TheDefaultIsConcreteType(typeof(SubSonicRepository<>));

                }

            );

        }

  1. IRepository<T> and SubSonicRepository<T> are both shipped in the SubSonic.Core DLL, so there’s no real work to do there.
  2. SomeDB() is a class generated by the Linq Templates mentioned above.
  3. The string passed to ProviderFactory.GetProvider(“”) is just the name of a connection that you define in your .config file.  You’ll see all this in the examples you downloaded when getting the bits.
  4. The last line of the config tells StructureMap to map the open generic type to a concrete generic type.  For instance, when you request IRepository<Customer> StructureMap will give you a Repository<Customer>.

Now you can use constructor injection or ObjectFactory.GetInstance<T>() to get a concrete ready-to-go SubSonic repository.

Sunday, January 24, 2010

Values – Practices - Tooling

Something I heard recently really resonated with me and has already guided me on a few decisions lately.  I’m writing it down here because this idea has really caused a revolution in my mind.

The quote is this:

Values lead us to

Practices which mandate

Tooling

This envelops a lot of thinking and emotion I’ve had in my career as I’ve made choices about how to develop software that I haven’t been able to verbalize.  When I was young in my career as a developer I had that phrase completely inverted.  I made most of my decisions based on the tooling I was given.  For example: when I discovered how to generate 5000 lines of source code to do my database access I immediately leapt and made all my design decisions around that optimization.  Even when my code got complex and unreadable I just kept on thinking about how much time that generated code saved me.  “Certainly the time I’ve saved is paying for the sins I’m committing as I add line number 687 to this method!”

What are My Values?

After introspecting on this question I came up with 5 core values I have as a software craftsman (in no order):

  1. Quality (clean code that works)
  2. Simple designs
  3. Learning
  4. Creativity
  5. Team

These values have led me to these practices:

  1. Code designed by testing (TDD)
  2. Question abstractions before implementation
  3. Simple team communication mechanisms
  4. Continuous integration
  5. Practice (Execute, assess, learn, repeat)
  6. Improving
  7. Teaching

And ultimately because of these practices I use these tools:

  1. Low-friction source control – supports continuous integration and team integration
  2. Kanban board – supports simple communication
  3. Keyboard shortcuts wherever possible – continuous improvement
  4. Automated scripts - to take care of tasks requiring no creativity
  5. Frameworks that make testing user code easy
  6. Continuous integration applications
  7. …and other frameworks/libraries/IDEs/languages that support my core values

These tool choices have to be constantly re-evaluated.  The idea is that I’m constantly pushing to make the optimal solution feasible so that I’m supporting my values as closely as I can. 

Hat tip to Dave Laribee and Steven Harman for sharing this thought at Codemash 2010!