Exemplo n.º 1
0
        private IRpcHander <T> ResolveImplementor <T>(ModelActionOption option, IContext context, T t, ICriterion criterion) where T : class, new()
        {
            IRpcHander <T> implementor = null;

            Type type = _TypeCache.ResolveType <T>();

            if (type == null)
            {
                var message = Exceptions.ComposeRpcImplementorResolutionError <T>(option, t, context);
                context.SetError(500, message.ToPublish());
                EventWriter.WriteError(message.ToLog(), SeverityType.Error);
            }
            else
            {
                implementor = Activator.CreateInstance(type) as IRpcHander <T>;
                if (implementor == null)
                {
                    var message = Exceptions.ComposeRpcImplementorInstantiationError <T>(option, t, context, type.FullName);
                    context.SetError(500, message.ToPublish());
                    var props = eXtensibleConfig.GetProperties();
                    props.Add(XFConstants.Context.Ticket, message.Id);
                    EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, props);
                }
                else
                {
                    EventWriter.Inform(String.Format("RpcHandler selected: {0}", type.FullName));
                    implementor.Context = context;
                }
            }
            return(implementor);
        }
        //private U Execute<T, U>(T model, ICriterion criterion, IRequestContext requestContext) where T : class, new()
        //{
        //    U result = default(U);
        //    var implementor = ResolveImplementor<T>(ModelActionOption.Execute, requestContext, null, criterion);
        //    if (implementor != null)
        //    {
        //        IContext context = requestContext as IContext;
        //        result = implementor.Execute<U>(model, criterion, context);
        //    }
        //    return result;
        //}



        #endregion

        //    string s = context.GetValue<string>(XFConstants.Context.Application);

        private string ResolveDbKey <T>(IContext context) where T : class, new()
        {
            string key = DatabaseKeyResolver.Resolve <T>(context);

            if (String.IsNullOrWhiteSpace(key) && !String.IsNullOrWhiteSpace(eXtensibleConfig.ConnectionStringKey))
            {
                key = eXtensibleConfig.ConnectionStringKey;
            }
            if (eXtensibleConfig.Inform)
            {
                EventWriter.Inform(String.Format("database key resolved to {0}", key));
            }
            return(key);
        }
        private IModelDataGateway <T> ResolveImplementor <T>(ModelActionOption option, IContext context, T t, ICriterion criterion) where T : class, new()
        {
            IModelDataGateway <T> implementor = null;

            Type type = _TypeCache.ResolveType <T>();

            if (type == null)
            {
                // if not, create one and add the model.  then add sproc based if exists, otherwise add inline on appcontext-model by appcontext-model basis;
                if (_DbConfigs.Contains(context.ApplicationContextKey))
                {
                    var    modelType = typeof(T);
                    string modelKey  = modelType.FullName;
                    var    found     = _DbConfigs[context.ApplicationContextKey].Models.Find(x => x.Key.Equals(modelKey));
                    if (found == null)
                    {
                        XF.Common.Db.Model model = new Common.Db.Model()
                        {
                            Key = modelKey, modelType = modelType.AssemblyQualifiedName, ModelActions = new List <Common.Db.ModelAction>(), Commands = new List <Common.Db.DbCommand>(), DataMaps = new List <Common.Db.DataMap>()
                        };
                        _DbConfigs[context.ApplicationContextKey].Models.Add(model);
                        found = _DbConfigs[context.ApplicationContextKey].Models.Find(x => x.Key.Equals(modelKey));
                    }
                    if (found != null)
                    {
                        implementor = new ConfigModelDataGateway <T>(found);
                    }
                    else
                    {
                    }
                }
                else if (eXtensibleConfig.Infer)
                {
                    implementor = new GenericModelDataGateway <T>();
                }

                //if (implementor == null)
                //{
                //    var message = Exceptions.ComposeImplementorResolutionError<T>(option, t, context);
                //    context.SetError(500, message.ToPublish());
                //    EventWriter.WriteError(message.ToLog(), SeverityType.Error);
                //}
            }
            else
            {
                implementor = Activator.CreateInstance(type) as IModelDataGateway <T>;
            }
            if (implementor == null)
            {
                if (type != null)
                {
                    var message = Exceptions.ComposeImplementorInstantiationError <T>(option, t, context, type.FullName);
                    context.SetError(500, message.ToPublish());
                    var props = eXtensibleConfig.GetProperties();
                    props.Add(XFConstants.Context.Ticket, message.Id);
                    EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, props);
                }
                else
                {
                    var message = Exceptions.ComposeImplementorResolutionError <T>(option, t, context);
                    context.SetError(500, message.ToPublish());
                    var props = eXtensibleConfig.GetProperties();
                    props.Add(XFConstants.Context.Ticket, message.Id);
                    EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, props);
                }
            }
            else
            {
                if (eXtensibleConfig.Inform)
                {
                    EventWriter.Inform(String.Format("MDG selected: {0}", implementor.GetType().FullName));
                }

                implementor.DataService = this as IDatastoreService;
                implementor.Context     = context;

                ICacheable <T> cacheable = implementor as ICacheable <T>;
                if (cacheable != null)
                {
                    cacheable.Cache = Cache;
                }
                // TODO, outsource this
                IModelDataGatewayInitializeable initializable = implementor as IModelDataGatewayInitializeable;
                if (initializable != null)
                {
                    initializable.Initialize <T>(option, context, t, criterion, ResolveDbKey <T>);
                }
            }

            return(implementor);
        }