Home > Development > Castle Windsor – Registering Existing Object Instances

Castle Windsor – Registering Existing Object Instances


Okay. Well we have not had a post on Castlw Windsor for a while so he we go. This is how to register an instance of an object you created in code against an interface. Basic you always get the object you passed in when you registered back. Sort of like a singleton. This can be useful if you have some extraordinary configuration to the object you need to do.

So here is come code showing it. You used the Instance Method to register your existing instance.

using System.Diagnostics;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

namespace CastleWindsorDemo
{
    public class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    public interface ICustomerRepository
    {
        void SaveCustomer(Customer customer);
        ISessionProvider Provider { get; }
    }

    public interface ISessionProvider
    {
        void StartSession();
        void CloseSession();
    }

    public class SessionProvider : ISessionProvider
    {
        public void StartSession()
        {
            
        }

        public void CloseSession()
        {
            
        }
    }

    public class CustomerRepository : ICustomerRepository
    {
        private readonly ISessionProvider provider;

        public CustomerRepository(ISessionProvider provider)
        {
            this.provider = provider;
        }

        public ISessionProvider Provider
        {
            get { return provider; }
        }

        public void SaveCustomer(Customer customer)
        {
            
        }
    }


    class Program
    {
        static void Main()
        {
            WindsorContainer container = new WindsorContainer();

            SessionProvider provider = new SessionProvider();
            container.Register(
                    Component.For<ISessionProvider>()
                             .Instance(provider)
                );

            container.Register(
                    Component.For<ICustomerRepository>()
                             .ImplementedBy<CustomerRepository>()
                             .LifeStyle
                             .Transient
                );

            var resolvedTypeA = container.Resolve<ICustomerRepository>();
            var resolvedTypeB = container.Resolve<ICustomerRepository>();

            Debug.Assert(ReferenceEquals(resolvedTypeA.Provider,resolvedTypeB.Provider));

        }
    }
}

You can also see how I am asserting they should be the same object.

Hope this helps..

Blair….

Advertisements
Categories: Development Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: