Improving “Boiler Plate” Data-Reader Code – Part 4

In part 3 we created a SQL repository object that took a populated instance of IQuery to select/return an enumerable list of objects. A limitation of the repository was that the query had to be text-based, it couldn’t handle stored procedures and/or parameters. By incorporating an abstract factory pattern we can extend the functionality to handle different types of query.

The original code inside “SqlRepository.Get(…)” needs to be changed from:

[code language=”csharp”] using (var command = connection.CreateCommand()) { command.CommandText = query.Text; connection.Open(); [/code]

To:

[code language=”csharp”] using (var command = connection.CreateCommand()) { var handler = QueryHandlerFactory.Create(query); handler.Assign(command, query); connection.Open(); [/code]

The static factory class takes an instance of IQuery and determines which “query handler” to return depending upon the additional interface that the “passed in” query implements. This is implemented via the following code:

[code language=”csharp”] public static class QueryHandlerFactory { public static IHandleAQuery Create(IQuery query) { if (query is IDefineCommmandTextQuery) { return new HandleCommandTextQuery(); } var ex = new NotSupportedException(); ex.Data.Add("IQuery Type", query.GetType()); throw ex; } } [/code]

To finish off the implementation the following new interfaces and code are needed:

[code language=”csharp”] public interface IDefineCommmandTextQuery : IQuery { string Text { get; } } public interface IHandleAQuery { void Assign(SqlCommand command, IQuery query); } public class HandleCommandTextQuery : IHandleAQuery { public void Assign(SqlCommand command, IQuery query) { command.CommandType = CommandType.Text; command.CommandText = ((IDefineCommmandTextQuery)query).Text; } } [/code]

Finally, we simplify the IQuery interface as it’s only property has now been moved up into IDefineCommandTextQuery and then update all concrete implementations of “IQuery” to “IDefineCommandTextQuery ” as well, without these final changes the factory class will not be able to correctly determine the handler from the interface that is implemented.

[code language=”csharp”] public interface IQuery { } public class GetAllCustomersQuery : IDefineCommmandTextQuery { public string Text { get { return "SELECT id, Firstname, Surname FROM Customer"; } } } [/code]

Now our code can be safely extended to handle different query types just be creating the new implementation of the query handler and modifying the factory to handle the identification and creation of the new type. Part 5 will show how this functionality can be extended to handle stored procedures with parameters.

Similar Posts

  • Improving “Boiler Plate” Data-Reader Code – Part 5

    In this post, we will extend the query functionality to handle stored procedures with parameters. To do this we need to create a new query type interface with an example implementation: [code language=”csharp”] public interface IDefineAStoredProcedureQuery : IQuery { string StoredProcName { get; } IList<SqlParameter> Parameters { get; } } public class GetCustomersByFirstName : IDefineAStoredProcedureQuery…

  • Visual Studio 2012

    One thing I’ve found over time is you can never find licensing details for VS and SQL products when you need them, so here’s the link for VS2012 Licensing details I quite like what Microsoft is doing with these white papers, the use case examples are really useful and answer most usage questions.

  • Starting Windows Workflow v4.0

    Today I’ve started learning about Windows Workflow v4.0, I’m hoping that it will help out with a current project. The original requirement was for mapping real-world business processes to what would normally be complex long-running computer tasks. Whilst investigating what it could do I’m beginning to think there could be real value using it for…

  • Add NuGet Reference in Resharper

    Today Jetbrains announced on their blog that they’ve released a resharper nuget package that will obtain project references via nuget, rather than making direct references to the locally installed code. As we are currently in the early stages of transitioning from VS2008 to VS2012 we aren’t ready to start using NuGet(*) but I think I’ll…

  • Playing with AutoMapper

    In my current role, we have come across a requirement to map message objects to more complex domain entities.  As anyone that has done this previously will quickly tell you, manually writing code to do this is very boring and repetitive.  With very limited time and resources, that effort could be better spent elsewhere on…

Leave a Reply

Your email address will not be published. Required fields are marked *