Dashboard > MicroKernel/Windsor > Home > Factory Support Facility
Log In   View a printable version of the current page.
Factory Support Facility
Added by Krzysztof Kozmic, last edited by Krzysztof Kozmic on Jan 22, 2010  (view change)
Labels: 
(None)


Introduction

Factory Support Facility allows using factories to create components. This is beneficial when you want to make available as services components that do not have accessible constructor, or that you don't instantiate, like HttpContext.

Using factories from code

Recommended approach is to use UsingFactoryMethod method in fluent API to create components.

See Fluent Registration API documentation for details on how to use it.

Version 2.1 is the last one where UsingFactoryMethod method uses Factory Support Facility. Later versions (current trunk) do not depend on the facility. This limits the usefulness of the facility to XML-driven and legacy scenarios.

Using factories from configuration

Blog post by David Hayden about the facility (Dec 13, 2007)

In addition to code, the facility uses XML configuration.

Registering facility in XML

You can register the facility in the standard facilities section of Windsor's config:

Just install the facility and add the proper configuration.

<configuration>
	<facilities>
		<facility
			id="factory.support"
			type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.MicroKernel" />
	</facilities>
</configuration>

Configuration Schema

Broadly speaking facility exposes the following scheme, with two kinds of supported factories: accessors and methods

<components>
    <component id="mycomp1" instance-accessor="Static accessor name" />
    <component id="factory1" />
    <component id="mycomp2" factoryId="factory1" factoryCreate="Create" />
</components>

Accessor example

Given the following singleton class:

public class SingletonWithAccessor
{
    private static readonly SingletonWithAccessor instance = new SingletonWithAccessor();

    private SingletonWithAccessor()
    {
    }

    public static SingletonWithAccessor Instance
    {
        get { return instance; }
    }
}

You may expose its instance to the container through the following configuration:

<components>
	<component id="mycomp1"
	type="Company.Components.SingletonWithAccessor, Company.Components"
	instance-accessor="Instance" />
</components>

Using it:

SingletonWithAccessor comp = (SingletonWithAccessor) container["mycomp1"];

Factory example

Given the following component and factory classes:

public class MyComp
{
    internal MyComp()
    {
    }

    ...
}

public class MyCompFactory
{
    public MyComp Create()
    {
    return new MyComp();
    }
}

You may expose its instance to the container through the following configuration:

 <components>
    <component id="mycompfactory"
        type="Company.Components.MyCompFactory, Company.Components"/>

    <component id="mycomp"
        type="Company.Components.MyComp, Company.Components"
        factoryId="mycompfactory" factoryCreate="Create" />
</components>

Using it:

MyComp comp = (MyComp) container["mycomp"];

Factory with parameters example

Given the following component and factory classes:

public class MyComp
{
    internal MyComp(String storeName, IDictionary props)
    {
    }

    ...
}

public class MyCompFactory
{
    public MyComp Create(String storeName, IDictionary props)
    {
        return new MyComp(storeName, props);
    }
}

You may expose its instance to the container through the following configuration:

<components>
    <component id="mycompfactory"
        type="Company.Components.MyCompFactory, Company.Components"/>

    <component id="mycomp"
        type="Company.Components.MyComp, Company.Components"
        factoryId="mycompfactory" factoryCreate="Create">

        <parameters>
            <storeName>MyStore</storeName>
            <props>
                <dictionary>
                    <entry key="key1">item1</entry>
                    <entry key="key2">item2</entry>
                </dictionary>
            </props>
        </parameters>
    </component>
</components>

Using it:

MyComp comp = (MyComp) container["mycomp"];

Factory using auto-wire example

If your factory request as parameter some other component instance, this facility will be able to resolve it without your aid:

public class MyComp
{
    internal MyComp(IMyService serv)
    {
    }

    ...
}

public class MyCompFactory
{
    public MyComp Create(IMyService service)
    {
        return new MyComp(service);
    }
}

You may expose its instance to the container through the following configuration:

 <facilities>
    <facility
        id="factorysupport"
        type="Castle.Facilities.FactorySupport.FactorySupportFacility, Castle.Facilities.FactorySupport"/>
</facilities>

<components>
    <component id="myservice"
        service="SomethingElse.IMyService"
        type="Company.Components.MyServiceImpl, Company.Components" />

    <component id="mycompfactory"
        type="Company.Components.MyCompFactory, Company.Components" />

    <component id="mycomp"
        type="Company.Components.MyComp, Company.Components"
        factoryId="mycompfactory" factoryCreate="Create" />
</components>

Using it:

MyComp comp = (MyComp) container["mycomp"];

External resources

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators