Mirror of Svelto.ECS because we're a fan of it
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.5KB

  1. using System;
  2. using System.Collections.Generic;
  3. namespace Svelto.ServiceLayer
  4. {
  5. public abstract class ServiceRequestsFactory : IServiceRequestsFactory
  6. {
  7. public RequestInterface Create<RequestInterface>() where RequestInterface : class, IServiceRequest
  8. {
  9. var ret = RetrieveObjectType<RequestInterface>();
  10. return ret.CreateInstance() as RequestInterface;
  11. }
  12. protected void AddRelation<RequestInterface, RequestClass>() where RequestClass : class, RequestInterface, new()
  13. where RequestInterface : IServiceRequest
  14. {
  15. _requestMap[typeof(RequestInterface)] = new Value<RequestClass>();
  16. }
  17. IHoldValue RetrieveObjectType<RequestInterface>()
  18. {
  19. if (_requestMap.ContainsKey(typeof(RequestInterface)) == false)
  20. throw new ServiceRequestFactoryArgumentException("Request not registered");
  21. var ret = _requestMap[typeof(RequestInterface)];
  22. if (ret == null)
  23. throw new ServiceRequestFactoryArgumentException("Request not found");
  24. return ret;
  25. }
  26. readonly Dictionary<Type, IHoldValue> _requestMap = new Dictionary<Type, IHoldValue>();
  27. interface IHoldValue
  28. {
  29. IServiceRequest CreateInstance();
  30. }
  31. class Value<RequestClass> : IHoldValue where RequestClass : class, IServiceRequest, new()
  32. {
  33. public IServiceRequest CreateInstance()
  34. {
  35. return new RequestClass();
  36. }
  37. }
  38. }
  39. }