Beispiel #1
0
        /// <summary>
        /// Create Action handler by action type.
        /// Uses dynamic as result because generic interface ISageActionHandler uses contravariant type and cannot cast to generic by base type
        /// </summary>
        public static dynamic CreateHandler(string actionType)
        {
            Log.DebugFormat("Creating ISageActionHandler for received SageAction ...");

            var actionHandlerType = typeof(ISageActionHandler <>);
            var classType         = SageAction.GetActionClassType(actionType);

            if (HandlersCache.ContainsKey(classType))
            {
                return(HandlersCache[classType]);
            }

            var actionHandlerTypes = AppDomain.CurrentDomain.GetAssemblies()
                                     .SelectMany(assembly => assembly.GetTypes())
                                     .Where(type => Any(type, actionHandlerType, classType)).ToArray();

            if (actionHandlerTypes.Length != 1)
            {
                throw new Exception($"Not found or more than one action handlers implementations for action type: {actionType}");
            }
            var handler = Activator.CreateInstance(actionHandlerTypes[0]);

            HandlersCache.Add(classType, handler);
            return(handler);
        }
        public SageAction Create(string jsonString)
        {
            Log.DebugFormat("Creating Sage50 Action from JSON: {0}", jsonString);

            dynamic sageAction = JObject.Parse(jsonString);

            var actionTypePrefix = (string)sageAction.type;
            var actionType       = SageAction.GetActionClassType(actionTypePrefix);

            if (actionType == null)
            {
                throw new Exception($"Can not found action type for '{actionTypePrefix}' type");
            }

            return((SageAction)JsonConvert.DeserializeObject(jsonString, actionType));
        }