The Inquisitive Coder – Davy Brion's Blog

Trying to walk that thin line between intelligence and ignorance

Archive for June, 2008

Testing batched Service calls

Posted by Davy Brion on 29th June 2008

Now that we can batch WCF calls, and do so with readable code we still need a way to test this.

Suppose that we have a page where we need to display a dropdown list of product categories and suppliers. This is the code in the controller of that screen for the Load event:

            if (!View.IsPostBack)

            {

                var batcher = new ServiceCallBatcher(service);

                batcher.Add("categories", new GetProductCategoriesRequest());

                batcher.Add("suppliers", new GetSuppliersRequest());

                View.ProductCategories = batcher.Get<GetProductCategoriesResponse>("categories").ProductCategories;

                View.Suppliers = batcher.Get<GetSuppliersResponse>("suppliers").Suppliers;

                View.DataBind();

            }

When i’m testing my controllers, i like to use mocked service instances, and i set expectations on the methods that should be called and the parameters they receive. With my batching technique, i don’t really execute specific methods on the service because the batcher calls this method:

        Response[] Process(params Request[] requests);

My Request classes are really simple, and to keep them simple they don’t override the Equals method. This makes it hard to set expectations because the Request instances are created by the controller, and i can’t set expectations on the Process method using Request instances that would equal the Request instances that were passed to the batcher by the controller. This makes it hard to test that the service is called correctly. If a Request type contains properties (which are really service method parameters) you really want to be able to test that those properties contain the correct values. Also, you want to make sure that the correct Requests are sent to the service. But if they’re handled by this very generic Process method, it makes it hard to verify correct usage during a test.

So how can we properly test the code listed above? I came up with the following approach. First we need a class that we can use to set up the Response instances to return from the service’s Process method, and to capture the Request instances that are passed to the Process method:

    public class ServiceRequestResponseSpy

    {

        private Request[] receivedRequests;

        private Response[] responsesToReturn;

 

        public void SetResponsesToReturn(params Response[] responses)

        {

            responsesToReturn = responses;

        }

 

        public T GetRequest<T>(int index) where T : Request

        {

            return (T)receivedRequests[index];

        }

 

        public Response[] GrabRequestsAndReturnGivenResponses(params Request[] requests)

        {

            receivedRequests = requests;

            return responsesToReturn;

        }

    }

Now we can write our test like this:

        [Test]

        public void RetrievesCategoriesAndSuppliersOnLoadIfNotPostBack()

        {

            view.Stub(v => v.IsPostBack).Return(false);

 

            var categoriesToReturn = new ProductCategoryDTO[0];

            var suppliersToReturn = new SupplierDTO[0];

            var spy = new ServiceRequestResponseSpy();

            spy.SetResponsesToReturn(new GetProductCategoriesResponse(categoriesToReturn),

                new GetSuppliersResponse(suppliersToReturn));

 

            service.Stub(s => s.Process(null))

                .IgnoreArguments()

                .Do(new Func<Request[], Response[]>(spy.GrabRequestsAndReturnGivenResponses));

 

            view.Expect(v => v.ProductCategories = categoriesToReturn);

            view.Expect(v => v.Suppliers = suppliersToReturn);

            view.Expect(v => v.DataBind());

 

            CreateController();

            controller.Load();

 

            view.VerifyAllExpectations();

            Assert.IsNotNull(spy.GetRequest<GetProductCategoriesRequest>(0));

            Assert.IsNotNull(spy.GetRequest<GetSuppliersRequest>(1));

        }

The important part of the code is this one:

            service.Stub(s => s.Process(null))

                .IgnoreArguments()

                .Do(new Func<Request[], Response[]>(spy.GrabRequestsAndReturnGivenResponses));

This basically tells Rhino Mocks to execute the spy’s GrabRequestsAndReturnGivenRepsonses method whenever the mocked service instance’s Process method is called, no matter what arguments are passed to it.

So we basically set up the mocked service to delegate the call to Process to the GrabRequestsAndReturnGivenRepsonses method, and then we set expectations on how the returned data should be passed to the view. After that we execute the Controller’s Load method, and we verify that the view’s expectations were met, and we test that the spy captured Request instances of the expected types.

Posted in Test Driven Development | No Comments »

The Service Call Batcher

Posted by Davy Brion on 28th June 2008

Picking up where we left off with the WCF batching… We had the following code client-side to execute a few service methods with one request:

            var results = service.Process(new GetProductCategoriesRequest(), new GetProductOverviewsRequest());

            View.ProductCategories = ((GetProductCategoriesResponse)results[0]).ProductCategories;

            View.Products = ((GetProductOverviewsResponse)results[1]).Products;

Similar to my query batcher, i wrote this simple WCFCallBatcher class:

    public class ServiceCallBatcher

    {

        private readonly IService service;

        private readonly Dictionary<string, int> responsePositions = new Dictionary<string, int>();

        private readonly List<Request> requests = new List<Request>();

        private Response[] responses;

 

        public ServiceCallBatcher(IService service)

        {

            this.service = service;

        }

 

        public void Add(string key, Request request)

        {

            requests.Add(request);

            responsePositions.Add(key, requests.Count - 1);

        }

 

        public T Get<T>(string key) where T : Response

        {

            if (responses == null) ExecuteRequests();

            return (T)responses[responsePositions[key]];

        }

 

        private void ExecuteRequests()

        {

            responses = service.Process(requests.ToArray());

        }

    }

and now we can rewrite the client code like this:

            var batcher = new ServiceCallBatcher(service);

            batcher.Add("categories", new GetProductCategoriesRequest());

            batcher.Add("products", new GetProductOverviewsRequest());

            View.ProductCategories = batcher.Get<GetProductCategoriesResponse>("categories").ProductCategories;

            View.Products = batcher.Get<GetProductOverviewsResponse>("products").Products;

Yes, we’ve got more lines of code now, but i like this block of a code a lot more than the earlier one. This is much more readable.

Posted in Performance, WCF | No Comments »

Batching WCF calls

Posted by Davy Brion on 26th June 2008

Update: i also have a complete series of posts on this which you may find interesting.

What happens if you have the following code client-side:

            View.ProductCategories = service.GetProductCategories();

            View.Products = service.GetProductOverviews();

The service variable is a reference to a WCF service proxy. This means that this causes 2 remote service calls. I really hate unnecessary/excessive roundtrips, so i would rather retrieve this data from the service in one roundtrip. Unfortunately, this is not something that is supported by WCF out of the box, so if you want to do this you have to do it yourself. There are a couple of options, so i started browsing the web. Once again, it’s Ayende to the rescue. The approach he suggests is a bit cumbersome compared to how you’d normally design your services, but it does offer a great deal of flexibility and it allows you to nicely batch your requests. So i figured i should try implementing this approach.

The idea is to provide a service method that looks like this:

        Response[] Process(params Request[] requests);

Each Request corresponds to what used to be a normal service method, and this method returns a Response for each Request. In Ayende’s post, he suggest offering this as the only service method. I won’t go that far though. I’ll offer this method, and each ‘normal’ method allthough those methods will also use Request types as input variables and Response types as return values. The idea is to be able to use each service method individually, or to have multiple service methods executed in one roundtrip through the Process method.

We need a way to define Requests and Responses, so we create the following classes:

    [Serializable]

    public abstract class Response { }

 

    [Serializable]

    public abstract class Request {}

For now, these are empty but i’m pretty sure i’ll add a few members to these later on. Then we define specific Request and Response types for the functionality our service needs to offer:

    [Serializable]

    public class GetProductCategoriesRequest : Request {}

 

    [Serializable]

    public class GetProductOverviewsRequest : Request

    {

        public int? ProductCategoryId { get; set; }

    }

    [Serializable]

    public class GetProductCategoriesResponse : Response

    {

        public ProductCategoryDTO[] ProductCategories { get; private set; }

 

        public GetProductCategoriesResponse(ProductCategoryDTO[] productCategories)

        {

            ProductCategories = productCategories;

        }

    }

 

    [Serializable]

    public class GetProductOverviewsResponse : Response

    {

        public ProductOverviewDTO[] Products { get; private set; }

 

        public GetProductOverviewsResponse(ProductOverviewDTO[] products)

        {

            Products = products;

        }

    }

We then create a base interface that each service will implement (along with more specific service interfaces):

    [ServiceContract]

    public interface IService : IDisposable

    {

        /// <summary>

        /// Processes each request within the same service call

        /// </summary>

        /// <param name="requests">each request</param>

        /// <returns>the corresponding responses</returns>

        [OperationContract]

        [ServiceKnownType("GetKnownTypes", typeof(KnownTypeProvider))]

        Response[] Process(params Request[] requests);

    }

The KnownTypeProvider is responsible for providing a list of types that should be treated as KnownTypes during serialization/deserialization. We could also list each type that derives from Response/Request but that will become a long list, so i went for this approach. The code of the KnownTypeProvider class looks like this:

    public static class KnownTypeProvider

    {

        private static readonly List<Type> knownTypes;

 

        static KnownTypeProvider()

        {

            knownTypes = BuildListOfKnownTypes();

        }

 

        private static List<Type> BuildListOfKnownTypes()

        {

            Assembly containingAssembly = typeof(KnownTypeProvider).Assembly;

 

            return containingAssembly.GetTypes()

                .Select(t => t)

                .Where(t => t.IsSubclassOf(typeof(Request)) || t.IsSubclassOf(typeof(Response)))

                .ToList();

        }

 

        public static IEnumerable<Type> GetKnownTypes(ICustomAttributeProvider provider)

        {

            return knownTypes;

        }

    }

Our real service now looks like this:

    [ServiceContract]

    public interface IProductsService : IService

    {

        [OperationContract]

        GetProductCategoriesResponse GetProductCategories(GetProductCategoriesRequest request);

 

        [OperationContract]

        GetProductOverviewsResponse GetProductOverviews(GetProductOverviewsRequest request);

    }

To handle these requests, i came up with the following RequestHandler type:

    public abstract class RequestHandler : Disposable, IRequestHandler

    {

        public IUnitOfWork UnitOfWork { get; private set; }

 

        protected RequestHandler(IUnitOfWork unitOfWork)

        {

            UnitOfWork = unitOfWork;

        }

 

        protected override void DisposeObjects()

        {

            if (UnitOfWork != null) UnitOfWork.Dispose();

        }

 

        protected override void ClearReferences()

        {

            UnitOfWork = null;

        }

 

        public abstract Response Handle(Request request);

    }

And the implementation of one of these concrete RequestHandlers looks like this:

    public class GetProductOverviewsHandler : RequestHandler

    {

        public ProductRepository ProductRepository { get; private set; }

 

        public GetProductOverviewsHandler(IUnitOfWork unitOfWork, ProductRepository productRepository)

            : base(unitOfWork)

        {

            ProductRepository = productRepository;

        }

 

        public override Response Handle(Request request)

        {

            var typedRequest = (GetProductOverviewsRequest)request;

 

            if (typedRequest.ProductCategoryId.HasValue)

            {

                var fetchStrategy = new FetchStrategy

                    { Association = "Supplier", FetchMode = FetchMode.Join };

                var productOverviews = ProductRepository.FindAllByCategory(

                    typedRequest.ProductCategoryId.Value, fetchStrategy);

                return new GetProductOverviewsResponse(productOverviews.ToOverviewDTOs());

            }

            else

            {

                // TODO: use query batcher to fetch all suppliers and products in one trip

                var productOverviews = ProductRepository.FindAll();

                return new GetProductOverviewsResponse(productOverviews.ToOverviewDTOs());

            }

        }

    }

