April 2006 Entries

XPath Debugging Visualizer

 

This morning I put together an XPath Visualizer for an XmlDocument to be used within Visual Studio 2005. This comes from waking up at 4 a.m. and not falling back asleep. Any suggestions? Here are a few screenshots:


XPathVisualizer 1


XPathVisualizer 2
XPath query during debugging. Viewing the Xml document while debugging.

Source is available here. Installer is available here.

Extending Enterprise Library 2.0

 

Brian Button, formerly of the Patterns and Practices team just held a webcast covering how to provide custom design time extensions to the Enterprise Library console. I missed his session, however after reviewing the slide deck which he has provided, extending Enterprise Library appears much easier than when I had to do it only a year ago. Definitely worth checking out if you would like to integrate your own custom extensions within Enterprise Library.

Service Locator with System.ComponentModel

 

Javier mentioned from my previous post that it would be nice to be able to request an object directly from .NET, without a reliance on the Spring Framework. To a certain extent, this is provided in the System.ComponentModel and System.ComponentModel.Design namespaces. You can create a ServiceContainer and register services with it, then if you expose your ServiceContainer as an IServiceProvider, your client can then request services back from the container. It looks something like this:

IServiceContainer sc = new ServiceContainer();
sc.AddService(typeof(IDatabase), new Database());
// Add more services
IServiceProvider provider = sc as IServiceProvider;

Now, within another layer, assuming we have the IServiceProvider, I can do something like this:
IDatabase db = provider.GetService(typeof(IDatabase));

Inversion of Control with Spring.NET

 

I have previously mentioned using the Spring Framework for the AOP support the framework provides, however Spring is deeply rooted in providing dependency injection or Inversion of Control (IoC). Spring uses a configuration file to manage type relationship mappings to objects, which can be stored in its own external XML file, an embedded resource within an assembly, or directly within your web.config or app.config file. For a simple example, we will define our own XML file (spring.xml), with the following contents:

 

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="Database"
singleton
="false"
type
="SpringDemo.Database, SpringDemo">
<constructor-arg ref="SqlConnectionObject" />
<
constructor-arg ref="SqlCommandObject" />
</
object>
<object name="SqlConnectionObject"
singleton
="false"
type
="System.Data.SqlClient.SqlConnection, System.Data">
<constructor-arg type="System.String"
value
="Data Source=demo;Initial Catalog=pubs;User Id=sa;Password=pwd;" />
</
object>
<object name="SqlCommandObject"
singleton
="false"
type
="System.Data.SqlClient.SqlCommand, System.Data">
</object>
</objects>

With this example we define three objects; a Database, a SqlConnection and a SqlCommand. From this, we can provide Spring with our resource file and create an XmlObjectFactory that will allow our application to request services from it.

 

using System;
using
Spring.Core.IO;
using
Spring.Context;
using
Spring.Context.Support;
using
Spring.Objects.Factory;
using
Spring.Objects.Factory.Xml;

namespace
SpringDemo
{
class Program
{
static void Main(string[] args)
{
IResource resource
= new FileSystemResource("spring.xml");
IObjectFactory factory = new XmlObjectFactory(resource);
if
(factory != null)
{
IDatabase db
= factory.GetObject("Database") as IDatabase;
if
(db != null)
{
if (db.Connection != null)
{
Console.WriteLine(db.Connection.ConnectionString)
;
}
}
}
Console.WriteLine(
"Done");
Console.Read();
}
}
}

Note that when the "Database" is requested from our XmlObjectFactory, Spring identifies that it has two dependent objects, a SqlConnection and a SqlCommand object which need to be constructed prior to creating the "Database" object. In fact, the Database class has a constructor that takes an IDbConnection and IDbCommand. When the SqlConnection object is created, it uses the overloaded constructor to pass in a connection string which was defined as a constructor argument when the SqlConnection was created. Thus all dependent objects are created and passed into the Database object. Source code can be downloaded here.

Ruby on Rails and .NET

 

There has been a lot of hype out in the development community over the past year regarding Ruby on Rails, even with Ruby alone. For those that are unaware, Ruby on Rails is a full stack web framework written in Ruby. While I've only read the first 100 pages of Agile Web Development with Rails, I can say that RoR is very intriguing. At a high level, RoR relies on a MVC architecture, using the ActiveRecord design pattern for data persistence. I've found radrails to be an excellent IDE for RoR if you're developing on a Windows machine. If you find that you really enjoy Ruby, but work in .NET, I'd suggest looking into RubyCLR created by John Lam.

Back to .NET, the Castle Project, where DynamicProxy lives (this is used in Rhino Mock to intercept mock interface calls); is also host to several other interesting projects relative to the .NET world. MonoRail is an attempt to provide much of the functionality seen in RoR within the .NET environment. I can't help but wonder if we will see something come from Microsoft in an attempt to fill this space with a commercial product similar to how they have provided a testing framework in VSTS and their ATLAS implementation that is soon to be released all of which were rooted in open source products.