|
|||||
|
|||||
NanoContainer.NET
NanoContainer.NETSo what is NanoContainer? NanoContainer is a complimentary API for defining PicoContainers. The NanoContainer.NET (or Nano.NET for short) implementation differs slightly from what has been developed for the Java implementation. Nano.NET takes advantage of the System.CodeDom Library for .NET to dynamically compile Nano scripts. And the XML implementation dynamically generates C# code from the XML script defined. Comparison of NanoContainer for Java with NanoContainer.NETThis section is useful for those familiar with the Java implementation of NanoContainer. An example of the differences are defined below. NanoContainer for JavaNanoContainer for Java relies on creating an API that is similar in nature to that of PicoContainer but based solely on the use of Strings. From a highlevel view, a script is parsed and components are added to an instance of NanoContainer by its String representation (i.e. "foo.bar.HelloWorld"). NanoContainer.NETThe Nano.NET implementation does not require the use of a concrete NanoContainer class. Scripts are loaded and executed dynamically. There is no need for an intermediate step to parse each script into an instance of a NanoContainer. The result of running a NanoContainer script is an instance of PicoContainer.IPicoContainer. I want to see an example!So for the impatient lets take a quick look at how we would utilize Nano.NET. The following script registers an implementation (bar.Foo) under the key "hello". C# Script example using PicoContainer;
using PicoContainer.Defaults;
namespace WhateverNamespace
{
public class MyNanoScript
{
private IPicoContainer parent;
public IPicoContainer Parent
{
set { parent = value; }
}
public IMutablePicoContainer Compose()
{
DefaultPicoContainer p = new DefaultPicoContainer(parent);
p.RegisterComponentImplementation("hello", typeof(bar.Foo));
return p;
}
}
}
Now there are 2 important things to notice with the above example.
It is also possible to define your script as an implementation of the NanoContainer.Script.IScript Interface. This interface defines both the Parent property and the Compose method. Regardless of whether your script implements the IScript interface NanoContainer will treat your script it as an instance of IScript.
Now that we have a script defined we need to load the script so it can be used by NanoContainer.NET. The following example shows the necessary code needed to load the script and build the associated container Loading the script from within your .NET application // Load in the script Stream stream = ... // read in script StreamReader streamReader = new StreamReader(stream); // Use the correct ContainerBuilderFacade (the script is C#) ContainerBuilderFacade cbf = new CSharpContainerBuilderFacade(streamReader ); // Build reference to external assemblies FileInfo fooBarDll = new FileInfo("../FooBar.dll"); StringCollection assemblies = new StringCollection(); assemblies.Add(fooBarDll.FullName); // build the container IMutablePicoContainer container = cbf.Build(assemblies); What languages can be used for scripting?
NanoContainer.NET also supports .NET Attributes for defining contents of a container but then that really doesn't fit under the "Scripts" definition and therefor not listed as a scripting option. |
|||||
|
Copyright 2003-2006 - The Codehaus. All rights reserved unless otherwise noted.
Powered by Atlassian Confluence
|
|||||