Now we have to make sure we can actually use this RequestHandlers in a generic manner. I came up with the following base class for my services:

    public abstract class Service : IService

    {

        /// <summary>

        /// server-side service implementation should be stateless, but the IService

        /// interface defines the Dispose method, so we just provide an empty one here

        /// </summary>

        public void Dispose() {}

 

        public Response[] Process(params Request[] requests)

        {

            if (requests == null)

            {

                return null;

            }

 

            var responses = new List<Response>(requests.Length);

 

            foreach (var request in requests)

            {

                Type handlerType = GetHandlerTypeFor(request);

 

                if (handlerType == null)

                {

                    responses.Add(new UnsupportedRequestResponse());

                    continue;

                }

 

                using (var handler = GetHandlerInstance(handlerType))

                {

                    responses.Add(handler.Handle(request));

                }

            }

 

            return responses.ToArray();

        }

 

        public virtual IRequestHandler GetHandlerInstance(Type handlerType)

        {

            return (IRequestHandler)Container.Resolve(handlerType);

        }

 

        public abstract Type GetHandlerTypeFor(Request request);

    }

And the real service then looks like this:

    public class ProductsService : Service, IProductsService

    {

        private static readonly object monitor = new object();

 

        private static readonly Dictionary<Type, Type> requestTypesToRequestHandlerTypes

            = new Dictionary<Type, Type>

            {

                { typeof(GetProductCategoriesRequest), typeof(GetProductCategoriesHandler) },

                { typeof(GetProductOverviewsRequest), typeof(GetProductOverviewsHandler) }

            };

 

        public GetProductCategoriesResponse GetProductCategories(GetProductCategoriesRequest request)

        {

            return (GetProductCategoriesResponse)Process(request)[0];

        }

 

        public GetProductOverviewsResponse GetProductOverviews(GetProductOverviewsRequest request)

        {

            return (GetProductOverviewsResponse)Process(request)[0];

        }

 

        public override Type GetHandlerTypeFor(Request request)

        {

            Type requestType = request.GetType();

 

            lock (monitor)

            {

                if (!requestTypesToRequestHandlerTypes.ContainsKey(requestType))

                {

                    return null;

                }

 

                return requestTypesToRequestHandlerTypes[requestType];

            }

        }

    }

At this point, using a dictionary to map the Request types to the RequestHandler types seems silly, but this is just a small part of the service, more methods will be added so more Request and RequestHandlers will be handled, and in that case i prefer to use a dictionary lookup over a big if statement :)

So now i can do this client-side:

            var results = service.Process(new GetProductCategoriesRequest(), new GetProductOverviewsRequest());

            View.ProductCategories = ((GetProductCategoriesResponse)results[0]).ProductCategories;

            View.Products = ((GetProductOverviewsResponse)results[1]).Products;

And it only uses one remote call which will be much better for performance. At this point, the syntax is not so nice, but i’ll probably provide something similar to my query batcher class so that should improve readability.

So that’s pretty much it… it takes some effort to set this up, and there’s still more to be done (dealing with exceptions in one of the requests for instance) but once you’ve got it set up, i kinda like it. Sure, implementing service methods takes more code than it used to because of the extra classes involved, but they do offer a few very important benefits. You may have noticed that the RequestHandler types receive all of their dependencies through the constructor, and that they are created through the IoC container. This means i can very easily add/remove/change dependencies in my handlers without having to modify code in other places. It also means i can test my service implementations in a thorough manner while providing mocked dependencies instead of the real ones. Another big advantage is that i can add optional parameters to my Request types and thus, offer more functionality through my RequestHandlers without having to update the services and the proxies.

So all in all, while this approach requires to you write more code at first, i think it might actually end up giving you more flexibility for changes later on, while offering you much more control over certain performance characteristics.

Posted in Performance, WCF | 16 Comments »

Data Access With NHibernate

Posted by Davy Brion on 23rd June 2008

One thing that keeps amazing me is how many smart developers still feel the urge to write their own data access layer (DAL). They either do it all manually, or they generate parts of it, or they generate the whole thing. Whichever way you go, there are still various alternative paths you can choose from. Some people like to use stored procedures for everything, some people generate sql statements and provide a semi-OO API in front of it, some people still spend time constructing Command objects. I think i’ve seen most approaches by now, and my personal opinion is that they pretty much all suck.

These DAL’s usually have at least one big downside to them. Generated DAL’s are usually pretty good for productivity but most of the times they don’t really offer you that much control over how queries should be executed, whether or not statements should be batched, specify fetching-strategies, etc. Hand-written DAL’s are terrible for productivity but you can fine tune each action to an optimal implementation. These downsides are pretty big IMHO, and are often underestimated. Sometimes due to semi-religious stances and sometimes it’s just due to ignorance.

As you already know, i’m a fan of NHibernate. Well, consider me a fan of decent ORM’s in general. NHibernate just happens to be the one i know best and am very happy with. In this post i’d like to take a look at how you can easily offer the most common data access requirements without generating code, while still allowing high developer productivity. We’ll go over the implementation of a generic Repository class, and hopefully you’ll see how much NHibernate can improve your way of working.

We’re going to look at the implementation piece by piece, so lets just start with the declaration of the class so we can get that out of the way:

    public class Repository<T> : IRepository<T>

As you can see, this is just a generic class that takes a type parameter. The type parameter represents the type of the Entity you want this repository to handle. If you have an Entity base class or interface, you probably want to restrict the type of T to inherit from Entity or implement IEntity or whatever.

This class needs to be able to access the current NHibernate session, which i discussed yesterday:

        private readonly IActiveSessionManager activeSessionManager;

 

        public Repository(IActiveSessionManager activeSessionManager)

        {

            this.activeSessionManager = activeSessionManager;

        }

 

        protected ISession Session

        {

            get { return activeSessionManager.GetActiveSession(); }

        }

Right, now we can actually get to the interesting Data Access parts. Obviously, each DAL needs a way to retrieve a specific entity based on its Primary Key value:

        /// <summary>

        /// Retrieves the entity with the given id

        /// </summary>

        /// <param name="id"></param>

        /// <returns>the entity or null if it doesn't exist</returns>

        public T Get(object id)

        {

            return Session.Get<T>(id);

        }

Very straightforward stuff… this simply uses the current NHibernate session to retrieve an entity of the requested type, with the given primary key value.

Another thing we need is a way to create or update entity instances:

        /// <summary>

        /// Saves or updates the given entity

        /// </summary>

        /// <param name="entity"></param>

        public void SaveOrUpdate(T entity)

        {

            Session.SaveOrUpdate(entity);

        }

