2

I have IRepository interface with which i want to use NHibernateRepository.

How do i configure it with structure map?

protected void ConfigureDependencies()
{
    ObjectFactory.Initialize(
        x =>
            {
                x.For<ILogger>().Use<Logger>();
                x.For<IRepository<T>>().Use<NHibernateRepository<T>>();
            }
        );
}

I m getting an error on T.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
DarthVader
  • 52,984
  • 76
  • 209
  • 300

4 Answers4

2

If you want to be able to map all closing types of IRepository<> to the corresponding closing type for NHibernateRepository<>, use:

x.For(typeof(IRepository<>)).Use(typeof(NHibernateRepository<>))
Joshua Flanagan
  • 8,527
  • 2
  • 31
  • 40
0

This line is expecting a substitution for the generic parameter T:

x.For<IRepository<T>>().Use<NHibernateRepository<T>>();

That is, which type T will be stored in the repository? You've chosen the NHibernateRepository class as the implementation for IRepository, but haven't shown which internal class will be stored.

Alternatively, look at using non-generic IRepository, see here: Generic repository - IRepository<T> or IRepository

Community
  • 1
  • 1
yamen
  • 15,390
  • 3
  • 42
  • 52
  • both interface and implementations are generic, so i can use any model class. – DarthVader Apr 15 '12 at 23:26
  • You can absolutely, but you can't use a generic type where you have it in your code because it is undeclared in the class. Perhaps use IRepository without generics, or try dynamic as suggested by the other answers. – yamen Apr 15 '12 at 23:31
0

Perhaps replace <T> with dynamic?

x.For<IRepository<dynamic>>().Use<NHibernateRepository<dynamic>>();

As for the second point, the Singleton / Service Locator pattern is a rather heated debate.

Travis J
  • 81,153
  • 41
  • 202
  • 273
0

Have a look at this article. Basically, what you want to do is something like this:

public void ConfigureDependencies()
{
    ObjectFactory.Initialize(x => x.Scan(cfg =>
    {
        cfg.TheCallingAssembly();
        cfg.IncludeNamespaceContainingType<Logger>();
        cfg.ConnectImplementationsToTypesClosing(typeof(NHibernateRepository<>));
    }));
}

Regarding the ApplicationContext static class: if you really have a cross-cutting concern, then I see nothing really wrong with it.

ahsteele
  • 26,243
  • 28
  • 134
  • 248
Filippo Pensalfini
  • 1,714
  • 12
  • 16