views:

236

answers:

0

I have a number of repositories that inherit from a base class Repository. Currently I am registering in-memory implmentations with Structure map like this (and it's working great):

ForRequestedType<Repository<Timeslot>>()
    .TheDefaultIsConcreteType<InMemoryRepository<Timeslot>>()
    .AsSingletons();

ForRequestedType<Repository<Appointment>>()
    .TheDefaultIsConcreteType<InMemoryRepository<Appointment>>()
    .AsSingletons();

I thought it would be nice to use StructureMap's support for Open Generics to register all these (the number is growing) so when I add a new Repository, I wouldn't have to update the ServiceRegistry.

I tried this:

ForRequestedType(typeof (Repository<>))
    .CacheBy(InstanceScope.Singleton)
    .TheDefaultIsConcreteType(typeof (InMemoryRepository<>));

That doesn't seem to be working. It doesn't throw an exception, but it acts like the repositories aren't singletons. Anything added to them doesn't persist during the application's life cycle.

Is it possible to register an open generic, and have the implementations cached by a singleton scope? This is an ASP.NET MVC app, and I just want the repositories to work until the application is restarted.