zoukankan      html  css  js  c++  java
  • IOC Container: Castle Windsor notes

    var container = new WindsorContainer();
    container.Register(Component.For<SauceBéarnaise>());
    SauceBéarnaise sauce = container.Resolve<SauceBéarnaise>();

    var container = new WindsorContainer();
    container.Register(Component
    .For<IIngredient>()
    .ImplementedBy<SauceBéarnaise>());
    IIngredient ingredient = container.Resolve<IIngredient>();

    return (IController)this.container.Resolve(controllerType);

    container.Register(Component
    .For<SauceBéarnaise, IIngredient>());

    container.Register(AllTypes
    .FromAssemblyContaining<Steak>()
    .BasedOn<IIngredient>());


    container.Register(AllTypes
    .FromAssemblyContaining<SauceBéarnaise>()
    .Where(t => t.Name.StartsWith("Sauce"))
    .WithService.AllInterfaces());

    container.Install(Configuration.FromAppConfig());
    <configSections>
    <section name="castle"
    type="Castle.Windsor.Configuration.AppDomain
    ?.CastleSectionHandler, Castle.Windsor" />
    </configSections>
    <castle>


    <components>
    <component id="ingredient.sauceBéarnaise"
    service="IIngredient"
    type="Steak"/>
    </components>
    </castle>


    public interface IWindsorInstaller
    {
    void Install(IWindsorContainer container, IConfigurationStore store);
    }

    public class IngredientInstaller : IWindsorInstaller
    {
    public void Install(IWindsorContainer container,
    IConfigurationStore store)
    {
    container.Register(AllTypes
    .FromAssemblyContaining<Steak>()
    .BasedOn<IIngredient>());
    }
    }

    container.Install(new IngredientInstaller());

    <installers>
    <install type="IngredientInstaller" />
    </installers>

    Castle Windsor lifestyles
    Name Comments

    Singleton This is Castle Windsor’s default lifestyle.
    Transient A new instance is created each time, but the instance is still tracked by
    the container.
    PerThread One instance is created per thread.
    PerWebRequest Requires registration in web.config (see section 10.2.2).
    Pooled Configuration of pool size will often be advisable (see section 10.2.2).
    Custom Write your own custom lifestyle (see section 10.2.3).

    container.Register(Component
    .For<SauceBéarnaise>()
    .LifeStyle.Transient);


    <component id="ingredient.sauceBéarnaise"
    service="IIngredient"
    type="Steak"
    lifestyle="transient" />

    container.Release(ingredient);


    container.Register(Component
    .For<IIngredient>()
    .ImplementedBy<SauceBéarnaise>()
    .LifeStyle.PooledWithSize(10, 50));

    container.Register(Component
    .For<IIngredient>()
    .ImplementedBy<SauceBéarnaise>()
    .LifeStyle.PerWebRequest);

    Add ‘<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequest-
    LifestyleModule, Castle.Windsor" />’ to the <httpModules> section on your web.config. If
    you’re running IIS7 in Integrated Mode you will need to add it to <modules> section under
    <system.webServer>

    Working with multiple components


    container.Register(Component

    .For<IIngredient>()
    .ImplementedBy<Steak>()
    .Named("meat"));
    container.Register(Component
    .For<IIngredient>()
    .ImplementedBy<SauceBéarnaise>()
    .Named("sauce"));

    You can give each registration a unique name that can later be used to distinguish this
    particular component from other components.
    Given the named components in listing 10.5, you can resolve both Steak and
    SauceBéarnaise like this:

    var meat = container.Resolve<IIngredient>("meat");
    var sauce = container.Resolve<IIngredient>("sauce");


    <configuration>

    <include uri="uri1" />
    <include uri="uri2" />

    <properties>
    <connection_string>value here</connection_string>
    </properties>

    <facilities>

    <facility id="" type="Facility full type name">

    </facility>

    </facilities>

    <components>

    <component
    id=""
    service=""
    type=""
    inspectionBehavior="all|declaredonly|none"
    lifestyle="singleton|thread|transient|pooled|custom"
    customLifestyleType="type that implements ILifestyleManager"
    initialPoolSize="&lt;num>" maxPoolSize="<num>">

    <parameters>
    <paramtername>value</paramtername>
    </parameters>

    <interceptors>
    <interceptor>${interceptor.id}</interceptor>
    </interceptors>

    </component>

    </components>

    </configuration>

  • 相关阅读:
    Ubuntu速配指南之热门设置
    最高境地的Linux桌面
    菜鸟在Linux零碎中安置Oracle 11G
    Ubuntu 7.10疾速设置指南
    excel的单元格怎么实现下拉菜单?
    Delphi 与 DirectX 之 DelphiX(16): DXImageList1.Items.Find();
    Delphi 与 DirectX 之 DelphiX(19): 绘图表面(TDirectDrawSurface)如何加载图片
    Delphi 与 DirectX 之 DelphiX(18): TDXDraw 中描绘图片的命令
    Delphi 与 DirectX 之 DelphiX(17): TPictureCollectionItem.PatternWidth、PatternHeight
    Delphi 与 DirectX 之 DelphiX(23): TDirectDrawSurface.Blur;
  • 原文地址:https://www.cnblogs.com/wucg/p/2391569.html
Copyright © 2011-2022 走看看