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
  • 25,938
  • 27
  • 134
  • 243
DarthVader
  • 49,515
  • 70
  • 199
  • 295

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,478
  • 2
  • 29
  • 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
  • 14,990
  • 3
  • 40
  • 50
  • 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
  • 79,093
  • 40
  • 195
  • 263
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
  • 25,938
  • 27
  • 134
  • 243
Filippo Pensalfini
  • 1,704
  • 12
  • 15