1

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?

jdraper3
  • 116
  • 1
  • 8
  • Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Nasreddine Oct 04 '15 at 09:08

1 Answers1

3

You most likely are getting an exception that StructureMap can't build an object, which causes a cascading exception of a null reference that has eaten the real exception.

The best solution for debugging these scenarios is turning on catch all exceptions, Ctrl+Alt+E and mark to catch all thrown exceptions.

The next tool to goto is StructureMap provides a utility method ObjectFactory.WhatDoIHave();

In all of my projects I have in my initialization code in Application_Start (I only do asp.net) I have the following code block

#if DEBUG
    string path = Server.MapPath("~/myproj.WhatDoIHave.txt");
    string whatDoIHave = ObjectFactory.WhatDoIHave();
    File.WriteAllText(path, whatDoIHave);
#endif

This output has offered me help innumerable times. Learning to read this file will let you troubleshoot basically any registration problems you have because you'll be able to see exactly what you do, and don't have.

Most times with StructureMap you end up troubleshooting what you DON'T have. Which usually boils down to needing to register a complex type that StructureMap can't satisfy.

Chris Marisic
  • 30,638
  • 21
  • 158
  • 255
  • 1
    The problem ended up being with the concrete object itself rather than with StructureMap. Thanks for the tips - they helped me with the process of elimination. – jdraper3 Jun 09 '11 at 17:31
  • I've been there many times myself, and this method has in I believe every time but 1 solved my problem. I do believe 2 or 3 years ago I actually did hit a bug in StructureMap that was since fixed. – Chris Marisic Jun 09 '11 at 17:57
  • 1
    We have a route in our web app that dumps WhatDoIHave() to the browser. Very useful. – Joshua Flanagan Jun 11 '11 at 01:28