Problem with TryGetInstance using StructureMap and NET MVC 2

I use StructurMap (just upgraded to version 2.6.1) and Jimmy Bogard Smart Model Binders in my NET MVC 2 application. I also adapt the Dominic Pettifer technique so that you can use a smart model binder to insert DI into your models viewing for postback scenarios where a list of lists is required to be populated!

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

So, in my IOCMOdelBinder classI am trying to use TryGetInstance()instead GetInstance(), since the first returns null if there is no match for the type modelType. Basically, if it does not find the registered instance, then return to the model binding by default.

My override class is CreateModelas follows:

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);
   }
}

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

+3
source share
3 answers

TryGetInstance " , , null". " , - , null".

, StructureMap , , . , , , StructureMap . , , " ", . , TryGetInstance , , , , null. , GetInstance, , , .

+10

In the new version, TryGetInstance is quenched, and the dose no longer exists.

0
source

Source: https://habr.com/ru/post/1787952/


All Articles