My favorites | Sign in
Project Home Downloads Wiki Issues Source
Search
for
LinFuIOCQuickstart  
Shows the basics of how to get LinFu IoC 2.0 up and running in a few lines of code.
Featured
Updated Feb 4, 2010 by Philip.L...@gmail.com

Introduction

LinFu's IoC container allows you to automatically inject services into a ServiceContainer instance using a single ImplementsAttribute declaration declared on a target class. For example, let's suppose that I have a service type named IVehicle that I want exposed to the container:

    public interface IVehicle
    {
        void Move();
        void Park();
        IPerson Driver { get; set; }
        IEngine Engine { get; set; }
    }

...and let's further suppose that I wanted to expose the Car class to the container so that it can implement the IVehicle interface. Here's the Car class declaration:

    [Implements(typeof(IVehicle), LifecycleType.OncePerRequest)]
    public class Car : IVehicle, IInitialize
    {
        private IEngine _engine;
        private IPerson _person;

        public IEngine Engine
        {
            get { return _engine; }
            set { _engine = value; }
        }
        public IPerson Driver
        {
            get { return _person; }
            set { _person = value; }
        }
        public void Move()
        {
            if (_engine == null || _person == null)
                return;

            _engine.Start();
            Console.WriteLine("{0} says: I’m moving!", _person.Name);
        }
        public void Park()
        {
            if (_engine == null || _person == null)
                return;

            _engine.Stop();
            Console.WriteLine("{0} says: I’m parked!", _person.Name);
        }


        public void Initialize(IServiceContainer container)
        {
            _engine = container.GetService<IEngine>();
            _person = container.GetService<IPerson>();
        }
    }

As you can see, the Car class implementation is relatively straightforward, aside from the ImplementsAttribute declaration, and the IInitialize implementation. The ImplementsAttribute declaration tells LinFu's IoC container loader to use the Car class to implement the IVehicle interface, and it also tells the loader to create a brand new Car instance every time the IVehicle service type is requested.

Service Initialization

The Initialize() method implementation of the IInitialize interface allows us to initialize the Car class with the respective IEngine and IPerson services that currently reside in the container. This feature allows us to inject properties into our own services without having to worry about the implementation details of the container instance itself.

Service Lifecycle Types

In general, LinFu's IoC container supports the following lifecycle types:

  • OncePerRequest: A new instance is created on every service request.
  • OncePerThread: Only one service instance is created per thread.
  • Singleton: One and only one service instance is created regardless of the number of new requests.

Loading Services into the Container

Assuming that the Car class is located in an assembly named CarLibrary.dll, all we need to do to load the Car implementation of the IVehicle interface into the container is:

    string directory = AppDomain.CurrentDomain.BaseDirectory;
    var container = new ServiceContainer();

    // Load CarLibrary.dll; If you need load
    // all the libaries in a directory, use "*.dll" instead
    container.LoadFrom(directory, "CarLibrary.dll");

The container will automatically load the Car implementation from CarLibrary.dll into the container and manage the lifetime of each service. The only thing left for us to do at this point is to use the actual IVehicle service itself:

    // Use the car instance
    IVehicle vehicle = container.GetService<IVehicle>();

    // Do something useful with the vehicle here
    vehicle.Move();

...and that's all you need to get your services up and running. The container will resolve the dependencies for you, leaving you to do more useful tasks.


Sign in to add a comment
Powered by Google Project Hosting