다음을 통해 공유


컨테이너, 사이트 및 구성 요소

컨테이너는 한 개 이상의 구성 요소를 논리적으로 포함하는 특수한 컬렉션 개체입니다. 컨테이너는 상호 작용이 발생할 수 있는 ISite 인터페이스 구현을 제공하여 구성 요소와 외부 응용 프로그램 환경은 물론 구성 요소 간의 상호 작용을 관리합니다. 컨테이너를 사용하면 선입선출 방식으로 구성 요소를 추적할 수 있고 인덱스로 구성 요소를 참조할 수 있습니다. 또한 컨테이너는 구성 요소가 더 이상 필요 없게 된 경우의 일반적인 처리 방법을 제공합니다.

포함은 시각적이나 물리적인 포함이 아니라 논리적인 포함을 의미합니다. 컨테이너는 한 개 이상의 구성 요소를 캡슐화하고 클라이언트와 상호 작용할 수 있는 래퍼를 제공합니다. 컨테이너를 사용하면 다음과 같은 구문을 통해 구성 요소를 추가하거나 제거할 수 있습니다.

Imports System.ComponentModel
Dim myComponent As New Component()
Dim myContainer As New Container()
myContainer.Add(myComponent)
myContainer.Remove(myComponent)
using System.ComponentModel;
Component myComponent = new Component();
Container myContainer = new Container();
myContainer.Add(myComponent);
myContainer.Remove(myComponent);

구성 요소 컨테이너는 Container 클래스의 인스턴스 또는 IContainer 인터페이스의 구현입니다. Container는 이 인터페이스의 참조 구현입니다.

구성 요소 이름 지정

컨테이너 안에 있는 구성 요소의 이름을 지정할 수도 있습니다. 이 이름은 컨테이너 안에서 고유해야 하며 Add 메서드를 사용하여 지정합니다.

Dim myComponent As New Component()
Dim myContainer As New Container()
MyContainer.Add(myComponent, "ThisComponent")
Component myComponent = new Component();
Container myContainer = new Container();
myContainer.Add(myComponent, "ThisComponent");

리소스 관리 및 컨테이너 확장

컨테이너는 구성 요소와 연관된 리소스를 관리하는 핵심적인 방법을 제공합니다. Dispose 메서드가 호출되면 컨테이너에서 포함된 모든 구성 요소의 Dispose 메서드를 자동으로 호출하여 리소스가 즉시 해제될 수 있도록 합니다.

컨테이너는 확장할 수 있습니다. 사용자 지정 기능을 통합한 Container에서 상속된 자신의 클래스를 만들 수 있습니다. 예를 들어, 다음 예제와 같이 컨테이너에 추가되는 구성 요소를 제어하는 규칙을 적용하는 컨테이너를 만들 수 있습니다.

Public Class MyContainer
   Inherits Container
   Public Overloads Overrides Sub Add(ByVal component As IComponent)
      ' Checks to see if the component is allowed to join this container.
      If TypeOf component Is Widget Then
         ' Calls the Add method of the base class, and adds the component.
         MyBase.Add(component)
      Else
         ' If the component is not allowed, an exception is thrown.
         Throw New NonWidgetException()
      End If
   End Sub
End Class
class MyContainer : Container
{
   public override void Add(IComponent component)
   {
      // Checks to see if the component is allowed to join this container.
      if (component is Widget)
      {
         base.Add(component);
      }
      else
      {
         throw new NonWidgetException();
      }
   }
}
class MyContainer extends Container
{
   public void Add(IComponent component) throws NonWidgetException
   {
      // Checks to see if the component is allowed to join this container.
      if (component instanceof Widget) 
      {
         super.Add(component);
      }
      else
      {
         throw new NonWidgetException() ;
      }
   }
}   

위의 예제에서는 컨테이너를 조인할 수 있는 구성 요소에 대한 규칙을 적용할 수 있는 새 컨테이너 클래스를 만듭니다. 지정된 클래스의 구성 요소가 아닌 경우(이 경우 Widget) 예외가 throw됩니다.

구성 요소가 컨테이너에 추가되면 컨테이너에서 구성 요소의 사이트를 만듭니다. 이것은 구성 요소의 Site 속성을 통해 노출되는 ISite 인터페이스의 구현입니다. 구성 요소와 호스트 컨테이너의 통신은 구성 요소의 Site 속성을 통해 수행됩니다. 이 속성은 구성 요소의 논리적 사이트를 나타내며 컨테이너에 의해 호스팅됩니다. 컨테이너에 없는 구성 요소는 Site 속성에 대해 null 참조를 반환합니다. Site 속성을 사용하면 ISite.Container 속성을 통해 컨테이너 인터페이스에 대한 참조를 얻거나 Component 속성을 통해 호스팅되는 구성 요소 인터페이스에 대한 참조를 얻을 수 있습니다.

Dim myComponent As New Component
Dim myContainer As New Container
myContainer.Add(myComponent)
Dim myIComponent as IComponent
Dim myIContainer as IContainer
myIComponent = myComponent.Site.Component
myIContainer = myComponent.Site.Container
' These two messages display True.
MessageBox.Show("Are the components equal? " & _
   myComponent.Equals(myIComponent).ToString)
MessageBox.Show("Are the containers equal? " & _
   myContainer.Equals(myIContainer).ToString)
Component myComponent = new Component();
Container myContainer = new Container();
myContainer.Add(myComponent);
IComponent myIComponent;
IContainer myIContainer;
myIComponent = myComponent.Site.Component;
myIContainer = myComponent.Site.Container;
MessageBox.Show("Are the components equal? " + 
   myComponent.Equals(myIComponent).ToString());
MessageBox.Show("Are the containers equal? " + 
   myContainer.Equals(myIContainer).ToString());
Component myComponent =  new Component();
Container myContainer =  new Container();
myContainer.Add(myComponent);
IComponent myIComponent;
IContainer myIContainer;
myIComponent = myComponent.get_Site().get_Component();
myIContainer = myComponent.get_Site().get_Container();
MessageBox.Show("Are the components equal? " 
   + System.Convert.ToString(myComponent.Equals(myIComponent)));
MessageBox.Show("Are the containers equal? " 
   + System.Convert.ToString(myContainer.Equals(myIContainer)));

이 두 가지 속성은 모두 개체 자체에 대한 참조가 아니라 이 개체와 연관된 인터페이스만 반환합니다. 또한 구성 요소에는 Container와 같은 인터페이스를 반환하는 Container 속성이 있습니다. 이 속성은 사이트를 통해 제공되며 바로 가기로 간주할 수 있습니다.

Add 메서드를 사용하여 구성 요소에 이름을 할당하면 Name 속성을 통해 이 이름을 검색할 수 있습니다. 컨테이너에 해당 컨테이너와 연관된 서비스 개체가 있는 경우 GetService 메서드를 통해 구성 요소에서 해당 개체에 대한 참조를 얻을 수 있습니다.

서비스 액세스

GetService 메서드를 통해 여러 가지 서비스에 액세스할 수 있습니다. 이러한 서비스는 구성 요소를 디자인 환경에 통합하는 다양한 기능을 제공합니다. 자세한 내용은 방법: 디자인 타임 서비스 액세스디자인 타임 아키텍처를 참조하십시오.

참고 항목

작업

방법: 구성 요소 컨테이너 만들기

방법: 구성 요소 컨테이너 확장

방법: 디자인 타임 서비스 액세스

개념

컨테이너와 구성 요소 간 통신

디자인 타임 아키텍처