4

I am trying to wire up a series of decorators using structure map but am having some issues.

I have read this question which got me to the point where i can have a single decorated class, but i can't seem to get multiple levels of decorator working. What i have is class A which takes a connection string as a parameter, and then class B and class C which are both decorators for class A. I can get structuremap to create B which wraps A but I cant seem to get C wrapping B wrapping A.

This works for 1 level:

For<IQuestionRepository> ()
                .Use<LinqToSqlQuestionRepository> ()
                    .Ctor<string>("connectionString")
                    .Is(x=>System.Configuration.ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString)
                .EnrichWith (x=>new RecentQuestionCachedRepository(x))

and I thought that just adding another EnrichWith would work like so:

For<IQuestionRepository> ()
                .Use<LinqToSqlQuestionRepository> ()
                    .Ctor<string>("connectionString")
                    .Is(x=>System.Configuration.ConfigurationManager.ConnectionStrings["aspnetdbConnectionString"].ConnectionString)
                .EnrichWith (x=>new RecentQuestionCachedRepository(x))
                .EnrichWith (y=>new FeaturedQuestionCachedRepository(y));

but this just gives me a FeaturedQuestionCachedRepository which wraps a LinqToSqlQuestionRepository but the RecentQuestionCachedRepository is not anywhere in the stack.

What am I doing wrong?

Community
  • 1
  • 1
Sam Holder
  • 32,535
  • 13
  • 101
  • 181

1 Answers1

7

Do it in a single EnrichWith call:

.EnrichWith (x=> 
  new FeaturedQuestionCachedRepository(
  new RecentQuestionCachedRepository(x)
  )
)
Joshua Flanagan
  • 8,527
  • 2
  • 31
  • 40