0

Using the following Generic Repository.

public interface IRepository<T> where T: class
{
    void Commit();
    void Delete(T item);
    IQueryable<T> Find();
    IList<T> FindAll();
    void Add(T item);     
}

How do you code structuremap to use it? I'm using Structuremap v 2.6.1 for .net 3.5

public static void BootstrapStructureMap()
{
    ObjectFactory.Initialize(x =>
    {                   
        // This is giving me problems!
        x.For<IRepository<Employee>>().Use<IRepository<Employee>>(); 
    });
}

I get the following error:

StructureMap Exception Code 202 No default instance defined for plugin family

Steven
  • 166,672
  • 24
  • 332
  • 435
InCode
  • 503
  • 3
  • 8
  • 25
  • possible duplicate of [StructureMap Exception Code: 202 No Default Instance defined for PluginFamily](http://stackoverflow.com/questions/2910493/structuremap-exception-code-202-no-default-instance-defined-for-pluginfamily) – Nathan Apr 23 '14 at 19:13
  • Nope Still having the same issue. – InCode Apr 23 '14 at 19:16

1 Answers1

2

With the For().Use() construct, you tell StructureMap to instantiate the type given in the Use when someone requests the type in the For. So typically, you supply the For with an interface or abstract base class, since you program against abstractions.

The problem here is that you passed in an abstract type (your IRepository<T>) into the Use method. StructureMap will be unable to create a new instance of that interface. You need to create a generic implementation of IRepository<T> (EntityFrameworkRepository<T> for instance) and register that. Example:

x.For<IRepository<Employee>>().Use<EntityFrameworkRepository<Employee>>();

This however will pretty soon lead to a lot of registrations, since you will have a dozen of repositories that you want to use. So instead of making many registrations for every closed generic repository you want to use, you can reduce this to a single registration using the open generic type, as follows:

x.For(typeof(IRepository<>)).Use(typeof(EntityFrameworkRepository<>)));
Steven
  • 166,672
  • 24
  • 332
  • 435