Auto -Registering View Models by convention, IHandler<> & EventDispatcher Castle Windsor

22 views
Skip to first unread message

Javanie Campbell

unread,
Mar 14, 2018, 12:16:26 AM3/14/18
to Castle Project Users


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);
           
}
 
       
}
 
   
}
}


My ViewModels are located in different Assemblies with the respective handlers unfortunate the dependencies are throwing an error that I can seem to resolve when I debug it the references are resolved but the window keeps showing up blank.

I have limited knowledge on using this IOC however I would love it if I can be assisted in resolving the issue by looking over my code and help me see what I have done or not?






Jonathon Rossi

unread,
Mar 14, 2018, 4:36:43 AM3/14/18
to castle-pro...@googlegroups.com
It appears your error is displaying while you are in the designer not a runtime error, so it is likely not everything has been set up correctly in your application because the entry point hasn't been invoked.

Usually you don't want code outside the view controls running in the designer, and would use WPF's GetIsInDesignMode to detect this (https://stackoverflow.com/questions/834283/is-there-a-way-to-check-if-wpf-is-currently-executing-in-design-mode-or-not).

--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to castle-project-u...@googlegroups.com.
To post to this group, send email to castle-pro...@googlegroups.com.
Visit this group at https://groups.google.com/group/castle-project-users.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages