开发者

problems with di using castle windsor

I am trying to learn di using windsor and am running into problems. I have an existing DAL that I am using and I would like to use di with this dal. I have the following interface (simplified for the sake of this post) -

public interface IConnection 
    {
        void OpenConnection(string ConnectionStringName);
        void CloseConnection();
        DbDataReader ExecuteReader(string query);
    }

and the implementation -

public class Connection : IConnection
    {
        private DBManager manager = new DBManager();

        public void OpenConnection(string ConnectionStringName)
        {
            manager.OpenConnection("connectionstringname");
        }

        public void CloseConnection()
        {
            manager.CloseConnection();
        }

        public DbDataReader ExecuteReader(string query)
        {
            return manager.ExecuteReader(query, CommandType.Text);
        }
    }

Here is my windsor installer -

public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            container.Register(
                Component.For<IConnection>().ImplementedBy<Connection>()
                .LifeStyle.Transient
                );
        }

and here is where I am doing the injection -

public class GetData
    {
        private IConnection conn;

        public GetData()
        {
        }

        public GetData(IConnection conn)
        {
            this.conn = conn;
        }

        public List<Entity> GetAllData()
        {
            List<Entity> d开发者_运维问答ata= new List<Entity>();
            //IConnection conn = new Connection();
            conn.OpenConnection("connectionstringname");

            try
            {
                var r = conn.ExecuteReader("select ... from ...");
                //code to convert reader to data list
                r.Dispose();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.CloseConnection();
            }

            return data;            
        }
    }

the injection is not working. If I step through the code, conn is always null in GetAllData. when I step through at the installer, the IConnection/Connection dependency is there and it says that all required dependencies can be resolved.

Can anyone see anything that I am doing wrong? As I said, I am new to di, so if I am using it incorrectly, please let me know.

thanks

EDIT - I'm not entirely sure I understand. If I change my installer to the following, it still isn't working -

container.Register(
                Component.For<IConnection>().ImplementedBy<Connection>()
                .LifeStyle.Transient
                );

            container.Register(
                Component.For<GetData>()
                );

            container.Resolve<GetData>();

Am I way off base compared to what you said, or am I heading in the right direction?


Not sure and I might be wrong, but I didn't notice you call

container.Resolve

If you have worked with ASP.NET MVC, you can get this working automatically with controller injection, but you will still need to hook the interface to some kind of a factory method. In your sample I would add

public class GetData
{
    private IConnection conn = ContainerManager.Instance.Resolve<IConnection>();
    //where ContainerManager.Instance points to the container instance
    ...
}


did you call install()? for instance:

container.Install(Castle.Windsor.Installer.Configuration.FromAppConfig());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