I'm new to IoC and I'm trying to get started using StructureMap, but it is throwing a NullReferenceException when I try to get an object instance. Here's my init code:
ObjectFactory.Initialize(x =>
{
x.ForRequestedType<IRepository<Customer>>().TheDefaultIsConcreteType<EFRepository<Customer>>();
x.ForRequestedType<ICustomerManager>().TheDefaultIsConcreteType<CustomerManager>();
});
The ICustomerManager uses ctor injection and receives an IRepository:
public class CustomerManager : ICustomerManager
{
IRepository<Customer> _repository;
public CustomerManager(IRepository<Customer> repository)
{
_repository = repository;
}
public Customer GetCustomerById(int id)
{
return _repository
.With(c => c.PhoneNumbers)
.FirstOrDefault<Customer>(c => c.Id == id);
}
public IEnumerable<Customer> GetCustomersByName(string lastName, string firstName, string middleName)
{
return _repository.Query(new CustomerMatchesName(lastName, firstName, middleName));
}
}
Then in my service layer code, this line throws the exception:
var manager = ObjectFactory.GetInstance<ICustomerManager>();
I really have no idea where to start debugging this, being so new to the concepts in general. Any ideas on what could be going wrong in such a simple scenario?