使用StructureMap和NET MVC 2的TryGetInstance的问题 [英] Issue with TryGetInstance using StructureMap and NET MVC 2

查看:60
本文介绍了使用StructureMap和NET MVC 2的TryGetInstance的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用StructurMap(刚刚升级到2.6.1)和采用Dominic Pettifer的技术,以便您可以使用智能模型绑定器将DI注入到viewModel中,以用于需要重新填充选择列表的回发场景!

I am using StructurMap (just upgraded to 2.6.1) and Jimmy Bogard's Smart Model Binders within my NET MVC 2 application. I am also adapting a technique by Dominic Pettifer so that you can use the smart model binder to inject DI into your viewModels for postback scenarios where select lists need to be repopulated!

我对StructureMap知之甚少,而我遇到的问题之一是使用无参数构造函数进行viewModel绑定的structuremap 202 no instance defined error.

I know very little about StructureMap, and one of the problems I was getting was a structuremap 202 no instance defined error for viewModel binding with parameterless constructors.

所以在我的IOCMOdelBinder class中,我尝试使用TryGetInstance()而不是GetInstance(),因为如果与modelType不匹配,则前者返回null.基本上,如果找不到注册的实例,则使用默认的模型绑定器.

So In my IOCMOdelBinder class I am trying to use TryGetInstance() instead of GetInstance() as the former returns null if there is no match with the modelType. Basically if it doesn't find a registered instance then fall back to the default model binder.

我的替代CreateModel类如下:

protected override object CreateModel(ControllerContext controllerContext,
ModelBindingContext bindingContext, Type modelType)
 {

   var myInstance = ObjectFactory.TryGetInstance(modelType);

   if (myInstance != null)
   {
     return myInstance;
   }
   else
   {
     return base.CreateModel(controllerContext, bindingContext, modelType);
   }
}

我删除了ObjectFactory.GetInstance(modelType);行,我希望它们以相同的方式工作,但是TryGetInstance返回null,而GetInstance返回正确的对象OK,因此它肯定在注册表中.我可以使用GetInstance,但必须将其包装在try catch中,这不太优雅!!!有什么建议吗?

I took out the line ObjectFactory.GetInstance(modelType); I would expect them to work the same way but TryGetInstance returns null and GetInstance returns the correct object OK so it is definitely in the registry. I can use GetInstance but have to wrap it in try catch which is a little less elegant!!! Any suggestions please?

推荐答案

TryGetInstance的意思是如果容器知道类型,则返回它,否则返回null".这并不意味着从容器中请求类型,如果由于某种原因而被炸毁,则返回null".

TryGetInstance means "if the container knows about the type, return it, otherwise return null". It does not mean "request the type from the container, if it blows up for any reason, return null".

最令人困惑的是StructureMap无需先注册即可解析具体类型的能力.这是一个非常不错的功能,因为如果您只想让StructureMap解析其依赖项,则不必注册所有具体类型.但是,由于您不必预先注册具体类型,因此容器不会了解它们",因为它们没有任何配置.因此,当TryGetInstance检查以了解是否知道具体类型时,它决定不知道具体类型,并返回null.但是,如果您只是简单地执行GetInstance,它甚至不会检查是否知道该类型,而是将其识别为具体类型并只是构建它.

What causes the most confusion is StructureMap's ability to resolve concrete types without requiring you to register them first. This is a very nice feature, as you do not have to register all of your concrete types if you just want StructureMap to resolve their dependencies. However, since you do not have to pre-register concrete types, the container doesn't "know about them", because there isn't any configuration for them. So when TryGetInstance checks to see if it knows about the concrete type, it decides it doesn't, and returns null. However, if you were to simply do GetInstance, it doesn't even check to see if it knows about the type, it recognizes it as a concrete and just builds it.

这篇关于使用StructureMap和NET MVC 2的TryGetInstance的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