I am working on an application that uses Prism's Unity for resolving dependency injection. But the problem is views and view models are arranges in their folders as:
But the problem is AutoWire method is working. So, I have to manually inject objects to resolve data context. my bootstrapper.cs class looks like this:
public class Bootstrapper : UnityBootstrapper
{
protected override DependencyObject CreateShell()
return this.Container.Resolve<Views.MainWindow>();
}
protected override void InitializeShell()
Application.Current.MainWindow.Show();
protected override void ConfigureContainer()
base.ConfigureContainer();
/*RegisterTypeIfMissing(typeof(IUnitOfWork),typeof(UnityServiceLocatorAdapter), true);
RegisterTypeIfMissing(typeof(IDbFactory), typeof(UnityServiceLocatorAdapter), true);
RegisterTypeIfMissing(typeof(IBlockRepository),typeof(UnityServiceLocatorAdapter), true);
RegisterTypeIfMissing(typeof(IBlockService), typeof(UnityServiceLocatorAdapter), true);
*/
// Container.RegisterType(typeof(object), typeof(DashboardView), "DashboardView");
/*Container.RegisterType(typeof(object), typeof(ViewA), "ViewA");
Container.RegisterType(typeof(object), typeof(ViewB), "ViewB");*/
Container.RegisterType<IUnitOfWork, UnitOfWork>();
Container.RegisterType<IDbFactory, DbFactory>();
Container.RegisterType<IUnitOfWork, UnitOfWork>("UnitOfWork", new ContainerControlledLifetimeManager());
Container.Resolve<IUnitOfWork>("UnitOfWork");
Container.RegisterType<IDbFactory, DbFactory>("DbFactory", new ContainerControlledLifetimeManager());
Container.Resolve<IDbFactory>("DbFactory");
#region Register Repositories
// Container.RegisterType<IBlockRepository, BlockRepository>();
// Container.RegisterType<IUse, DbFactory>();
Container.RegisterType<IFloorRepository, FloorRepository>("FloorRepository", new ContainerControlledLifetimeManager());
Container.Resolve<IFloorRepository>("FloorRepository");
Container.RegisterType<IBlockRepository, BlockRepository>("BlockRepository", new ContainerControlledLifetimeManager());
Container.Resolve<IBlockRepository>("BlockRepository");
Container.RegisterType<IUnitRepository, UnitRepository>("UnitRepository", new ContainerControlledLifetimeManager());
Container.Resolve<IUnitRepository>("UnitRepository");
Container.RegisterType<IResidentRepository, ResidentRepository>("ResidentRepository", new ContainerControlledLifetimeManager());
Container.Resolve<IResidentRepository>("ResidentRepository");
#endregion
// Container.Resolve();
#region Register Services
Container.RegisterType<IBlockService, BlockService>("BlockService", new ContainerControlledLifetimeManager());
// Container.Resolve<IBlockService>("BlockService");
// // Container.Resolve<IBlockService>("BlockService");
// Container.RegisterInstance(new BlockService(), new ContainerControlledLifetimeManager());
#region Register View Navigation
#region Register View
Container.RegisterTypeForNavigation<DashboardView>(NavCommandParams.DashboardView);
Container.RegisterTypeForNavigation<ManageBlocksView>(NavCommandParams.ManageBlocksView);
Container.RegisterTypeForNavigation<BlocksListView>(NavCommandParams.BlocksListView);
Container.RegisterTypeForNavigation<ManageUnitsView>(NavCommandParams.ManageUnitsView);
Container.RegisterTypeForNavigation<UnitsListView>(NavCommandParams.UnitsListView);
Container.RegisterTypeForNavigation<ManageFloorsView>(NavCommandParams.ManageFloorsView);
Container.RegisterTypeForNavigation<FloorsListView>(NavCommandParams.FloorsListView);
Container.RegisterTypeForNavigation<ManageResidentsView>(NavCommandParams.ManageResidentView);
Container.RegisterTypeForNavigation<ResidentListView>(NavCommandParams.ResidentListView);
#endregion //
Container.RegisterTypeForNavigation<BlockFormDialogView>("BlockFormDialogView");
// Container.RegisterTypeForNavigation<TopMenuView>("TopMenuView");
/*ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(x => {
var viewName = x.FullName;
viewName = viewName.Replace(".Views.", ".ViewModels.");
var viewAssemblyName = x.GetTypeInfo().Assembly.FullName;
var suffix = viewName.EndsWith("View") ? "Model" : "ViewModel";
var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}{1}, {2}", viewName, suffix, viewAssemblyName);
return Type.GetType(viewModelName);
});
ViewModelLocationProvider.SetDefaultViewModelFactory(type => Container.Resolve(type));*/
/*
protected override void ConfigureViewModelLocator()
base.ConfigureViewModelLocator();
ViewModelLocationProvider.SetDefaultViewTypeToViewModelTypeResolver(viewType =>
var viewName = viewType.FullName;
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName;
var viewModelName = String.Format(CultureInfo.InvariantCulture, "{0}{1}", viewName, suffix);
var assembly = viewType.GetTypeInfo().Assembly;
var type = assembly.GetType(viewModelName, true); return type;
}); }*/
public static class UnityExtentions
public static void RegisterTypeForNavigation<T>(this IUnityContainer unityContainer, string name)
unityContainer.RegisterType(typeof(object), typeof(T), name);
This project also uses repository and unit of work pattern but that's on a different layer but to create an object of any ViewModel I have to manually inject all those objects.
Is there any way to do this via IoC within Prism Unity.