0

我在我的 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

}
4

1 回答 1

0

您所指的功能称为ILazyComponentLoaders。

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

Windsor 本身在少数地方使用它,最常用的是基于委托的类型工厂

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

Container.Register(
    Component.For<MyType>().Properties(PropertyFilter.IgnoreAll));
   );
于 2013-02-05T23:41:54.317 回答