12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using FISSDKDemoV2.Database.IRespository;
- using System;
- using System.Collections.Generic;
- namespace FISSDKDemoV2.Manager
- {
- internal class DataBaseManager
- {
- private static Dictionary<Type, IDatabaseService> _services = new Dictionary<Type, IDatabaseService>();
- /// <summary>
- /// Register a service into the container.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="service"></param>
- /// <exception cref="InvalidOperationException"></exception>
- public static void RegisterService<T>(IDatabaseService service)
- {
- var type = typeof(T);
- if (_services.ContainsKey(type))
- {
- throw new InvalidOperationException($"Key:{type.Name} already exists.");
- }
- _services.Add(type, service);
- }
- /// <summary>
- /// Get the service from the container.
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <returns></returns>
- /// <exception cref="KeyNotFoundException"></exception>
- public static T GetService<T>()
- {
- var type = typeof(T);
- if (_services.ContainsKey(type))
- {
- return (T)_services[type];
- }
- throw new KeyNotFoundException($"Can not find the key:{type.Name}");
- }
- }
- }
|