2016-08-11 3 views
2

新しい.netコアの世界に自己完結型ライブラリ/サービスを作りたいと思います。基本的に私はいくつかのSASS製品を持っています:どのようにして、サービスのDLLの依存関係を.netコアのそのレイヤーにバインドしますか?

ServiceProduct1:UI/Composistionルートレイヤーが知る必要のないリポジトリレイヤーを持っています。

ServiceProduct2:UI/Composistionルートレイヤーが知る必要がないリポジトリレイヤーを持っています。それにも電子メールサービスがあります。

これらの2つのserviceProductsは複数のアプリケーションで使用されますが、消費するアプリケーションはリポジトリに隠れたインタフェースをバインドすることを知っている必要があります。また、電子メールサービスはDependendy Injectionを使用しており、サービスによって使用されているにもかかわらず、使用アプリケーションでは にバインドされている必要があります。

public static IKernel LoadAssemblies(IKernel kernel) 
    { 
     var type = typeof(INinjectDependency); 
     var dependencies = AppDomain.CurrentDomain.GetAssemblies() 
      .SelectMany(x => x.GetMatchingTypesInAssembly(y => type.IsAssignableFrom(y) && y.IsClass)); 
     var assemblies = dependencies.Select(Assembly.GetAssembly).Distinct(); 
     kernel.Load(assemblies); 
     return kernel; 
    } 

その後、すべてのあなたの結合を行うだろう消費サービスに:以前私が結合するもののためのDLLを検索するために再帰を使用しているだろう。ネットコアへ

私はもうNinjectを使用していませんが、概念は同じです。現在、dllをビルドするためにスワップしない限り、この方法を使用することはできません。私は私のDLLを公開したくありません。

これを処理する別の方法はありますか?

答えて

1

具体的には、ASP.NET Coreの実装に特化した多くのドキュメントがありますが、なぜこれが混乱しているのか分かります。答えはむしろ単純です。サービスが完全実行可能ファイルである場合、つまりコンパイルすると、*.exeが生成されます。起動時には、メインエントリポイントの近くのどこかでサービスを結線する必要があります。あなたのサービスが*.dllの場合は、依存関係を結び付けて、サービスコレクションを手渡してIServiceProviderを構築できるホストアプリケーション(実行可能ファイル)が必要です。

ここにはDependency Injection with .NET Coreという素晴らしい記事があります。 、いくつかの標準的な命名規則がここにあります

public class Host 
{ 
    public static void Main() 
    { 
     IServiceCollection serviceCollection = new ServiceCollection(); 
     ConfigureServices(serviceCollection); 

     var application = new Application(serviceCollection); 

     // Run 
     // ... 
    } 

    static void ConfigureServices(
     IServiceCollection serviceCollection) 
    { 
     ILoggerFactory loggerFactory = new Logging.LoggerFactory(); 
     serviceCollection.AddInstance<ILoggerFactory>(loggerFactory); 
    } 
} 

ConfigureServicesに気づく:ここでは、これを実現する方法の例です。その後Applicationオブジェクトは、このような次のように定義されます

public class PaymentService: IPaymentService 
{ 
    public ILogger Logger { get; } 

    public PaymentService(ILoggerFactory loggerFactory) 
    { 
     Logger = loggerFactory?.CreateLogger<PaymentService>(); 
     if (Logger == null) 
     { 
      throw new ArgumentNullException(nameof(loggerFactory)); 
     } 
     Logger.LogInformation("PaymentService created"); 
    } 
} 

この:

public class Application 
{ 
    public IServiceProvider Services { get; set; } 
    public ILogger Logger { get; set; } 

    public Application(IServiceCollection serviceCollection) 
    { 
     ConfigureServices(serviceCollection); 

     // The service-provider is not built until all services are configured. 
     Services = serviceCollection.BuildServiceProvider(); 

     Logger = 
      Services.GetRequiredService<ILoggerFactory>() 
        .CreateLogger<Application>(); 

     Logger.LogInformation("Application created successfully."); 
    } 

    public void MakePayment(PaymentDetails paymentDetails) 
    { 
     Logger.LogInformation(
      $"Begin making a payment { paymentDetails }"); 
     IPaymentService paymentService = 
      Services.GetRequiredService<IPaymentService>(); 
     // ... 
    } 

    void ConfigureServices(IServiceCollection serviceCollection) 
    { 
     serviceCollection.AddSingleton<IPaymentService, PaymentService>(); 
    } 
} 

現在のインタフェースと決済サービスの対応する実装は次のようになりますことを想像することができます必ずしもASP.NET Coreアプリケーションである必要はありません。

関連する問題