.NET Core 2.0中StructureMap实现验证代理类的DI配置问题
Absolutely this is feasible—you just need to tweak your StructureMap registration to avoid the circular dependency that's tripping you up. Let's walk through why your initial attempts failed and how to fix it.
Why Your Previous Configurations Failed
Your first two attempts ran into circular dependencies because StructureMap couldn't distinguish between the proxy and the real implementation when resolving IComponentDataAccessor:
- Attempt #1:
Forward<IComponentDataAccessor, ComponentDataAccessorProxy>tells StructureMap to redirect requests forIComponentDataAccessortoComponentDataAccessorProxy, but since the proxy itself depends onIComponentDataAccessor, the container tries to inject the proxy into itself—creating an infinite loop. - Attempt #2:
For<IComponentDataAccessor>().Use<ComponentDataAccessorProxy>has the same core issue: when constructing the proxy, it needs anIComponentDataAccessor, which the container tries to resolve as the proxy again, triggering the circular dependency.
The Fix: Explicitly Inject the Concrete Implementation
The solution is to register the concrete ComponentDataAccessor under its own type, then explicitly tell StructureMap to inject that concrete instance into the proxy when resolving IComponentDataAccessor. Here's the working configuration:
var container = new Container(); container.Configure(config => { // 1. First register your IMongoDatabase (you likely already have this set up) config.For<IMongoDatabase>().Use(...); // Add your existing IMongoDatabase registration here // 2. Register the concrete ComponentDataAccessor under its own type config.For<ComponentDataAccessor>().Use<ComponentDataAccessor>(); // 3. Register the proxy as the implementation for IComponentDataAccessor // and explicitly inject the concrete ComponentDataAccessor into the proxy's constructor config.For<IComponentDataAccessor>().Use<ComponentDataAccessorProxy>() .Ctor<IComponentDataAccessor>().Is<ComponentDataAccessor>(); });
Alternative: Manual Instance Construction
If you prefer a more explicit hands-on approach, you can construct the proxy directly and resolve the concrete implementation from the container manually:
var container = new Container(); container.Configure(config => { config.For<IMongoDatabase>().Use(...); config.For<ComponentDataAccessor>().Use<ComponentDataAccessor>(); config.For<IComponentDataAccessor>().Use(ctx => new ComponentDataAccessorProxy(ctx.GetInstance<ComponentDataAccessor>()) ); });
How This Works
- When the container resolves
IComponentDataAccessor, it creates aComponentDataAccessorProxy. - Instead of trying to resolve
IComponentDataAccessoragain for the proxy's constructor, it directly injects theComponentDataAccessorconcrete type (which has no dependency on the proxy). ComponentDataAccessorgets its requiredIMongoDatabasefrom the container as expected.
This setup avoids the circular dependency entirely while letting you use the proxy for validation whenever IComponentDataAccessor is requested.
内容的提问来源于stack exchange,提问作者Node.JS