This method simply uses the session’s SaveOrUpdate method, which will either perform an Insert (in case of a new entity) or an Update (in case of an existing entity). NHibernate will perform a check for a new instance depending on how you’ve configured this in your mapping files.

So far this has been really straightforward, so it’s time to get to a more interesting part. Retrieving entities based on criteria (basically a query):

        /// <summary>

        /// Returns each entity that matches the given criteria

        /// </summary>

        /// <param name="criteria"></param>

        /// <returns></returns>

        public IEnumerable<T> FindAll(DetachedCriteria criteria)

        {

            return criteria.GetExecutableCriteria(Session).List<T>();

        }

This probably needs a bit of explaining. The parameter is an instance of the DetachedCriteria type. A DetachedCriteria is just like a Criteria instance, except that it hasn’t been associated with a session yet. So you can create the DetachedCriteria without being connected to a session. This basically represents a query. I’ll show a concrete example of this later on. The thing to remember is that this is the only code you really need to perform any query you want. You just have to write the query using the Criteria API. This does have a bit of a learning curve, but most people pick it up pretty fast.

You usually also want a way to determine the ordering of the result of the query:

        /// <summary>

        /// Returns each entity that maches the given criteria, and orders the results

        /// according to the given Orders

        /// </summary>

        /// <param name="criteria"></param>

        /// <param name="orders"></param>

        /// <returns></returns>

        public IEnumerable<T> FindAll(DetachedCriteria criteria, params Order[] orders)

        {

            if (orders != null)

            {

                foreach (var order in orders)

                {

                    criteria.AddOrder(order);

                }

            }

 

            return FindAll(criteria);

        }

Each order you provide is applied to the query and then the query is executed. Again, pretty simple stuff right?

You want to know how you can get paging working? Here it is:

        /// <summary>

        /// Returns each entity that matches the given criteria, with support for paging,

        /// and orders the results according to the given Orders

        /// </summary>

        /// <param name="criteria"></param>

        /// <param name="firstResult"></param>

        /// <param name="numberOfResults"></param>

        /// <param name="orders"></param>

        /// <returns></returns>

        public IEnumerable<T> FindAll(DetachedCriteria criteria, int firstResult, int numberOfResults, params Order[] orders)

        {

            criteria.SetFirstResult(firstResult).SetMaxResults(numberOfResults);

            return FindAll(criteria, orders);

        }

Keep in mind that the executed query only retrieves the results within the paging range. It does not retrieve everything to perform the paging client-side, this happens in the db where it’s supposed to happen.

What if you have a query that should only return one instance? That’s easy to do as well:

        /// <summary>

        /// Returns the one entity that matches the given criteria. Throws an exception if

        /// more than one entity matches the criteria

        /// </summary>

        /// <param name="criteria"></param>

        /// <returns></returns>

        public T FindOne(DetachedCriteria criteria)

        {

            return criteria.GetExecutableCriteria(Session).UniqueResult<T>();

        }

What if you have a query and you just want the very first result instead of the entire resultset? Again, pretty easy to do:

        /// <summary>

        /// Returns the first entity to match the given criteria

        /// </summary>

        /// <param name="criteria"></param>

        /// <returns></returns>

        public T FindFirst(DetachedCriteria criteria)

        {

            var results = criteria.SetFirstResult(0).SetMaxResults(1)

                .GetExecutableCriteria(Session).List<T>();

 

            if (results.Count > 0)

            {

                return results[0];

            }

 

            return default(T);

        }

Again, NHibernate will issue a smart sql statement… that is, it only retrieves the first result instead of the entire resultset.

Obviously, this is also useful if you can define the order of the results to pick the first result:

        /// <summary>

        /// Returns the first entity to match the given criteria, ordered by the given order

        /// </summary>

        /// <param name="criteria"></param>

        /// <param name="order"></param>

        /// <returns></returns>

        public T FindFirst(DetachedCriteria criteria, Order order)

        {

            return FindFirst(criteria.AddOrder(order));

        }

How often have you seen developers execute a query in code, only to use the count of the records without actually needing the returned records? We no longer need to beat the shit out of these developers:

        /// <summary>

        /// Returns the total number of entities that match the given criteria

        /// </summary>

        /// <param name="criteria"></param>

        /// <returns></returns>

        public long Count(DetachedCriteria criteria)

        {

            return Convert.ToInt64(criteria.GetExecutableCriteria(Session)

                .SetProjection(Projections.RowCountInt64()).UniqueResult());

        }

In this case, NHibernates issues a simple select count… query based on the criteria you’ve provided. Nice huh?

We might as well throw in this one as well:

        /// <summary>

        /// Returns true if at least one entity exists that matches the given criteria

        /// </summary>

        /// <param name="criteria"></param>

        /// <returns></returns>

        public bool Exists(DetachedCriteria criteria)

        {

            return Count(criteria) > 0;

        }

And last, but certainly not least, you’ll also want a way to delete entities from the database. How about this:

        /// <summary>

        /// Deletes the given entity

        /// </summary>

        /// <param name="entity"></param>

        public void Delete(T entity)

        {

            Session.Delete(entity);

        }

 

        /// <summary>

        /// Deletes every entity that matches the given criteria

        /// </summary>

        /// <param name="criteria"></param>

        public void Delete(DetachedCriteria criteria)

        {

            // a simple DELETE FROM ... WHERE ... would be much better, but i haven't found

            // a way to do this yet with Criteria. So now it does two roundtrips... one for

            // the query, and one with all the batched delete statements (that is, if you've

            // enabled CUD statement batching

            foreach (T entity in FindAll(criteria))

            {

                Delete(entity);

            }

        }

The first Delete method simply deletes the given entity. The second method probably needs to be explained a bit more. This method receives a query, and it deletes all the items that the query returns. As you can see from the comment, it would be better if it would perform a DELETE FROM … WHERE instead of fetching the results of the query but i didn’t find a way to do that with the criteria API. In a more advanced scenario, this might actually be better than simply issuing a large DELETE statement because you could offer a Delete method which also receives a block of code to execute before or after each delete is executed. Which opens the door to a lot of interesting opportunities.

