예제 #1
0
        public IAgent BuildAgent(IProcessExecutionContext executionContext, IRecordPointer <Guid> agentId, bool useCache = true)
        {
            try
            {
                if (executionContext is null)
                {
                    throw new ArgumentNullException("executionContext");
                }
                if (agentId is null)
                {
                    throw new ArgumentNullException("agentId");
                }

                var cacheTimeout = useCache ? getCacheTimeout(executionContext) : null;

                useCache = useCache && executionContext.Cache != null && cacheTimeout != null;

                string cacheKey = null;
                if (useCache)
                {
                    cacheKey = CACHE_KEY + ".Agent." + agentId.Id.ToString();
                    if (executionContext.Cache.Exists(cacheKey))
                    {
                        executionContext.Trace("Returning Agent from Cache.");
                        return(executionContext.Cache.Get <IAgent>(cacheKey));
                    }
                }

                var agentRecord = DataConnector.GetAgentRecord(executionContext.DataService, agentId);

                var roles = DataConnector.GetAgentRoles(executionContext.DataService, agentId);

                var authorizedCustomers = DataConnector.GetAgentRoles(executionContext.DataService, agentId);

                var prohibitedCustomers = DataConnector.GetAgentProhibitedCustomers(executionContext.DataService, agentId);

                var agent = new Agent(agentRecord, roles, authorizedCustomers, prohibitedCustomers);

                if (cacheKey != null)
                {
                    executionContext.Trace("Caching Agent for {0}", cacheTimeout.Value);
                    executionContext.Cache.Add <IAgent>(cacheKey, agent, cacheTimeout.Value);
                }

                return(agent);
            }
            catch (Exception)
            {
                throw;
            }
        }