StructureMap Conditional use

試著忘記壹切 提交于 2019-12-01 08:51:23

I think you should avoid the conditional construction syntax with if - if there is a simpler alternative. In your case I think that this will do:

For<ICacheStorage>().Use<NullObjectCache>();
For<Test>().Use<Test>.Ctor<ICacheStorage>().Is<HttpContextCacheAdapter>();
For<Test2>().Use<Test2>.Ctor<ICacheStorage>().Is<HttpContextCacheAdapter>();

Thanks i did find the solution if eny one of you neade hear is it (i did change some thing in condition and is working ):

x.For<ICacheStorage>().ConditionallyUse(c =>
                                                                {
                                                                    c.If(t => t.ParentType == typeof(Test) ||
                                                                        t.ParentType == typeof(Test2))
                                                                        .ThenIt
                                                                        .Is.ConstructedBy(
                                                                            by => new HttpContextCacheAdapter());

                                                                    c.TheDefault.Is.ConstructedBy(
                                                                            by => new NullObjectCache());

                                                                });

Sorry for my bad English

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!