And that’s it basically… I really haven’t shown you that much code right? And what does this code offer us? We get create/update/delete functionality, and we also have some nice options for querying our data. And since the Criteria API of NHibernate allows you to create powerful and complex queries, you can use it to create your queries and then you simply pass these criteria to the repository to fetch the data. Just so we’re clear on this, you’re not just limited to specifying which data you want to retrieve, but you can also tell NHibernate how you want to retrieve it because you can define fetching strategies for each association. This is a tremendously powerful feature which offers you a lot of flexibility in choosing the most optimal data fetching approaches, without being limited to what your DAL supports or having to spend a lot of code on it.

Let’s wrap up this post with a small example of how you could use this repository class to execute a query you wrote yourself:

            var criteria = DetachedCriteria.For<ProductCategory>()

                .Add(Expression.Like("Name", "Test%"));

 

            var categories = repository.FindAll(criteria);

As you can see, this is really easy. The only effort basically lies within building the query, so as you’re queries become more complex, this effort obviously increases.

If you combine this repository approach with query batching, you end up with an easy-to-use data layer which offers you all the flexibility you could want, while still allowing you to implement specifically tuned solutions to achieve excellent performance. Also, keep in mind that this is only a very basic repository implementation. The Rhino Commons repository implementation offers even more functionality with a couple of extra options to boost performance. I really can’t think of a single good reason not to use this approach anymore.

Posted in NHibernate | 18 Comments »

No confidence in Entity Framework?

Posted by Davy Brion on 23rd June 2008

A group of developers have expressed their lack of confidence in Microsoft’s upcoming Entity Framework. If you agree with their statements, you can sign the Vote Of No Confidence document here. The document lists some of the biggest problems of the Entity Framework (according to a certain group of developers) so whether you plan to sign it or not, it’s definitely an interesting read that’s worth checking out.

Posted in Entity Framework | 1 Comment »

Managing your NHibernate Sessions

Posted by Davy Brion on 22nd June 2008

Note: you can find a more recent post on this concept here.

I was working on my northwind sample application (which i’ll post about as soon as i get something that’s worth showing) and i was struggling to find a clean way to manage my NHibernate sessions. I wanted a way to create a session once you enter the service layer, and that session should be available transparently to the repository implementations without actually having to constantly pass it around. But i didn’t just want to store it somewhere and have all the classes that needed the current session get it directly from that place. Also, i didn’t want the current session to be available to everyone. Another important requirement was to have maximum flexibility for writing tests. I could just use Rhino-Commons’ implementations of the UnitOfWork and Repository patterns and be done with it, but where’s the fun in that? And since this application is mostly a learning experience i figured i should try to write this myself. After a bit of searching and experimenting, i finally came up with a way that i’m happy with (for now anyways). Let’s have a look.

To illustrate what i want, take a look at this made-up method in my service layer:

        public ProductCategoryDTO[] GetAllProductCategories()

        {

            using (ISession session = GetNewSession())

            {

                var repository = GetProductCategoryRepository();

                var categories = repository.GetAllProductCategories();

                return categories.ToDTOs();

            }

        }

This is just some example code, it doesn’t even work. But it’s kinda what i want… The thing is, i don’t want to deal directly with the ISession type in the service layer, but i do want the ProductCategoryRepository to use the ISession that is created within the context of this method call.

NHibernate’s ISession type is an implementation of the Unit Of Work pattern. I don’t want to use the ISession directly in my service layer because i want something that is a bit more high-level. Basically just something that would allow me to work within an NHibernate session, and provide me with the ability to flush changes to the database whenever i want to. And obviously, i want it to allow me to create a transaction as well. So i came up with the following unit of work interface:

    public interface IUnitOfWork : IDisposable

    {

        /// <summary>

        /// Flushes any changes that haven't been executed yet

        /// </summary>

        void Flush();

 

        /// <summary>

        /// Creates an ITransaction instance with the ReadCommitted isolation level

        /// </summary>

        /// <returns></returns>

        ITransaction CreateTransaction();

 

        /// <summary>

        /// Creates an ITransaction instance with the given isolation level

        /// </summary>

        /// <param name="isolationLevel"></param>

        /// <returns></returns>

        ITransaction CreateTransaction(IsolationLevel isolationLevel);

    }

This interface pretty much offers me anything i’m concerned with in my service layer. The real implementation would do a bit more though… It has to create an NHibernate session and make it available to the repositories in a clean way. Here’s the thing though… the repositories have a completely different lifetime than the NHibernate sessions. The NHibernate session has to be created when we enter a service method, and it has to be valid within the scope and context of the execution of that service method. The repositories however stay alive for the lifetime of the application so they can’t just hold a reference to an NHibernate session. Every time you call a method of a repository, it has to find out which session it should use, which is the session that is being used in the context of the current service method call. Now, we could always pass around the session but that really doesn’t look good and is cumbersome. So i basically need something that gives me access to the active nhibernate session:

    public interface IActiveSessionManager

    {

        /// <summary>

        /// Returns the active ISession for the current thread. Throws exception if there's

        /// no active ISession instance

        /// </summary>

        /// <returns></returns>

        ISession GetActiveSession();

 

        /// <summary>

        /// Sets the active ISession for the current thread. Throws exception if there's

        /// already an active ISession instance

        /// </summary>

        /// <param name="session"></param>

        void SetActiveSession(ISession session);

 

        /// <summary>

        /// Clears the active ISession for the current thread.

        /// </summary>

        void ClearActiveSession();

    }

That gives me the ability to retrieve and set the active session on a per-thread basis. After all, a service method call will be handled by one thread, so setting the active session for that current thread is a convenient way to store the session.

Now i still need something that takes care of creating the NHibernate sessions:

    public interface ISessionFactory

    {

        /// <summary>

        /// Creates a new ISession instance

        /// </summary>

        /// <returns></returns>

        ISession Create();

    }

