Our application implements MVVM-WPF-Unity and I adding a feature that will add plugins in our application thru MEF. Thus I have a Plugin Manager class and plugin class.
In the Plugin class module, I have this.
namespace project.plugin
{
[Export("Module",typeof(IModule))]
public class Plugin : IModule
{
private readonly IRegionManager regionManager;
private readonly IUnityContainer container;
}
[ImportingConstructor]
public Module(IRegionManager regionManager, IUnityContainer container)
{
this.regionManager = regionManager;
this.container = container;
}
public void Initialize()
{
RegisterViewsAndServices();
}
}
Inside my Plugin Manager I did this:
[ImportMany]
public IEnumerable<Lazy<IModule>> ImportedModules { get; set; }
AggregateCatalog moduleCatalog = new AggregateCatalog(
new AssemblyCatalog(typeof(IModule).Assembly),
new DirectoryCatalog("Plugins"));
CompositionContainer container = new CompositionContainer(moduleCatalog);
container.ComposeParts(this);
foreach (IModule iModule in ImportedModules)
{
iModule.Initialize();
}
The problem here is that ImportedModules seems to be empty although as I looked in my container catalog I can see the plugin class has been exported. Help please please please
Related
Hello I'm trying to setup an architecture where only one module gets booted when the app is launched. Then I'd like to lazy load other modules based on the user's actions.
To achieve this in my app.xaml.cs I have one module loaded at bootstrap time (MainModule), and an other has InitializationMode = InitializationMode.OnDemand
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
Type BlipModuleType = typeof(BlipModule);
moduleCatalog.AddModule(new ModuleInfo()
{
ModuleName = BlipModuleType.Name,
ModuleType = BlipModuleType.AssemblyQualifiedName,
InitializationMode = InitializationMode.OnDemand
});
moduleCatalog.AddModule<MainModule>();
}
then my main module, which displays the view correctly, has a single view registered to the only region available:
public class MainModule : IModule
{
private readonly IRegionManager _regionManager;
public MainModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ViewA));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
The lazy loaded module has the same structure, registering a different view (which works properly if i decide to use it as my main module)
public class BlipModule : IModule
{
private readonly IRegionManager _regionManager;
public BlipModule(IRegionManager regionManager)
{
_regionManager = regionManager;
}
public void OnInitialized(IContainerProvider containerProvider)
{
_regionManager.RegisterViewWithRegion(RegionNames.ContentRegion, typeof(ViewB));
}
public void RegisterTypes(IContainerRegistry containerRegistry)
{
}
}
finally I have a Command in the viewmodel of my MainModule ViewA, that is supposed to load the new module and navigate to it.
public class ViewAViewModel : BindableBase
{
const string BlipModuleName = "BlipModule";
public ReactiveCommand ChangeRoute { get; set; } = new ReactiveCommand();
public ViewAViewModel(IRegionManager regionManager, IModuleManager moduleManager)
{
ChangeRoute.Subscribe(res =>
{
moduleManager.LoadModule(BlipModuleName);
});
moduleManager.LoadModuleCompleted += (s, e) =>
{
if (e.ModuleInfo.ModuleName == BlipModuleName)
{
regionManager.RequestNavigate(RegionNames.ContentRegion, new Uri(BlipModuleName, UriKind.Relative));
}
};
}
}
The viewB of the BlipModule is actually loaded (I get a hit if I set a breakpoint in the view's constructor), but instead of the view I get a white page with "System.Object" inside of it.
Any idea? thanks!
You want to RegisterForNavigation instead of RegisterViewWithRegion.
I'm trying to set up a WPF app to call the new window on a menu click with the data provider interface injected into the new viewmodel.
Followed many tutorials and created the Bootstrapper for Caliburn, a service locator and module for ninject. So far the main view doesn't need the IDataProvider but I'd like to open a new window on click event.
The Bootstrapper:
public class Bootstrapper : BootstrapperBase
{
public Bootstrapper()
{
Initialize();
}
protected override void OnStartup(object sender, StartupEventArgs e)
{
DisplayRootViewFor<MainScreenViewModel>();
}
}
The Service Locator and Module:
public class ServiceLocator
{
private readonly IKernel _kernel;
public ServiceLocator()
{
_kernel = new StandardKernel(new ServiceModule());
}
public MainScreenViewModel MainScreenViewModel => _kernel.Get<MainScreenViewModel>();
public NewLayoutViewModel NewLayoutViewModel => _kernel.Get<NewLayoutViewModel>();
}
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<ISqlite>().To<Sqlite>();
Bind<IDataProvider>().To<DataProvider>();
}
}
And this is where I got stuck:
public class MainScreenViewModel : Conductor<object>
{
private IWindowManager _windowManager;
public MainScreenViewModel()
{
_windowManager = new WindowManager();
}
public void NewLayout()
{
_windowManager.ShowWindow(new NewLayoutViewModel());
}
}
since the NewLayoutViewModel requires the IDataProvider.
Not sure, what am I missing, but in my understanding Ninject should take care of this di for NewLayoutViewModel.
Found a good solution from Tim Corey on YouTube.
Basically the answer is, if you not insist Ninjet, use Caliburn.Micro's build-in DI solution "SimpleContainer".
Recently I am going through some old code and found the below code
public class ProfileModule : IModule
{
private readonly IRegionManager regionManager;
private readonly IUnityContainer container;
private IEventAggregator eventAggregator;
public ProfileModule(IUnityContainer c, IRegionManager r, IEventAggregator e)
{
container = c;
regionManager = r;
eventAggregator = e;
}
public void Initialize()
{
// Create and add profiles as new Tab items
container.RegisterType<IProfileViewModel, Profile1ViewModel>(new ContainerControlledLifetimeManager());
regionManager.Regions[RegionNames.HomeRegion].Add(container.Resolve<ProfileView>());// HomeRegion is of type TabControl
container.RegisterType<IProfileViewModel, Profile2ViewModel>(new ContainerControlledLifetimeManager());
regionManager.Regions[RegionNames.HomeRegion].Add(container.Resolve<ProfileView>());
container.RegisterType<IProfileViewModel, Profile3ViewModel>(new ContainerControlledLifetimeManager());
regionManager.Regions[RegionNames.HomeRegion].Add(container.Resolve<ProfileView>());
}
}
Below is the ProfileView.xaml.cs
public partial class ProfileView : INotifyPropertyChanged
{
[InjectionConstructor]
public ProfileView(IProfileViewModel vm)
{
DataContext = vm;
InitializeComponent();
}
}
Below are the viewModels
public abstract class ProfileViewModelBase : IProfileViewModel, IDataErrorInfo, INotifyPropertyChanged
{
public ProfileViewModelBase(IEventAggregator eventAggregator, IRegionManager regionManager)
{
}
}
public class Profile1ViewModel : ProfileViewModelBase
{
[InjectionConstructor]
public Profile1ViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
: base (eventAggregator, regionManager)
{
}
}
public class Profile2ViewModel : ProfileViewModelBase
{
[InjectionConstructor]
public Profile2ViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
: base (eventAggregator, regionManager)
{
}
}
public class Profile3ViewModel : ProfileViewModelBase
{
[InjectionConstructor]
public Profile3ViewModel(IEventAggregator eventAggregator, IRegionManager regionManager)
: base (eventAggregator, regionManager)
{
}
}
The part of the code that is not clear for me is the ProfileModule.Initialise().
Everytime when the region manager is adding a view a new new instance of ProfileView is getting created and the viewModel is the one that is registered last.
First time ProfileView is created with Profile1ViewModel as a Datacontext.
Second time ProfileView is created with Profile2ViewModel as a Datacontext.
Third time ProfileView is created with Profile3ViewModel as a Datacontext.
How the container knows exactly which viewmodel to create when creating the view.
Also I understand , container.Resolve will return the view if it already got one, first time view is created and returned, second time I except same view will be returned, but a new view is created. same with third.
Can anyone explain what is happening?
Here goes:
What you can see inside the Initialize method is that after registering the IProfileViewModel the code is then immediately calling Resolve<ProfileView> which on the first Resolve is providing Profile1ViewModel to the ProfileView constructor. Then the second Register replaces the first registration with Profile2ViewModel. Therefore subsequent calls to Resolve will never give you an instance (or the singleton instance) of Profile1ViewModel.
If for some reason you really want to resolve the same instance of ProfileView then you need to Register this with the Unity container as a singleton like the below.
container.RegisterType(new ContainerControlledLifetimeManager());
This is obviously assuming you have an interface defined called IProfileView
I have recently puchased a very good book on MVVM - MVVM Survival Guide For Enterprise Architectures in Silverlight and WPF
Unfortunatly, one of the sections relating to IoC has a lot of code samples for StructureMap which is not available for Silverlight
Can anyone point me to a link that would help me translate Structure Map code to Autofac which is the injection tool I am looking at using
The code uses the factory mehod of creating classes and a bootstrapper
using Northwind.ViewModel;
using StructureMap;
namespace Northwind.UI.WPF
{
public class BootStrapper
{
public MainWindowViewModel MainWindowViewModel
{
get
{
return ObjectFactory
.GetInstance<MainWindowViewModel>();
}
}
public BootStrapper()
{
ObjectFactory.Initialize(
o => o.Scan(
a =>
{
a.WithDefaultConventions();
a.AssembliesFromApplicationBaseDirectory(
d => d.FullName
.StartsWith("Northwind"));
a.LookForRegistries();
}));
}
}
using StructureMap;
namespace Northwind.ViewModel
{
public class CustomerDetailsViewModelFactory
: ICustomerDetailsViewModelFactory
{
private readonly IContainer _container;
public CustomerDetailsViewModelFactory(
IContainer container)
{
_container = container;
}
public CustomerDetailsViewModel CreateInstance(
string customerID)
{
return _container
.With("customerID")
.EqualTo(customerID)
.GetInstance<CustomerDetailsViewModel>();
}
}
}
Paul
Autofac and StructureMap work differently, so you can't "translate" it one to one.
However, this is what it should look like to accomplish the same.
I've made some assumptions as not everything is there to test out your code.
public class BootStrapper
{
private readonly ILifetimeScope _container;
public BootStrapper()
{
var builder = new ContainerBuilder();
Assembly[] assemblies =
GetAssembliesFromApplicationBaseDirectory(
x => x.FullName.StartsWith("Northwind"));
builder.RegisterAssemblyTypes(assemblies)
.AsImplementedInterfaces();
// Module in Autofac = Registry in StructureMap
builder.RegisterAssemblyModules(assemblies);
Assembly viewModelAssembly =
typeof(MainWindowViewModel).Assembly;
builder.RegisterAssemblyTypes(viewModelAssembly);
_container = builder.Build();
}
private static Assembly[] GetAssembliesFromApplicationBaseDirectory(Func<AssemblyName, bool> condition)
{
string baseDirectoryPath =
AppDomain.CurrentDomain.BaseDirectory;
Func<string, bool> isAssembly =
file => string.Equals(
Path.GetExtension(file), ".dll", StringComparison.OrdinalIgnoreCase);
return Directory.GetFiles(baseDirectoryPath)
.Where(isAssembly)
.Where(f => condition(new AssemblyName(f)))
.Select(Assembly.LoadFrom)
.ToArray();
}
public MainWindowViewModel MainWindowViewModel
{
get
{
return _container.Resolve<MainWindowViewModel>();
}
}
}
public class CustomerDetailsViewModelFactory : ICustomerDetailsViewModelFactory
{
private readonly ILifetimeScope _container;
public CustomerDetailsViewModelFactory(ILifetimeScope container)
{
_container = container;
}
public CustomerDetailsViewModel CreateInstance(string customerID)
{
return _container.Resolve<CustomerDetailsViewModel>(
new NamedParameter("customerID", customerID));
}
}
Can anyone explain the difference between this way of loading modules in Prism:
protected override void InitializeModules()
{
IModule customerModule = Container.Resolve<CustomerModule.CustomerModule>();
IModule helloWorldModule = Container.Resolve<HelloWorldModule.HelloWorldModule>();
customerModule.Initialize();
helloWorldModule.Initialize();
}
and this way:
protected override IModuleCatalog GetModuleCatalog()
{
ModuleCatalog catalog = new ModuleCatalog()
.AddModule(typeof(CustomerModule.CustomerModule))
.AddModule(typeof(HelloWorldModule.HelloWorldModule));
return catalog;
}
I've seen both ways in demos but as far as I can tell they do the same thing, both seem to pass in a container and regionManager that I need in my modules:
public class CustomerModule : IModule
{
public IUnityContainer Container { get; set; }
public IRegionManager RegionManager { get; set; }
public CustomerModule(IUnityContainer container, IRegionManager regionManager)
{
Container = container;
RegionManager = regionManager;
}
public void Initialize()
{
RegionManager.RegisterViewWithRegion("MainRegion", typeof(Views.CustomerView));
}
}
Both IModuleCatalog GetModuleCatalog() and InitializeModules are from UnityBootstrapper.
GetModuleCatalog is for configuring how you want to load the module. And InitializeModules is for initializing the module.
GetModulecatalog will be fired before calling Initializing the module.
You don't need to override the InitializeModules for the most of scenarios but you will need to tell the UnityBootstrapper how you want your modules to be loaded (based on app.config, Directory Lookup or Xap Dynamic Loader or etc)
Hope it helps.