StructureMap Open Generics and CacheBy Singleton

I have several repositories that inherit from the Repository base class. I am currently registering an implementation in memory with a struct like this (and it works great):

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

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

      

I thought it would be nice to use Open Generics' StructureMap support to register the whole thing (the number goes up), so when I add a new repository, I don't need to update the ServiceRegistry.

I've tried this:

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

      

This does not work. It does not throw an exception, but it acts like the repositories are not single. Anything added to it does not persist throughout the life of the application.

Is it possible to register an open shared file and implement caching with a singleton scope? This is an ASP.NET MVC application and I just want the repositories to work before restarting the application.

+2


source to share


1 answer


I had a similar scenario and was able to successfully solve it using the new strong syntax (using StructureMap 2.6.3.0):



x.For(typeof(Repository<>))
 .LifecycleIs(InstanceScope.Singleton)
 .Use(typeof(InMemoryRepository<>));

      

0


source







All Articles