Skip to content

Instantly share code, notes, and snippets.

@ndonze-zz
Created November 27, 2013 13:50
Show Gist options
  • Save ndonze-zz/7675989 to your computer and use it in GitHub Desktop.
Save ndonze-zz/7675989 to your computer and use it in GitHub Desktop.
This is pretty rough. There's a lot of thrashing as I experiment with this. I apologize if I'm missing something fundamental (which I likely am).
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
public class PartialAppPlayground
{
public static void Initialize()
{
Func<IDbConnection> createConnection = () => new SqlConnection();
var queries = new List<Func<IMessage, IHandlerResult>>();
// Will not compile. Method signature would have to specify IMessage
// instead of UsersByLastNameQuery, which has data required by the Handle
// method; could cast inside Handle, but not sure if I want to go down
// that road
queries.Add(message => UsersByLastNameQueryHandler.Handle(message, createConnection));
// Ideally, would like to be have everything registered to the point of being able
// to call from MVC app as:
var query = new UsersByLastNameQuery { LastName = "Anderson" };
var result = someApplicationObject.Handle(query);
// Would also like to be able to sub dependencies for testing
Func<IDbConnection> createTestConnection = () => new SqlConnection();
someApplicationObject.RegisterHandler(message => UsersByLastNameQueryHandler.Handle(message, createTestConnection));
}
}
public class UsersByLastNameQuery : IMessage
{
public string LastName { get; set; }
}
public class UsersByLastNameQueryHandler
{
public static UsersByLastNameQueryResult Handle(UsersByLastNameQuery query, Func<IDbConnection> createConnection)
{
using (var connection = createConnection())
{
//SQL
return new UsersByLastNameQueryResult();
}
}
}
public class UsersByLastNameQueryResult : IHandlerResult
{
}
public interface IMessage
{
}
public interface IHandlerResult
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment