【发布时间】:2010-12-29 01:06:57
【问题描述】:
我有一个表单,它在一个组合框中显示三个项目。 大陆、国家和城市
如果我选择一个项目,例如如果我选择城市,然后如果我点击“获取结果”按钮,我会通过业务和数据层向数据库发送一个选择命令,然后检索类型城市的列表。
然后列表绑定到 UI 表单上的网格。
类:Continents、Countrys 和 Cities 使用属性字符串“Name”实现 IEntities 接口。
按钮点击事件调用业务层使用:
click(object sender, EventArgs e)
{
string selectedItem = comboBox.SelectedItem;
IEntities entity = null;
List<IEntities> list = null;
if (selectedItem == "Cities")
{
entity = new Cities("City");
}
if (selectedItem == "Continents")
{
entity = new Continents("Continents");
}
if (selectedItem == "Countries")
{
entity = new Countries("Countries");
}
//Then I call a method in Business Layer to return list
BL bl = new BL(entity);
list = bl.GetItems();
myDataGrid.DataContext = list;//to bind grid to the list
}
业务层如下所示:
public class BL
{
public IEntities _entity;
//constructor sets the variable
public BL(IEntity entity)
{
_entity = entity;
}
public IList<Entities> GetItems()
{
//call a method in data layer that communicates to the database
DL dl = new DL();
return dl.CreateItemsFromDatabase(_entity.Name);//name decides which method to call
}
}
我想将 Unity 用作 IOC,因此我不想在按钮单击事件中使用工厂(某种)模式以及 if then elses 并使用硬编码的类名,而是使用容器的配置来创建相关的类实例。而当 IEntities 实例传递给 BL 类的构造函数时,我想使用 Unity 传递对象。可以请教一下怎么做吗?
【问题讨论】:
标签: c# wpf unity-container