콤보 상자에 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으로 유니티를 사용하려면 (일종의) 버튼 클릭 이벤트에서 패턴을 선택하고 하드 코드 된 클래스 이름을 사용하면 관련 클래스 인스턴스를 만드는 컨테이너의 구성을 사용하고자합니다. IEntities 인스턴스가 BL 클래스의 생성자에 전달되면 Unity를 사용하여 객체를 전달하려고합니다. 어떻게 할 수 있는지 조언 해 주시겠습니까?