di 使用温莎城堡的问题
我正在尝试使用温莎学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定,我可能是错的,但我没有注意到你打电话
如果你使用过 ASP.NET MVC,你可以使用 控制器注入,但您仍然需要将接口挂钩到某种工厂方法。在你的样本中我会添加
Not sure and I might be wrong, but I didn't notice you call
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
你调用了 install() 吗?例如:
did you call install()? for instance: