How do I use IServiceLocator within a repository QueryFactory?
I am using an example from NHibernate 3.0 Cookbook which includes the following QueryFactory class. I have never used a service locator before and was wondering how I should be configuring it for use.
Code:
public class QueryFactory : IQueryFactory
{
private readonly IServiceLocator _serviceLocator;
public QueryFactory(IServiceLocator serviceLocator)
{
_serviceLocator = serviceLocator;
}
public TQuery CreateQuery<TQuery>() where TQuery : IQuery
{
return _serviceLocator.GetInstance<TQuery>();
}
}
The QueryFactory is created in the repository constructor using
Code:
IServiceLocator serviceLocator = ServiceLocator.Current;
IQueryFactory queryFactory = new QueryFactory(serviceLocator);
however, ServiceLocator.Current throws a Null exception when I create the repository.
Can someone please point me in right direction.
Thanks,