A simple synchronous component

To become acquaintant with the Application Space (AppSpace), let´s start with something very familiar to you: dependency injection (DI). Define a synchronous component by separating its contract (interface) from its implementation (class) and map the contract to the implementation using a DI container.

The Xcoordination AppSpace comes with Microsoft´s Unity DI container built in. It was not chosen because of any specific capability, but just because it was a good enough DI container and promised to play well with other Microsoft technologies.

General usage of the Application Space

Since an AppSpace owns resources, it implements IDisposable. It is recommended to use it like this or call Dispose() explicitly:

using XcoAppSpaces.Core;
...
using(XcoAppSpace space = new XcoAppSpace())
{
    ...
}

Just link to the xcoappspaces.dll assembly as well as the Microsoft CCR Core assembly and import the XcoAppSpaces.Core namespace.

Dependency injection as usual

Once you have set up "a space" you can use it like Unity, e.g.

interface IService {...}
...
class MyService : IService {...}
...
space.Register<IService, MyService>();
...
IService s = space.Resolve<IService>();

The AppSpace DI API resembles Unity´s with Register<>() and Resolve<>() to register mappings and to access registered types/instances. Of course you can also assign a name to a mapping:

space.Register<IService, MyService>("foo");
...
IService s = space.Resolve<IService>("foo");

If those "standard methods" are not enough to suite your application´s needs, you can always get access to an AppSpace´s DI container instance:

using Microsoft.Practices.Unity;
...
IUnityContainer uc = space.DefaultDIContainer;

Then do whatever Unity allows you to do. Of course for this you need to reference the Microsoft.Practices.Unity.dll as well which is included in the AppSpace binary download.

Or, if need be, do it the other way around: prime an AppSpace with your IUnityContainer instance:

IUnityContainer uc = new UnityContainer();
...
XcoAppSpace space = XcoAppSpace.UsingDIContainer(uc);

For this and other scenario the AppSpace provides a fluent interface.

Injecting the Application Space

The Application Space registeres itself with its DI container instance so you can get it injected into your implementation types upon instanciation:

class MyService : IService
{
    ...
    public MyService(XcoAppSpace space)
    { ... }
    ...
}
...
space.Register<IService, MyService>();
...
IService s = space.Resolve<IService>(); // space gets injected into the IService instance via ctor injection

Other types of injection are of course also possible. See the Unity reference for examples.

Summary

To get your feet wet with the Application Space just replace your current DI container with it. Set up your type mappings with Microsoft Unity and let it stitch together your synchronous component architecture at runtime. Once you´re comfortable with this, move on to more insteresting stuff like asynchronous components...

Last edited May 12, 2009 at 8:29 AM by ralfw, version 8

Comments

No comments yet.