2010-06-17 6 views
2

합성 부분에서 사용 수출 및 수입에 충족 부분 : 나는 알림 서비스를MEF : 나는 실버 라이트 4에서 다음과 같은 시나리오 한

발췌문

[InheritedExport] 
public interface INotificationsService : IObservable<ReceivedNotification> 
{ 
    void IssueNotifications(IEnumerable<ClientIssuedNotification> notifications); 
} 

및 및 구현 이 서비스 중 스 니펫

[PartCreationPolicy(CreationPolicy.NonShared)] 
public class ClientNotificationService : INotificationsService 
{ 
    [Import] 
    IPlugin Plugin { get; set; } 
    ... 
} 

ClientNotificationService의 Plugin 속성이 INotificationsService를 가져 오는 가져 오기 클래스에 의해 제공되어야한다고 MEF에게 말할 수 있습니까? 예를 들어

:

발췌문

public class Client 
{ 
    [Export] 
    IPlugin Current { get; set; } 

    [Import] 
    INotificationService NotificationService; 
} 

어떻게 내가 MEF는 클라이언트 클래스에서 보낸 IPlugin와 ClientNotificationService.Plugin 부분을 만족한다고 말할 수있다.

기본적으로 나는 새 클래스 인, 으로 작성되거나 작성 될 때마다 가져 오기 클래스에서 제공하는 고유 ID를 수신하기 위해 NotificationService를 원합니다. 메타 데이터를 사용하여 어떤 통찰력을 주셔서 감사합니다. 나는 이걸로 고생하고있다.

감사

당신은 당신이 당신의 플러그인에 액세스 할 수 있도록하는 위임, 예를 내보낼 수

답변

1

가 :

기본적으로
public class Client 
{ 
    [Export("PluginDelegate")] 
    IPlugin GetPlugin() 
    { 
     return new SamplePlugin(); 
    } 

    [Import] 
    public INotificationService NotificationService { get; set; } 
} 

[PartCreationPolicy(CreationPolicy.NonShared)] 
public class ClientNotificationService : INotificationService 
{ 
    [Import("PluginDelegate")] Func<IPlugin> PluginDelegate; 
} 
2

나는 NotificationService을 원 의해 제공 고유 ID를 수신하기 클래스를 가져올 때마다 가져오고 새 클래스로 구성된

,

당신은 INotificationsService 계약에 ID (그리고 초기화 할 필요가 있다는 사실을) 추가 할 수 있습니다

public interface INotificationsService : IObservable<ReceivedNotification> 
{ 
    /// <summary> 
    /// Gets or sets the ID for this notification service. May only be set once. 
    /// </summary> 
    /// <exception cref="InvalidOperationException"> 
    /// The setter was called more than once, or the getter was called before the 
    /// ID was initialized. 
    /// </exception> 
    string ID { get; set; } 

    void IssueNotifications(IEnumerable<ClientIssuedNotification> notifications); 
} 

을 반입은 다음과 같을 수 있습니다

public class Client 
{ 
    private readonly INotificationsService _notificationsService; 

    [Import(typeof(INotificationService), 
     RequiredCreationPolicy = CreationPolicy.NonShared)] 
    public INotificationsService NotificationsService 
    { 
     get 
     { 
      return _notificationsService; 
     } 
     set 
     { 
      _notificationsService = value; 
      _notificationsService.ID = "SomeID"; 
     } 
    } 
} 

또 다른 옵션은 가져 오는 것입니다 ID 파라미터를 받아들이는 팩토리 :

public interface INotificationsServiceFactory 
{ 
    INotificationsService Create(string ID); 
} 

두 가지 접근법에는 각각 다른 장단점이 있습니다. 예를 들어 import-on-import 방식은 간단하지만 구성 요소 수명에 추가 단계 ("생성되었지만 아직 초기화되지 않음")가 있습니다.

공장 접근 방식에서는이 문제를 피할 수 있지만 하나의 인스턴스 만 필요로한다는 사실을 모호하게 만듭니다. 청소가 필요할 경우, 공장 접근법은 또한 컨테이너에서 공장 고객에게 물건을 버리는 책임을 이동시킵니다.

또 다른 옵션은 MEF에서 다른 IoC 컨테이너로 전환하는 것입니다.이 컨테이너는 Castle Windsor와 같은 구성 요소 등록 및 종속성 해결에 대해보다 세부적인 제어를 제공합니다. 그렇다면 당연히 구성을 유지해야하므로 고통이 될 수 있습니다.

+0

답변을 제공해 주셔서 감사합니다.하지만 실제로 필요한 것은 '범위 지정'솔루션의 일부 왕이며 자동으로 그러한 문제를 해결할 수있는 솔루션입니다. 이러한 문제를 어떻게 해결할 수 있는지 더 많이 조사해야합니다. –

관련 문제