views:

125

answers:

2

I have a default concrete type defined in a registry:

    ForRequestedType<IXRepository>()
        .TheDefaultIsConcreteType<CacheXRepository>();

The ChaceXRepository has the following constructor:

public class CacheXRepository: IXRepository{

    public CacheXRepository(IXRepository xRepository,ICache cacheService){

In the constructor it receives an object that has the same interface type as itself, but I want to pass in a different concrete type.

How do I define in the registry that if the type is a CacheXRepository then I want the concrete type for parameter IXRepository to be XRepository, in all other cases IXRepository should resolve to CacheXRepository.

+1  A: 

You should take a look at the enrichwith method. Then the code will look something like:

      ForRequestedType<IXRepository>().TheDefault.Is
            .OfConcreteType<XRepository>()
            .EnrichWith(
            (context, repository) =>
            new CacheXRepository(repository));

Look at this page for more info: http://codebetter.com/blogs/jeremy.miller/archive/2008/01/27/interception-techniques-in-structuremap-2-5.aspx

gautema
Thank you!The code I used based on your reply: ForRequestedType<IXRepository>() .TheDefaultIsConcreteType<XRepository>() .EnrichWith((repository) => new CacheXRepository(repository,ObjectFactory.GetInstance<ICache>()));
Devora
A: 

How about something like (not testet):

ForRequestedType<IXRepository>()
  .TheDefaultIsConcreteType<CacheXRepository>()
  .WithCtorArg("xREpository")
  .EqualTo(new XRepository());

Im not so familiar with that Structuremap dialect :)

In the version I am using it would be:

For<IXRepository>()
  .Use<CacheXRepository>()
  .CtorDependency<IXRepository>("xRepository")
  .IsConcreteType<XRepository>();
Luhmann
Thanks for the quick response, I ended up using a different answer since it used the syntax in my version. Thank you, I appreciate your help!
Devora
No problem - you should upvote his answer as well as accepting it :)
Luhmann