di 使用温莎城堡的问题

发布于 2024-11-08 12:01:48 字数 2664 浏览 0 评论 0原文

我正在尝试使用温莎学习 di,但遇到了问题。我有一个正在使用的现有 DAL,我想将 di 与此 dal 一起使用。我有以下界面(为了这篇文章而简化) -

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

和实现 -

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);
        }
    }

这是我的温莎安装程序 -

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

这是我进行注入的地方 -

public class GetData
    {
        private IConnection conn;

        public GetData()
        {
        }

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

        public List<Entity> GetAllData()
        {
            List<Entity> data= 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;            
        }
    }

注入不起作用。如果我单步执行代码,则 GetAllData 中的 conn 始终为 null。 当我逐步执行安装程序时,IConnection/Connection 依赖项就在那里,它表示所有必需的依赖项都可以得到解决。

谁能看到我做错了什么吗?正如我所说,我是 di 的新手,所以如果我使用不正确,请告诉我。

谢谢

编辑 - 我不完全确定我理解了。如果我将安装程序更改为以下内容,它仍然无法工作 -

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

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

            container.Resolve<GetData>();

与您所说的相比,我是否偏离了基础,或者我是否朝着正确的方向前进?

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> data= 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟燃烟灭 2024-11-15 12:01:48

不确定,我可能是错的,但我没有注意到你打电话

container.Resolve

如果你使用过 ASP.NET MVC,你可以使用 控制器注入,但您仍然需要将接口挂钩到某种工厂方法。在你的样本中我会添加

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

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
    ...
}
南汐寒笙箫 2024-11-15 12:01:48

你调用了 install() 吗?例如:

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

did you call install()? for instance:

container.Install(Castle.Windsor.Installer.Configuration.FromAppConfig());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文