开发者

Structuremap use different concrete type by object type

I have a default concrete type defined in a registry:

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

The ChaceXRepository has the following cons开发者_运维问答tructor:

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.


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


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>();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