|
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.DosingSystem.Service
- {
- public class ServerFactory
- {
- private volatile static ServerFactory _Instance;
- public static ServerFactory GetInstance => _Instance ?? (_Instance = new ServerFactory());
- private ServerFactory() { }
-
- ConcurrentDictionary<string, IAPIServer> servers = new ConcurrentDictionary<string, IAPIServer>();
-
- public void Set<T>(T server) where T : IAPIServer
- {
- string name = server.GetType().Name;
- if (!servers.ContainsKey(name))
- {
- servers.TryAdd(name, server);
- }
- }
-
- public IAPIServer Get()
- {
- if (servers.Count > 0)
- return servers.ElementAt(0).Value;
- return default;
- }
-
- public IAPIServer Get<TServer>(TServer server) where TServer : IAPIServer
- {
- var name = server.GetType().Name;
- if (servers.ContainsKey(name))
- return servers[name];
- return default;
- }
-
- }
- }
|