Ok, so what do we have so far? An interface to define the functionality that a UnitOfWork should offer at the service level. An interface to store/retrieve the active session on the current thread, and finally, an interface to actually create the NHibernate session. Let’s start looking at the real implementations. Here’s the code to the SessionFactory class:

    public class SessionFactory : ISessionFactory

    {

        private readonly NHibernate.ISessionFactory sessionFactory;

 

        public SessionFactory()

        {

            Configuration configuration = new Configuration()

                .Configure()

                .AddAssembly("Northwind");

 

            sessionFactory = configuration.BuildSessionFactory();

        }

 

        public ISession Create()

        {

            return sessionFactory.OpenSession();

        }

    }

Pretty straightforward… it initializes NHibernate and gives us the ability to ask for new sessions. So how are we going to make these sessions available to our repositories? Through the ActiveSessionManager class of course:

    public class ActiveSessionManager : IActiveSessionManager

    {

        [ThreadStatic]

        private static ISession current;

 

        public ISession GetActiveSession()

        {

            if (current == null)

            {

                throw new InvalidOperationException("There is no active ISession instance for this thread");

            }

 

            return current;

        }

 

        public void SetActiveSession(ISession session)

        {

            if (current != null)

            {

                throw new InvalidOperationException("There is already an active ISession instance for this thread");

            }

 

            current = session;

        }

 

        public void ClearActiveSession()

        {

            current = null;

        }

    }

It basically just stores the session in a ThreadStatic field, which means that each thread will have a different static reference for this field. So if we set the active session through the SetActiveSession method in thread X, and thread Y also sets an active session, the GetActiveSession method will return the correct session instances for each thread.

So now we have everything we need to create our UnitOfWork class:

    public class UnitOfWork : Disposable, IUnitOfWork

    {

        private readonly IActiveSessionManager activeSessionManager;

        private readonly ISession session;

 

        public UnitOfWork(ISessionFactory sessionFactory, IActiveSessionManager activeSessionManager)

        {

            this.activeSessionManager = activeSessionManager;

            session = sessionFactory.Create();

            activeSessionManager.SetActiveSession(session);

        }

 

        protected override void DisposeObjects()

        {

            if (session != null)

            {

                session.Close();

                session.Dispose();

            }

        }

 

        protected override void ClearReferences()

        {

            activeSessionManager.ClearActiveSession();

        }

 

        public void Flush()

        {

            session.Flush();

        }

 

        public ITransaction CreateTransaction()

        {

            return CreateTransaction(IsolationLevel.ReadCommitted);

        }

 

        public ITransaction CreateTransaction(IsolationLevel isolationLevel)

        {

            return session.BeginTransaction(isolationLevel);

        }

    }

When the UnitOfWork is created, it receives an ISessionFactory instance, and an IActiveSessionManager instance. It then creates a new session through the ISessionFactory and uses the IActiveSessionManager to make sure that session is the active session for the current thread. When the UnitOfWork is disposed, it closes and cleans up the session and it also uses the IActiveSessionManager to clear the active session for the current thread. Oh and it obviously also provides implementations for what it is we actually need in our service layer: flushing the changes whenever we want and creating transactions.

The ISessionFactory and IActiveSessionManager instances should stay alive as long as the application is alive. But as you could see in the implementations of those types, we didn’t write any code to deal with their lifetimes. I’m actually relying on my IoC container for that. In the class where my container is set up, you’ll find the following code:

        private static void RegisterUnitOfWorkComponents()

        {

            Register(Component.For<ISessionFactory>()

                .ImplementedBy<SessionFactory>().LifeStyle.Singleton);

            Register(Component.For<IActiveSessionManager>()

                .ImplementedBy<ActiveSessionManager>().LifeStyle.Singleton);

            Register(Component.For<IUnitOfWork>()

                .ImplementedBy<UnitOfWork>().LifeStyle.Transient);

        }

ISessionFactory and IActiveSessionManager are registered as singleton instances, so whenever these types are requested, the same instances will be returned. The IUnitOfWork type is registered with a transient lifetime, so whenever it is requested, the container will create a new UnitOfWork class and pass the ISessionFactory and IActiveSessionManager instances to the constructor.

Right, we’ve taken care of creating the session, associating it with the current thread and making it available in a nice and clean way. Now we actually have to make sure our repositories can use it. In the base repository implementation, you can find the following code:

        private readonly IActiveSessionManager activeSessionManager;

 

        public Repository(IActiveSessionManager activeSessionManager)

        {

            this.activeSessionManager = activeSessionManager;

        }

 

        protected ISession Session

        {

            get { return activeSessionManager.GetActiveSession(); }

        }

Whenever a repository needs a session, it just needs to use the protected Session property and it will get the session that is associated with the current thread.

So now we can rewrite our made-up service method from earlier to the following:

        public ProductCategoryDTO[] GetAllProductCategories()

        {

            using (Container.Resolve<IUnitOfWork>())

            {

                var repository = Container.Resolve<ProductCategoryRepository>();

                var categories = repository.GetAllProductCategories();

                return categories.ToDTOs();

            }

        }

We know that the NHibernate session will be created, and more importantly, that it is not just globally accessible to everyone. It can only be accessed when you have a reference to an IActiveSessionManager instance. We also don’t need to pass around the session all the time so our code is a bit more concise, showing only the intent of what we’re trying to do without distracting us with details that are not relevant to that intent. We also have a lot of flexibility to write tests… i can write tests for my repositories without having to create a UnitOfWork… i can simply pass a fake IActiveSessionManager to my repositories when i’m testing them and have it return the session that i’m using for my test. All in all, this approach offers me with a lot of advantages, without ugly disadvantages. Well, at this moment i don’t really see any disadvantages so if you do see some, please let me know :)

Posted in Dependency Injection, Inversion Of Control, NHibernate, Patterns | 35 Comments »

Disposing of the IDisposable implementation

Posted by Davy Brion on 17th June 2008

