Windsor - 解析项目后添加的组件和服务

时间:2013-02-05 15:47:15

标签: wpf castle-windsor

我在WPF项目中使用最新的Castle Windsor,我遇到了一个非常奇怪的问题。

解析我的一个ViewModel时 - 容器自我注册不需要作为服务的类型并注入这些类型的空属性, 容器是否可以自行注册服务?

我可以看到,在配置容器之后检查内核的服务集合,我在一个解析之前停止了,并且在解析结束后又添加了3个服务......

如果有人能够对这个问题有所了解,那将会非常有帮助 感谢

我的配置文件:

    public IContainerService Get()
    {
        var container = new WindsorContainer();
        var adapter = new ContainerServiceWindsorAdapter(container.Kernel);

        container.Register(Component.For<IWindowManager>().ImplementedBy<TelerikWindowManager>());
        container.Register(Component.For<IEventAggregator>().ImplementedBy<EventAggregator>());
        container.Register(Component.For<INavigationService>().ImplementedBy<NavigationService>());
        container.Register(Component.For<IFileService>().ImplementedBy<FileService>());
        container.Register(Component.For<ISessionFactory>().UsingFactoryMethod(k =>
        {
            var fs = container.Resolve<IFileService>();
            var normalConfig = new Configuration().Configure(Path.Combine(fs.GetWorkingFolder(), CONFIG_FILE));

            return Fluently.Configure(normalConfig)
                           .CurrentSessionContext<ThreadStaticSessionContext>()
                           .Mappings(m => m.FluentMappings.AddFromAssemblyOf<NHDataAccessProvider>())
                           .ExposeConfiguration(BuildSchema)
                           .BuildSessionFactory();
        }));
        container.Register(Component.For<IDataAccessProvider>().ImplementedBy<NHDataAccessProvider>());

        //Register IShell
        container.Register(Component.For<IShell>().ImplementedBy<ShellViewModel>()
            .Properties(p => p.PropertyType != typeof(IModuleDataScreen)));

        //Register all IModuleDataScreens
        container.Register(AllTypes.FromAssemblyContaining<ShellViewModel>()
                     .BasedOn(typeof(IModuleDataScreen))
                     .WithService.FromInterface(typeof(IModuleDataScreen))
                     .Configure(x => x.LifeStyle.Is(LifestyleType.Transient))
                     .Configure(x => x.Named(x.Implementation.Name)));

        //Register all Modules
        container.Register(AllTypes.FromAssemblyContaining<ShellViewModel>()
                .BasedOn(typeof(IModule))
                .WithService.FromInterface(typeof(IModule))
                .Configure(x => x.LifeStyle.Is(LifestyleType.Singleton))
                .Configure(x => x.Named(x.Implementation.Name)));


        container.Install(new CommonComponentsInstaller(),new DynamicCalculationsInstaller());

        var sf = container.Resolve<ISessionFactory>();
        CurrentSessionContext.Bind(sf.OpenSession());

        return adapter;
    }

public class DynamicCalculationsInstaller : IWindsorInstaller
{
    public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
    {
        container.Register(Component.For<IFormulaEvaluator>().ImplementedBy<FleeFormulaEvaluator>().LifeStyle.Singleton);
        container.Register(Component.For<IRulesEvaluator>().ImplementedBy<FleeRulesEvaluator>().LifeStyle.Singleton);

        container.Register(Component.For<IPlansModule>()
                                    .ImplementedBy<PlansModule>()
                                    .LifeStyle
                                    .Transient);

        container.Register(
            Component.For<IDataProvider<PackageData>>()
                     .ImplementedBy<PackageDataProvider>()
                     .LifeStyle
                     .Transient);

        container.Register(Component.For<IPackagesModule>()
                                    .ImplementedBy<PackagesModule>()
                                    .LifeStyle
                                    .Transient);

        container.Register(Component.For<ExternalRuleSetService>());
    }
}

public class CommonComponentsInstaller : IWindsorInstaller
{

    #region Implementation of IWindsorInstaller

    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(Component.For<IErrorHandler>().ImplementedBy<ErrorHandlerEntLibAdapter>());
        container.Register(Component.For<ICacheService>().ImplementedBy<CacheServiceEntLibAdapter>());
        container.Register(Component.For<ILogger>().ImplementedBy<LoggerEntLibAdapter>());
        container.Register(Component.For<IMessageSerializer>().ImplementedBy<MessageSerializer>());
        container.Register(Component.For<IInteroperableSerializer>().ImplementedBy<InteroperableSerializer>());
        container.Register(Component.For<ILazyComponentLoader>().ImplementedBy<ConcreteClassComponentLoader>().Named("concreteClass"));
    }

    #endregion

}

1 个答案:

答案 0 :(得分:0)

您所指的功能称为ILazyComponentLoader s。

这是一个扩展点,Windsor允许组件在需要的地方注册。

Windsor本身在很少的地方使用它,最常用的是delegate-based typed factories

现在,解决方案。如果您不希望Windsor满足这些依赖关系,请不要将它们暴露给容器。

Container.Register(
    Component.For<MyType>().Properties(PropertyFilter.IgnoreAll));
   );