Injecting an ObjectContext constructor using Unity / Prism / MVVM

I am developing an application using WPF with the MVVM and Prism pattern. Views are added to the ModuleCatalog, and viewmodels are registered in the unity container. To do this, I use Bootstrapper, which is responsible for creating the shell, setting up the unit container and the module directory.
The question is how to enter my EntityContext into several view modes.
Bootstrapper first:

 
public class Bootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            Shell shell = Container.Resolve();
            shell.Show();
            return shell;
        }

    protected override void ConfigureContainer()
    {
        base.ConfigureContainer();
        Container.RegisterType<EntityContext >("Context");
        Container.RegisterType<PersonViewModel>(new InjectionConstructor(
            new ResolvedParameter<EntityContext >("Context")));
    }

    protected override IModuleCatalog GetModuleCatalog()
    {
        ModuleCatalog catalog = new ModuleCatalog();
        catalog.AddModule(typeof(PersonModule));
        return catalog;
    }

code>

The video model looks like this (excerpt)


public class PersonViewModel : ViewModelBase, IDataErrorInfo
    {
        private Person _person;
        private PersonRepository _repository;
        readonly EntityContext _context;

    public PersonViewModel(EntityContext context)
    {
        _context = context;
        _person = new Person();
        _repository = new PersonRepository(context);
    }

code>

Module:


    public class PersonModule : IModule
    {
        private readonly IRegionManager regionManager;

    public PersonModule(IRegionManager regionManager)
    {
        this.regionManager = regionManager;
    }

    public void Initialize()
    {
        regionManager.RegisterViewWithRegion("PersonData", typeof(PersonView));
    }

}

code>

Code Code:


    public partial class PersonView : UserControl
    {
        private PersonViewModel _vm;

    public PersonView()
    {
        InitializeComponent();
    }

    [Dependency]
    public PersonViewModel VM
    {
        get
        {
            return this.DataContext as PersonViewModel;
        }
        set
        {
            _vm = value;
            this.DataContext = _vm;
        }
    }      
}

code> I'm not sure that my approach works in principle, but in order to save changes to the database, I need my context in the knowledge of the changes made to it. Now it clearly does not work, as a ModuleInitializeException is occurring . StackTrace:
An exception occurred while initializing the "PersonModule" module.
  - Exception message: An exception occurred while trying to add a view to the "PersonData" area.
  - The most likely cause was the following: "System.InvalidOperationException: the EntityContext type has several constructors of length 1. Cannot resolve the ambiguity.
 Bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase 1.SelectConstructor (IBuilderContext context)1.FindLongestConstructor(Type typeToConstruct)
bei Microsoft.Practices.ObjectBuilder2.ConstructorSelectorPolicyBase

 bei Microsoft.Practices.ObjectBuilder2.DynamicMethodConstructorStrategy.PreBuildUp (IBuilderContext context)
 bei Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp (IBuilderContext context).

  But also check InnerExceptions for more details or call .GetRootException (). - The assembly that the module was trying to load was: App, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null
  For more information, check the InnerException property for an exception. If an exception occurred while creating the object in the DI container, you can exclude it. GetRootException () to help find the root cause of the problem.

, , , .
.

+3
1

EntityContext:

Container.RegisterType<EntityContext >("Context", new InjectionConstructor(...))
+5

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


All Articles