私はコンボボックスに3つの項目を示すフォームを持っています。 大陸、国および都市UnityをIoCとして使用するコンストラクタ依存性注入
アイテムを選択すると、都市を選択し、「結果を取得」ボタンをクリックすると、ビジネスとデータレイヤーを介してデータベースに選択コマンドを送信し、タイプの都市のリストを取得します。
リストはUIフォームのグリッドにバインドされます。
クラス:大陸、国および都市は、プロパティ文字列「名前」を持つ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
}
}
私が代わりに工場(の一種)を使用するので、IOCとしてユニティを使用したい
ボタンクリックイベントは、使用して、ビジネスレイヤを呼び出しますパターンがボタンのクリックイベントでelsesとハードコードされたクラス名を使用して、私は関連するクラスインスタンスを作成するコンテナの設定を使用したいと思います。 IEntitiesインスタンスがBLクラスのコンストラクタに渡されるとき、私はUnityを使ってオブジェクトを渡したいと思います。あなたはそれをする方法をアドバイスできますか?