Jeff Mc
Jeff Mc

Reputation: 3793

Using StructureMap to cache a named instance

I'm having difficulty figuring out how to have StructureMap cache a non-default instance. I can cache the default instance with code like this

ForRequestedType<ISession>()
            .CacheBy(InstanceScope.HttpContext)
            .TheDefault.Is.ConstructedBy(() => ObjectFactory.GetInstance<ISessionFactory>().OpenSession());

which works as expected. And I can create a named instance fine

InstanceOf<User>().Is.ConstructedBy(() => someAwesomeConstructor()).WithName("CurrentUser");

But I'd really like to cache "CurrentUser" by session, but .CacheBy() only exists in CreatePluginFamilyExpression<> and the only way I can tell to get from CreatePluginFamilyExpression<> to IsExpression<> is by the property TheDefault, which forces me to create a default, which I don't want.

Upvotes: 0

Views: 1755

Answers (2)

eglasius
eglasius

Reputation: 36037

Add a separate line with:

ForRequestedType<User>().CacheBy(...
InstanceOf<User>().Is.ConstructedBy(...

You can't currently configured caching for specific instances. The same happens if you had an IUser, and you wanted different caching for User than for another class, say PerRequestUser. In that case, if you are requesting the instance by IUser, then the same caching will always apply.

Its a current limitation of SM. The above might be what you need though. Alternatively, consider rolling your own lines that get called in the place of this expression:

() => someAwesomeConstructor()

Instead you would call:

() => GetUserCachingInSession()

And in that method, you have simple logic that retrieves it from session, if it isn't gets a new one saving it in the session.

Upvotes: 1

Matt Hinze
Matt Hinze

Reputation: 13679

this was as close as I was able to get quickly

ForRequestedType<IInterface>()
    .AddInstances(x => x
        .OfConcreteType<Implementation>()
        .WithName("foo"))
    .CacheBy(InstanceScope.HttpContext);

Upvotes: 3

Related Questions