using Castle.Core;
using Castle.Facilities.TypedFactory;
using Castle.MicroKernel.Registration;
using Castle.Windsor;
using LMS.Infrastructure;
using LMS.Infrastructure.Interop.Network;
using MvvmFramework;
using QLMS.ViewModels;
using QLMS.ViewModels.Handlers;
using QLMS.Views.DesignTime;
using QLMS.Views.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace QLMS.Views
{
public class Bootstrapper : BootstrapperBase
{
private WindsorContainer container;
protected override IEnumerable<Assembly> SelectAssemblies()
{
return new[] { typeof(MainViewModel).Assembly };
}
protected override void ConfigureForRunTime()
{
container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(new AppSettingsConvention());
container.AddFacility<TypedFactoryFacility>();
container.Register(
Component.For<ICheckInternetConnectivity>()
.ImplementedBy<CheckInternetConnectivity>(),
Component.For<IEventAggregator>()
.ImplementedBy<EventAggregator>().LifestyleSingleton(),
//Component.For<ShellViewModel>().LifestyleSingleton(),
Component.For<NotificationsViewModel>().LifestyleSingleton(),
Classes
.FromAssembly(typeof(MainViewModel).Assembly)
.BasedOn(typeof(IHandle<>))
.Configure(x => x.LifeStyle.Is(LifestyleType.Singleton)),
//.WithServiceAllInterfaces()
//.LifestyleSingleton()
Classes.FromAssembly(typeof(ViewModel).Assembly).InSameNamespaceAs<ViewModel>().WithServiceDefaultInterfaces()
);
RegisteViewModels();
RegisterRoutes();
}
protected override void ConfigureForDesignTime()
{
container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(new AppSettingsConvention());
container.Register(
Component.For<ICheckInternetConnectivity>()
.ImplementedBy<DesignTimeCheckInternetConnectivity>(),
Component.For<IEventAggregator>()
.ImplementedBy<EventAggregator>().LifestyleSingleton(),
//Component.For<ShellViewModel>().LifestyleSingleton(),
Component.For<NotificationsViewModel>().LifestyleSingleton(),
Classes
.FromAssembly(typeof(RouteToViewHandler).Assembly)
.BasedOn(typeof(IHandle<>))
.Configure(x => x.LifeStyle.Is(LifestyleType.Singleton))
//Classes.FromAssembly(typeof(ViewModel).Assembly).InSameNamespaceAs<ViewModel>().WithServiceDefaultInterfaces(),
//AllTypes.
);
RegisteViewModels();
RegisterRoutes();
container.AddFacility<TypedFactoryFacility>();
}
protected override object GetInstance(Type service, string key)
{
return string.IsNullOrWhiteSpace(key)
? container.Resolve(service)
: container.Resolve(key, service);
}
private void RegisteViewModels()
{
container.Register(Classes.FromAssembly(typeof(ShellViewModel).Assembly)
.Where(x => x.Name.EndsWith("ViewModel")
&& !x.Name.Equals(nameof(NotificationsViewModel))
&& !x.Name.Equals(nameof(ShellViewModel))
)
//.WithServiceDefaultInterfaces()
.Configure(x => x.LifeStyle.Is(LifestyleType.Transient)));
container.Register(Component.For<ShellViewModel>().LifestyleSingleton());
}
private void RegisterRoutes()
{
var routes = container.ResolveAll(typeof(IHandle));
var aggregator = container.Resolve<IEventAggregator>();
foreach (var route in routes)
{
aggregator.Subscribe(route);
}
}
}
}