tyronegroves/SimpleCQRS — Issue #15: Autofac ServiceLocator
Author: mkobaly
Created: 2011-10-22 05:44:55+00:00
State: open

Sorry for doing this but I am new to SimpleCqrs & GitHub. I am trying to incorporate Autofac as one of the Service Locators but the below code seems to blow up when resolving say the LocalCommandBus using the SimpleCqrsDemo. Any ideas?

public class AutofacServiceLocator : IServiceLocator
{
public IContainer Container { get; private set; }

public AutofacServiceLocator()
{
var builder = new ContainerBuilder();

Container = builder.Build();
}

public AutofacServiceLocator(IContainer container)
{
if (container == null)
throw new ArgumentNullException("container", "The specified Autofac container cannot be null.");
Container = container;
}

#region IServiceLocator Members

public T Resolve() where T : class
{
return Container.Resolve();
}

public T Resolve(string key) where T : class
{
return Container.ResolveNamed(key);
}

public object Resolve(Type type)
{
return Container.Resolve(type);
}

public IList ResolveServices() where T : class
{
return Container.Resolve>().ToList();
}

public void Register(Type implType) where TInterface : class
{
var builder = new ContainerBuilder();
builder.RegisterType(implType).As();
builder.Update(Container);
}

public void Register() where TImplementation : class, TInterface
{
var builder = new ContainerBuilder();
builder.RegisterType().As();
builder.Update(Container);
}

public void Register(string key) where TImplementation : class, TInterface
{
var builder = new ContainerBuilder();
builder.RegisterType().As().Named(key);
builder.Update(Container);
}

public void Register(string key, Type type)
{
var builder = new ContainerBuilder();
builder.RegisterType(type).Named(key, type);
builder.Update(Container);
}

public void Register(Type serviceType, Type implType)
{
var builder = new ContainerBuilder();
builder.RegisterType(implType).As(serviceType);
builder.Update(Container);
}

public void Register(TInterface instance) where TInterface : class
{
var builder = new ContainerBuilder();
builder.RegisterInstance(instance).As();
builder.Update(Container);
}

public void Release(object instance)
{
throw new NotImplementedException();
}

public void Reset()
{
throw new NotImplementedException();
}

public TService Inject(TService instance) where TService : class
{
return Container.InjectProperties(instance);
}

public void TearDown(TService instance) where TService : class
{
throw new NotImplementedException();
}

public void Register(Func factoryMethod) where TInterface : class
{
var builder = new ContainerBuilder();
builder.Register(x => factoryMethod()).As();
builder.Update(Container);
}

#endregion

#region IDisposable Members

public void Dispose()
{
if (Container != null)
{
Container.Dispose();
Container = null;
}
}

#endregion
}