using System; using System.Collections.Generic; namespace Svelto.ServiceLayer { public abstract class ServiceRequestsFactory : IServiceRequestsFactory { public RequestInterface Create() where RequestInterface : class, IServiceRequest { var ret = RetrieveObjectType(); return ret.CreateInstance() as RequestInterface; } protected void AddRelation() where RequestClass : class, RequestInterface, new() where RequestInterface : IServiceRequest { _requestMap[typeof(RequestInterface)] = new Value(); } IHoldValue RetrieveObjectType() { if (_requestMap.ContainsKey(typeof(RequestInterface)) == false) throw new ServiceRequestFactoryArgumentException("Request not registered"); var ret = _requestMap[typeof(RequestInterface)]; if (ret == null) throw new ServiceRequestFactoryArgumentException("Request not found"); return ret; } readonly Dictionary _requestMap = new Dictionary(); interface IHoldValue { IServiceRequest CreateInstance(); } class Value : IHoldValue where RequestClass : class, IServiceRequest, new() { public IServiceRequest CreateInstance() { return new RequestClass(); } } } }