Everytime i need to implement the IDisposable interface i have to lookup the recommended way of doing so. That in itself is a bad sign, so i figured i might as well get rid of this by putting the implementation in a reusable base class, based on the officially recommended way:

    public abstract class Disposable : IDisposable

    {

        private bool disposed;

 

        public void Dispose()

        {

            Dispose(true);

            GC.SuppressFinalize(this);

        }

 

        protected void Dispose(bool disposing)

        {

            if (!disposed)

            {

                if (disposing)

                {

                    DisposeManagedResources();

                }

 

                DisposeUnmanagedResources();

                disposed = true;

            }

        }

 

        protected void ThrowExceptionIfDisposed()

        {

            if (disposed)

            {

                throw new ObjectDisposedException(GetType().FullName);

            }

        }

 

        protected abstract void DisposeManagedResources();

        protected virtual void DisposeUnmanagedResources() {}

    }

So now i can simply inherit from Disposable, and i just need to implement the two abstract methods. Here’s a made up example to illustrate this:

    public class MyExpensiveResource : Disposable

    {

        private FileStream fileStream;

        private MemoryStream memoryStream;

 

        public MyExpensiveResource(string path)

        {

            fileStream = new FileStream(path, FileMode.Open);

            memoryStream = new MemoryStream();

        }

 

        public void DoSomething()

        {

            ThrowExceptionIfDisposed();

 

            // ... something

        }

 

        protected override void DisposeManagedResources()

        {

            if (fileStream != null) fileStream.Dispose();

            if (memoryStream != null) memoryStream.Dispose();

        }

    }

Obviously, you can’t use the Disposable base class if you’re already inheriting from another base class so in that case you’d still have to implement the IDisposable interface.

Update: Here’s a thread-safe version of this idea.

Posted in Memory Management, Patterns, Performance | 18 Comments »

Are women better developers than men?

Posted by Davy Brion on 16th June 2008

A coworker just pointed out the following article. This is something i’m pretty interested in. Let’s just get to the two most important quotes from the article:


“Women are more touchy-feely and considerate of those who will use the code later, she says. They’ll intersperse their code with helpful comments and directions, explaining why they wrote the lines the way they did and exactly how they did it. The code becomes a type of “roadmap” for others who might want to alter it or add to it later”

and…


“Men, on the other hand, have no such pretenses. They try to show how clever they are by writing very cryptic code. They try to obfuscate things in the code and don’t leave clear directions for people using it later.”

I wouldn’t be surprised if this was actually true, to be honest. Allthough there are always exceptions of course (no pun intended). But how many of us have been confronted with cryptic code with hardly any comments to clarify the code? How likely was it that that code was written by a male developer? Since there aren’t that many female developers, i’d say the odds are pretty high it was written by a guy. Now, i don’t believe men write crappy code on purpose though. But considering how common crappy code really is, while keeping in mind that most developers are male, and that women in general think differently than men, is it really that hard to believe that women might actually be better at this?

The subject of female programmers is one that is occasionaly brought up among male developers. And you’ll very often hear them say “i’ve never known one who was good at coding”. I’ve even heard some developers claim that it is impossible for women to be good at coding. Most of them simply don’t believe in it. Me, honestly, i don’t know… i know a lot of male programmers, but i really don’t know a lot of them that are actually good. I would guess that only 10% of male developers is actually good. I obviously have no studies or facts or figures to back that up, it’s just a guesstimate based on the total numbers of coders i’ve met so far. But if you think about that, it’s not at all unlikely that the percentage of good programmers could actually be higher among women. I mean come on, 10%?! That shouldn’t be hard to beat. If only there were more female programmers we might actually be able to get some real data on all of this. But until then, put me in the ‘believer’-camp :)

Posted in Software Development | 14 Comments »

Automanual Dependency Injection?

Posted by Davy Brion on 16th June 2008

I apologize in advance for using the term ‘automanual’ but if you’ve been reading this blog for a while you already know i completely suck at coming up with good names. So bear with me, and you’ll probably understand what i mean as you work your way through this post. It really is pretty cool… i promise :p

I’m playing around with some supervising controller MVP stuff using regular ASP.NET webforms. Yes i know, i should be using ASP.NET MVC but i have a strict “i don’t touch it before there’s a final release”-policy when it comes to Microsoft products (as opposed to my “lemme just build the latest version of the trunk”-policy for various open source products). Anyways, what i’m trying to do is pretty simple. I have a view (ProductList.aspx) which uses a supervising controller (ProductListController). The view (the aspx page) will notify the controller when it needs to do something through events. The controller will then do whatever it needs to do and it will send the data back to the view through properties of the view. If that’s not clear to you, read the post i linked to for a much better and detailed explanation.

Since ASP.NET automatically instantiates your aspx page, the easiest thing to do is to let the view create the controller and then pass itself as a parameter to the controller. But the controller also has other dependencies, such as a service which exposes the business logic that we need for this screen. So i have two options: i either create the controller myself and provide all the dependencies, or i use an inversion of control container to create the controller and to wire up all the dependencies. I don’t want to have to modify my view code whenever i add/remove a dependency of the controller, so i go with the inversion of control container.

Suppose the constructor of the controller looks like this:

        public ProductListController(IProductList view, IProductsService productsService)

        {

            this.view = view;

            this.productsService = productsService;

        }

Here’s where it gets tricky… since the view (which implements the IProductList interface) is asking the container to create a ProductListController, how can the container pass the correct IProductList dependency to the controller? We can’t use the regular dependency look-up mechanisms because our controller actually needs the current view instance, but that view instance is asking the container to create the controller! By default, the container has no way whatsoever to resolve the IProductList dependency to the current view instance. So basically, what we want to do in this case is to manually provide the view dependency, but still have the container automatically provide the IProductsService dependency (hence the term ‘automanual’ which you have to admit is starting to sound pretty good at this point, right?). And of course, we want all of this to work automagically.

It turns out that Castle’s Windsor actually does have some slick tricks to make this work. Here’s how we can create the controller through the container from the view:

            IProductListController controller =

                Container.Resolve<IProductListController>(new { view = this });

Told you it was slick! It’s pretty easy actually… the parameter we pass to the Resolve method is an instance of an anonymous type with a view property. We set the view property to the current aspx instance (using the ‘this’ keyword obviously) and Windsor is smart enough to figure out that this value should be used to satisfy the view dependency instead of using it’s normal look-up mechanisms. The result is that our controller has a reference to the current view, and its IProductsService reference is resolved as the container would typically resolve dependencies. Pretty sweet.

