Как я могу зарегистрировать сервисы в Kephas, используя свободный API? - PullRequest
1 голос
/ 21 марта 2019

Я хочу сохранить уровень абстракции DI, предоставляемый Kephas, но в моем конкретном случае мне нужно зарегистрировать сервис, который импортируется из сторонней библиотеки. Учитывая это, я не могу аннотировать сервис с атрибутом [AppServiceContract], необходимым для регистрации сервиса. Есть ли способ добиться этого?

1 Ответ

1 голос
/ 21 марта 2019

Да, есть.Вот следующие шаги:

  • Определите класс, реализующий IConventionsRegistrar.
  • Используйте конструктор соглашений для регистрации желаемых служб.

Примерcode:

public class MyConventionsRegistrar : IConventionsRegistrar
{
    /// <summary>
    /// Registers the conventions.
    /// </summary>
    /// <param name="builder">The registration builder.</param>
    /// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
    /// <param name="registrationContext">Context for the registration.</param>
    public void RegisterConventions(
        IConventionsBuilder builder,
        IEnumerable<TypeInfo> candidateTypes,
        ICompositionRegistrationContext registrationContext)
    {
        //... here you can use the conventions builder to register your services using the fluent API. The candidate types are provided if you need the  identified application types. A couple of examples are provided below:

        // shared/singleton service exported as IMyService with MyService implementation.
        builder.ForType(typeof(MyServiceImpl))
               .Export(b => b.AsContractType(typeof(IMyService)))
               .Shared();

        // instance-based multiple services exported as IMultiImplService
        builder.ForTypesDerivedFrom(typeof(IMultiImplService))
               .Export(b => b.AsContractType(typeof(IMultiImplService)));
    }

Существует второй способ регистрации сервисов, но с помощью дескрипторов сервисов вместо свободного API.В этом случае выполните следующие действия:

  • Определите класс, реализующий IAppServiceInfoProvider.
  • Возвращает дескрипторы нужных служб.

Пример кодас теми же регистрациями, что и выше:

public class MyAppServiceInfoProvider : IAppServiceInfoProvider
{
    /// <summary>
    /// Gets an enumeration of application service information objects.
    /// </summary>
    /// <param name="candidateTypes">The candidate types which can take part in the composition.</param>
    /// <param name="registrationContext">Context for the registration.</param>
    /// <returns>
    /// An enumeration of application service information objects and their associated contract type.
    /// </returns>
    public IEnumerable<(TypeInfo contractType, IAppServiceInfo appServiceInfo)> GetAppServiceInfos(IEnumerable<TypeInfo> candidateTypes, ICompositionRegistrationContext registrationContext)
    {
        yield return (typeof(IMyService).GetTypeInfo(), 
                      new AppServiceInfo(typeof(IMyService), 
                                         typeof(MyServiceImpl),
                                         AppServiceLifetime.Shared));
        yield return (typeof(IMultiImplService).GetTypeInfo(), 
                      new AppServiceInfo(typeof(IMultiImplService), 
                                         AppServiceLifetime.Instance, 
                                         allowMultiple: true));
    }

Оба случая автоматически обнаруживаются, и в надлежащее время вызываются методы регистрации.Однако во втором случае преимущество заключается в том, что регистрации доступны как метаданные службы для запроса в более позднее время.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...