Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. przestrzeń nazw SoniqCalendar.Services
  2. {
  3.     ClientManager klasy publicznej
  4.     {
  5.         klasa publiczna OptionsClientApplication
  6.         {
  7.             ciąg publiczny Nazwa {get; zestaw; }
  8.             ciąg publiczny ApiKey {get; zestaw; }
  9.             ciąg publiczny DatabaseProvider {get; zestaw; }
  10.             ciąg publiczny DatabaseConnectionString {get; zestaw; }
  11.         }
  12.  
  13.         Opcje klasy publicznej
  14.         {
  15.             ciąg publiczny SettingsDir {get; zestaw; }
  16.             public OptionsClientApplication [] ClientApplications {get; zestaw; }
  17.         }
  18.  
  19.         prywatne tylko do odczytu Opcje _opcje;
  20.  
  21.         prywatny tylko do odczytu List <ClientApplication> _clients = new List <ClientApplication> ();
  22.  
  23.         prywatny słownik tylko do odczytu <ciąg, ClientApplication> _clientApiKeyLookup =
  24.             nowy słownik <ciąg, ClientApplication> ();
  25.  
  26.         prywatny tylko do odczytu JsonSerializerOptions _jsonSerializerOptions;
  27.  
  28.         public IEnumerable <ClientApplication> Clients => _clients;
  29.  
  30.         public ClientManager (opcje IOptions <Opcje>)
  31.         {
  32.             _options = opcje. Wartość;
  33.             _jsonSerializerOptions = new JsonSerializerOptions ()
  34.             {
  35.                 WriteIndented = true,
  36.                 Konwertery =
  37.                 {
  38.                     nowy TimeSpanJsonConverter (),
  39.                 }
  40.             };
  41.         }
  42.  
  43.         /// <podsumowanie>
  44.         /// Rejestruje aplikacje klienckie i inicjuje ich bazy danych.
  45.         /// Musi zostać wywołany przed użyciem.
  46.         /// </summary>
  47.         public async Task Initialize ()
  48.         {
  49.             if (! Directory.Exists (_options.SettingsDir))
  50.                 Directory.CreateDirectory (_options.SettingsDir);
  51.  
  52.             foreach (klient var w _options.ClientApplications)
  53.                 czekają na AddClientFromConfig (klient);
  54.         }
  55.  
  56.         private async Zadanie AddClientFromConfig (OptionsClientApplication optionClient)
  57.         {
  58.             var app = new ClientApplication ()
  59.             {
  60.                 Nazwa = optionClient.Name,
  61.                 ApiKey = optionClient.ApiKey,
  62.                 DbOptions = BuildDbOptions (optionClient.DatabaseProvider, optionClient.DatabaseConnectionString),
  63.                 Settings = LoadSettings (optionClient.Name),
  64.                 Logger = CreateLogger (),
  65.             };
  66.  
  67.             czekaj na użycie (var context = new AppContext (app.DbOptions.Options))
  68.                 czekają na kontekst.Database.EnsureCreatedAsync ();
  69.  
  70.             _clients.Add (aplikacja);
  71.             _clientApiKeyLookup.Add (app.ApiKey, aplikacja);
  72.         }
  73.  
  74.         prywatny ILogger CreateLogger ()
  75.         {
  76.             var logger = new LoggerConfiguration ()
  77.                 .Enrich.FromLogContext ()
  78.                 .MinimumLevel.Debug ()
  79.                 .WriteTo.ColoredConsole (
  80.                     LogEventLevel.Verbose)
  81.                 .CreateLogger ();
  82.         }
  83.  
  84.         public async Task SeedDatabases ()
  85.         {
  86.             foreach (klient var w _clients)
  87.             {
  88.                 czekaj na użycie var context = new AppContext (client.DbOptions.Options);
  89.                 var seeder = new TestDbSeeder (kontekst);
  90.                 czekaj na seeder.Seed ();
  91.             }
  92.         }
  93.  
  94.         /// <podsumowanie>
  95.         /// Zwraca <patrz cref = "ClientApplication" /> przez dany klucz interfejsu API.
  96.         /// </summary>
  97.         /// <wyjątek cref = "InvalidApiKeyException"> Zgłasza, gdy nie ma klienta z takim kluczem API. </exception>
  98.         public ClientApplication GetByApiKey (ciąg apiKey)
  99.         {
  100.             if (! _clientApiKeyLookup.ContainsKey (apiKey))
  101.                 wrzuć nowy InvalidApiKeyException ();
  102.  
  103.             return _clientApiKeyLookup [apiKey];
  104.         }
  105.  
  106.         prywatny statyczny DbCon
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement