views:

88

answers:

2

I have an interface IGenericRepository<TEntity> where TEntity : IEntity and an implementation GenericRepository<TEntity> where TEntity : Entity.

I'm trying to inject a specific IGenericRepository<Section> into a class using StructureMap:

    ObjectFactory.Initialize(x =>
        {
            x.For(typeof(IGenericRepository<>)).Use(typeof(GenericRepository<>));
        });

But when I try to use ObjectFactory.GetInstance<IGenericRepository<Section>>(); I get:

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily System.Data.Common.DbConnection

Any ideas why this is or what I'm doing wrong?

Thanks in advance,

Simon

+1  A: 

whats your constructor like for GenericRepository<>?

It or one of it's dependencies is expecting a DbConnection that SM cant create

Andrew Bullock
Thanks for that, my generic repository is taking an ObjectContext which I have no added through SM
simonjreid
+1  A: 

You are receiving in your GenericRepository's constructor a DbConnection, which is an abstract class, and are not configuring SM to know which specific class should use for it.

i.e.:

 ObjectFactory.Initialize(x =>
        {
            x.For(typeof(DbConnection)).Use(typeof(SqlConnection));
            x.For(typeof(IGenericRepository<>)).Use(typeof(GenericRepository<>));
        });
eglasius