Tag: 结构图

使用相同的接口调用多个类

我有一个像这样的界面 public interface IAddressProvider { string GetAddress(double lat, double long); } 在我的消费类中,我想循环遍历具体的提供者,直到得到结果,如(简化): string address; address = _cachedAddressProvider.GetAddress(lat, long); if(address == null) address = _localDbAddressProvider.GetAddress(lat, long); if(address = null) address = _externalAddressProvider.GetAddress(lat, long); return address ?? “no address found”; 然后我可以模拟每个提供程序进行unit testing,将null设置为返回值以适当地测试所有代码路径。 我如何将接口注入我的消费类(最好使用StructureMap),以便正确解析每个具体的实现?

StructureMapexception代码:202没有为PluginFamily定义的默认实例

我是StructureMap的新手。 我已下载并使用的是2.6.1.0版。 我一直收到以下错误: StructureMapexception代码:202没有为PluginFamily Company.ProjectCore.Con.IConfiguration,Company.ProjectCore,Version = 1.0.0.0定义默认实例,Culture = neutral,PublicKeyToken = null 我的Global.asax.cs看起来像: protected void Application_Start(object sender, EventArgs e) { var container = new Container(x => { x.For().Use(); x.For().Use(); x.For().Use(); x.For().Use(); x.For().Use(); }); container.AssertConfigurationIsValid(); } 我从ObjectFactory.Initialize更改为“new Container”进行调试。 当单步执行AssertConfigurationIsValid()方法时,缓存有效,但EmailService在以下行中的GetInstance方法失败: [Pluggable(“Default”)] public class EmailService : IEmailService private readonly IConfiguration _configuration; public EmailService() { _configuration = ObjectFactory.GetInstance(); } 如果我删除IEmailService,IUserSession会抛出相同的202错误。 […]

从StructureMap获取的HttpContext上的Null User

好吧,我之前的问题/设置有太多的变量,所以我把它剥离到它的裸骨组件。 给出下面的代码使用StructureMap3 … //IoC setup For().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null )); For().Use(); //Classes used public class CurrentUser : ICurrentUser { public CurrentUser(HttpContextBase httpContext) { if (httpContext == null) return; if (httpContext.User == null) return; var user = httpContext.User; if (!user.Identity.IsAuthenticated) return; UserId = httpContext.User.GetIdentityId().GetValueOrDefault(); UserName = httpContext.User.Identity.Name; } […]