Ticket T309140
Visible to All Users

ArgumentNullException in MS Unity Container & MVVM POCO

created 9 years ago

Hello DX Team

I'm trying to use a MS Unity IoC container with the MVVM framework.
I'm creating POCO view model with constructor injection but I keep getting ArgumentNullException (parameter name "source").
Reproduce sln is attached.

My example

C#
var container = new UnityContainer(); // It's ok var pocoVMWithoutParameterInCtorInstance = container .Resolve(ViewModelSource.GetPOCOType(typeof (VMWithoutParameterInCtor))); // It's ok var withoutViewModelSource = container.Resolve(typeof(VMWithParameterInCtor)); // It's throw var pocoViewModelInstance = container .Resolve(ViewModelSource.GetPOCOType(typeof (VMWithParameterInCtor)));
C#
[POCOViewModel] public class VMWithoutParameterInCtor { } [POCOViewModel] public class VMWithParameterInCtor { public VMWithParameterInCtor(SomeClass someClass) { } } public class SomeClass { }

Answers

created 9 years ago (modified 9 years ago)

Hello Anton,

You have to register the types with ViewModelSource.GetPOCOType method (to emit a ViewModel from the POCO) and then to resolve the ViewModels by POCO types:

C#
var container = new UnityContainer(); container.RegisterType(ViewModelSource.GetPOCOType(typeof(VMWithoutParameterInCtor))); container.RegisterType(ViewModelSource.GetPOCOType(typeof(VMWithParameterInCtor))); var withoutParameter = container .Resolve(typeof(VMWithoutParameterInCtor)); var withParameter = container .Resolve(typeof(VMWithParameterInCtor));

Best regards,
Mikhail

    Show previous comments (2)
    M M
    Mikhail Shubin [DevExpress MVP] 9 years ago

      Hello Anton,
      Yes, I use SimpleInjector, now Unity…
      It seems you have to use special Register method:
         var container = new UnityContainer();
         container.RegisterType(typeof(VMWithoutParameterInCtor), ViewModelSource.GetPOCOType(typeof(VMWithoutParameterInCtor)));
         container.RegisterType(typeof(VMWithParameterInCtor),
      ViewModelSource.GetPOCOType(typeof(VMWithParameterInCtor)), new InjectionConstructor(new SomeClass()));
         var withoutParameter = container.Resolve<VMWithoutParameterInCtor>();
         var withParameter = container.Resolve<VMWithParameterInCtor>();
      Now it works.
      In SimpleInjector case it looks simpler:
           //  SimpleInjection Container: https://simpleinjector.readthedocs.org/en/latest/index.html
           var simpleContainer = new Container();
           simpleContainer.Register(typeof(VMWithoutParameterInCtor), ViewModelSource.GetPOCOType(typeof(VMWithoutParameterInCtor)));
           simpleContainer.Register(typeof(VMWithParameterInCtor), ViewModelSource.GetPOCOType(typeof(VMWithParameterInCtor)));
           //  Optional registration for injecton
           //simpleContainer.RegisterSingleton<SomeClass>();
           //  Optional verification
           //simpleContainer.Verify();
           var instance1 = simpleContainer.GetInstance<VMWithoutParameterInCtor>();
           var instance2 = simpleContainer.GetInstance<VMWithParameterInCtor>();
      The sample project is attached.
      Thanks,
      Mikhail

        Hello, Mikhail.
        Thanks again for your help.
        About registration
        Yours variant of registration must be working.
        But this mean that I need register all my view models with all it's constructor parameters!
        If I use Generic view models (MyViewModel<T>) I need register all variations of it (ex: MyViewModel<int>, MyViewModel<string>, …).
        And I need support registrations during app life. It's not cool )
        About Unity
        My first question was exactly about MS Unity - how to use MS Unity with DX MVVM Framework.
        ViewModel without poco emit logic works fine (without any extra registrations)
          var withParameter = container.Resolve(typeof(VMWithParameterInCtor));
        after emit - simple way is not working )
        About SimpleInjector
        It also not solve problem with registration requirement. It's make the registration simpler.
        And I'm not ready to change my DI library )

        DevExpress Support Team 9 years ago

          Hi Anton,
          Thank you for your patience. We have researched this issue and it appears that we have found a way to resolve it on our side. If everything goes according to plan, the code you initially posted will work without additional modifications. I have created a separate report about the exception - ArgumentNullException exception occurs when calling UnityContainer.Resolve for a type created using ViewModelSource.GetPOCOType. We will update it once we have any news.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.