Btw, i googled the term ‘automanual’ and sure enough, it already exists… but it’s not really used in the context of writing code so if this thing sticks, remember where you heard it first ;)

Posted in ASP.NET, Castle Windsor, Dependency Injection, Inversion Of Control | 3 Comments »

The Query Batcher

Posted by Davy Brion on 14th June 2008

One of my favorite NHibernate features must be the MultiCriteria/MultiQuery support. This basically allows you to execute a set of queries in one database call. In case you don’t know, an ICriteria instance is basically a programmatic query. If you use MultiCriteria, you can batch either ICriteria or DetachedCriteria instances. And if you use MultiQuery, you can batch regular HQL strings, IQuery instances (which are in fact also HQL queries) or references to named queries.

That gives you quite a few options to batch your queries. The only downside to using MultiCriteria and/or MultiQuery is that you have to retrieve the results with an index, based on the order in which you added the criteria or queries. Using index values in your code often reduces readability, so if i can i try to avoid using them.

So i wrote a little QueryBatcher class which allows me to retrieve the results based on a key value. The class supports MultiCriteria as well as MultiQuery so you can use it to batch all kinds of queries. I didn’t find a way to combine the batched criteria and the batched HQL queries in one database call, so if you mix criteria with hql queries, it will use one database call to execute all the criteria, and one database call to execute the hql queries.

And now i can write code like this:

        [Test]

        public void ReturnsProperResultsWhenUsingMultipleQueries()

        {

            queryBatcher.AddCriteria("Products", session.CreateCriteria(typeof(Product)));

            queryBatcher.AddCriteria("Suppliers", DetachedCriteria.For<Supplier>());

            queryBatcher.AddHqlQuery("ProductCategories", "from ProductCategory");

            queryBatcher.AddHqlQuery("SomeProducts",

                session.CreateQuery("from Product where Discontinued = :discontinued")

                        .SetBoolean("discontinued", false));

 

            Assert.That(queryBatcher.GetEnumerableResult<Product>("Products").Count() > 0);

            Assert.That(queryBatcher.GetEnumerableResult<Supplier>("Suppliers").Count() > 0);

            Assert.That(queryBatcher.GetEnumerableResult<ProductCategory>("ProductCategories").Count() > 0);

            Assert.That(queryBatcher.GetEnumerableResult<Product>("SomeProducts").Count() > 0);

        }

Obviously, this is a very simple example with extremely simple queries… but you can of course batch very complex queries with this as well. You can also add queries that return scalar values so you could for instance do do something like queryBatcher.GetSingleResult<long>(“TotalOutstandingAmount”) and it would return that scalar value.

This is the code of the QueryBatcher class:

    public class QueryBatcher : IQueryBatcher

    {

        private readonly Dictionary<string, int> criteriaResultPositions;

        private readonly List<ICriteria> criteriaList;

        private readonly List<IQuery> hqlQueryList;

        private readonly Dictionary<string, int> queryResultPositions;

        private readonly ISession session;

 

        private IList criteriaResults;

        private IList queryResults;

 

        public QueryBatcher(IActiveSessionManager activeSessionManager)

        {

            session = activeSessionManager.GetActiveSession();

            criteriaList = new List<ICriteria>();

            hqlQueryList = new List<IQuery>();

            criteriaResultPositions = new Dictionary<string, int>();

            queryResultPositions = new Dictionary<string, int>();

        }

 

        public void AddCriteria(string key, ICriteria criteria)

        {

            criteriaList.Add(criteria);

            criteriaResultPositions.Add(key, criteriaList.Count - 1);

        }

 

        public void AddCriteria(string key, DetachedCriteria detachedCriteria)

        {

            AddCriteria(key, detachedCriteria.GetExecutableCriteria(session));

        }

 

        public object GetResult(string key)

        {

            ExecuteQueriesIfNecessary();

            object result = GetResultFromList(key, criteriaResults, criteriaResultPositions);

            if (result != null) return result;

            result = GetResultFromList(key, queryResults, queryResultPositions);

            if (result != null) return result;

 

            return null;

        }

 

        public IEnumerable<T> GetEnumerableResult<T>(string key)

        {

            var list = GetResult<IList>(key);

            return list.Cast<T>();

        }

 

        public T GetSingleResult<T>(string key)

        {

            var result = GetResult<IList>(key);

            return (T)result[0];

        }

 

        public void AddHqlQuery(string key, IQuery query)

        {

            hqlQueryList.Add(query);

            queryResultPositions.Add(key, hqlQueryList.Count - 1);

        }

 

        public void AddHqlQuery(string key, string query)

        {

            AddHqlQuery(key, session.CreateQuery(query));

        }

 

        public void ExecuteQueriesIfNecessary()

        {

            ExecuteCriteriaIfNecessary();

            ExecuteHqlIfNecessary();

        }

 

        private void ExecuteCriteriaIfNecessary()

        {

            if (criteriaList.Count > 0 && criteriaResults == null)

            {

                if (criteriaList.Count == 1)

                {

                    criteriaResults = new ArrayList { criteriaList[0].List() };

                }

                else

                {

                    var multiCriteria = session.CreateMultiCriteria();

                    criteriaList.ForEach(c => multiCriteria.Add(c));

                    criteriaResults = multiCriteria.List();

                }

            }

        }

 

        private void ExecuteHqlIfNecessary()

        {

            if (hqlQueryList.Count > 0 && queryResults == null)

            {

                if (hqlQueryList.Count == 1)

                {

                    queryResults = new ArrayList { hqlQueryList[0].List() };

                }

                else

                {

                    var multiQuery = session.CreateMultiQuery();

                    hqlQueryList.ForEach(q => multiQuery.Add(q));

                    queryResults = multiQuery.List();

                }

            }

        }

 

        private T GetResult<T>(string key)

        {

            return (T)GetResult(key);

        }

 

        private static object GetResultFromList(string key, IList list, IDictionary<string, int> positions)

        {

            if (positions.ContainsKey(key)) return list[positions[key]];

            return null;

        }

    }

Posted in NHibernate, Performance | 3 Comments »