Ejemplo n.º 1
0
        private static object GetValue(string name, DbType dbType, ICriterion criterion, IContext context)
        {
            object o = null;

            if (criterion != null)
            {
                try
                {
                    switch (dbType)
                    {
                    case DbType.AnsiString:
                        break;

                    case DbType.AnsiStringFixedLength:
                        break;

                    case DbType.Binary:
                        break;

                    case DbType.Boolean:
                        o = criterion.GetValue <bool>(name);
                        break;

                    case DbType.Byte:
                        break;

                    case DbType.Currency:
                        break;

                    case DbType.Date:
                    case DbType.DateTime:
                    case DbType.DateTime2:
                    case DbType.DateTimeOffset:
                        o = criterion.GetValue <DateTime>(name);
                        break;

                    case DbType.Decimal:
                        o = criterion.GetValue <decimal>(name);
                        break;

                    case DbType.Double:
                        o = criterion.GetValue <double>(name);
                        break;

                    case DbType.Guid:
                        o = criterion.GetValue <Guid>(name);
                        break;

                    case DbType.Int16:
                        o = criterion.GetValue <Int16>(name);
                        break;

                    case DbType.Int32:
                        o = criterion.GetValue <Int32>(name);
                        break;

                    case DbType.Int64:
                        o = criterion.GetValue <Int64>(name);
                        break;

                    case DbType.Object:
                        break;

                    case DbType.SByte:
                        break;

                    case DbType.Single:
                        break;

                    case DbType.String:
                        o = criterion.GetValue <string>(name);
                        break;

                    case DbType.StringFixedLength:
                        o = criterion.GetValue <string>(name);
                        break;

                    case DbType.Time:
                        break;

                    case DbType.UInt16:
                        break;

                    case DbType.UInt32:
                        break;

                    case DbType.UInt64:
                        break;

                    case DbType.VarNumeric:
                        break;

                    case DbType.Xml:
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    string message = ex.InnerException != null ? ex.InnerException.Message : ex.Message;
                    var    props   = eXtensibleConfig.GetProperties();
                    context.SetError(500, message);
                    EventWriter.WriteError(message, SeverityType.Error, "DataAccess", props);
                }
            }
            return(o);
        }
Ejemplo n.º 2
0
        void IModelDataGatewayInitializeable.Initialize <T>(ModelActionOption option, IContext context, T t, ICriterion criterion, ResolveDbKey <T> dbkeyResolver)
        {
            string connectionStringKey = ResolveConnectionStringKey(context);

            if (String.IsNullOrWhiteSpace(connectionStringKey))
            {
                connectionStringKey = dbkeyResolver.Invoke(context);
            }
            if (String.IsNullOrEmpty(connectionStringKey))
            {
                int connectionStringCount = ConfigurationProvider.ConnectionStrings.Count;
                if (connectionStringCount > 0)
                {
                    var found = ConfigurationProvider.ConnectionStrings[connectionStringCount - 1];
                    connectionStringKey = found.Name;
                    if (eXtensibleConfig.Inform)
                    {
                        EventWriter.Inform(String.Format("Could not resolve the database key, selecting a connection string from configuration (key={0})", found.Name));
                    }
                }
            }
            if (String.IsNullOrEmpty(connectionStringKey))
            {
                var message = Exceptions.ComposeDbConnectionStringKeyResolutionError <T>(option, t, context);
                context.SetError(500, message.ToPublish());
                EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
            }
            else
            {
                ConnectionStringSettings settings = ConfigurationProvider.ConnectionStrings[connectionStringKey];
                if (settings == null)
                {
                    var message = Exceptions.ComposeDbConnectionNullSettingsError <T>(option, t, context, connectionStringKey);
                    context.SetError(500, message.ToPublish());
                    EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                }
                else
                {
                    SqlConnection connection = null;
                    try
                    {
                        connection = new SqlConnection(settings.ConnectionString);
                    }
                    catch (Exception ex)
                    {
                        var message = Exceptions.ComposeDbConnectionStringFormatError <T>(option, t, context, connectionStringKey, settings.ConnectionString);
                        context.SetError(500, message.ToPublish());
                        context.SetStacktrace(ex.StackTrace);
                        EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                    }
                    if (connection == null)
                    {
                        var message = Exceptions.ComposeDbConnectionCreationError <T>(option, t, context, connectionStringKey);
                        context.SetError(500, message.ToPublish());
                        EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                    }
                    else
                    {
                        DbConnection = connection;
                        if (eXtensibleConfig.CaptureMetrics)
                        {
                            IRequestContext ctx = context as IRequestContext;
                            ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Database.Datasource, String.Format("server:{0};database:{1};", connection.DataSource, connection.Database));
                        }
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public virtual DataSet ExecuteCommand(DataSet dataSet, ICriterion criterion, IContext context)
        {
            DataSet ds = new DataSet()
            {
                DataSetName = "DataSet"
            };

            if (DbConnection != null)
            {
                IRequestContext        ctx      = context as IRequestContext;
                ISqlCommandContext <T> resolver = (ISqlCommandContext <T>) this;
                SqlCommand             cmd      = null;

                using (SqlConnection cn = DbConnection)
                {
                    try
                    {
                        cn.Open();
                        cmd = ExecuteCommandSqlCommand(cn, dataSet, criterion, context);
                        if (cmd != null)
                        {
                            if (eXtensibleConfig.CaptureMetrics)
                            {
                                ctx.SetMetric(cmd.CommandType.ToString(), XFConstants.Metrics.Database.Command, cmd.CommandText);
                                ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.Begin, DateTime.Now);
                            }
                            using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                            {
                                try
                                {
                                    adapter.Fill(ds);
                                }
                                catch (Exception ex)
                                {
                                    string s       = cn.ConnectionString;
                                    var    message = Exceptions.ComposeBorrowReaderError <T>(ModelActionOption.GetAll, ex, null, criterion, context, this.GetType().FullName);
                                    context.SetError(500, message.ToPublish());
                                    context.SetStacktrace(ex.StackTrace);
                                    EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                                }
                            }

                            if (eXtensibleConfig.CaptureMetrics)
                            {
                                ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.End, DateTime.Now);
                            }
                        }
                        else
                        {
                            var message = Exceptions.ComposeNullSqlCommand <T>(ModelActionOption.GetAll, null, criterion, context, this.GetType().FullName);
                            context.SetError(500, message.ToPublish());
                            EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                        }
                    }
                    catch (Exception ex)
                    {
                        if (eXtensibleConfig.CaptureMetrics)
                        {
                            ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.End, DateTime.Now);
                        }
                        string database = String.Format("server:{0};database:{1}", cn.DataSource, cn.Database);
                        var    message  = Exceptions.ComposeSqlException <T>(ModelActionOption.GetAll, ex, null, criterion, context, this.GetType().FullName, database);
                        context.SetError(500, message.ToPublish());
                        context.SetStacktrace(ex.StackTrace);
                        EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                    }
                }
            }


            return(ds);
        }
Ejemplo n.º 4
0
        public virtual IEnumerable <Projection> GetAllProjections(ICriterion criterion, IContext context)
        {
            List <Projection> list = new List <Projection>();

            if (DbConnection != null)
            {
                IRequestContext ctx = context as IRequestContext;
                SqlCommand      cmd = null;

                using (SqlConnection cn = DbConnection)
                {
                    try
                    {
                        cn.Open();
                        cmd = GetAllProjectionsSqlCommand(cn, criterion, context);

                        if (cmd != null)
                        {
                            if (eXtensibleConfig.CaptureMetrics)
                            {
                                ctx.SetMetric(cmd.CommandType.ToString(), XFConstants.Metrics.Database.Command, cmd.CommandText);
                                ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.Begin, DateTime.Now);
                            }
                            using (SqlDataReader reader = cmd.ExecuteReader())
                            {
                                try
                                {
                                    BorrowReaderProjections(reader, list);
                                }
                                catch (Exception ex)
                                {
                                    var message = Exceptions.ComposeBorrowReaderError <T>(ModelActionOption.GetAllProjections, ex, null, criterion, context, this.GetType().FullName);
                                    context.SetError(500, message.ToPublish());
                                    context.SetStacktrace(ex.StackTrace);
                                    EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                                }
                            }
                            if (eXtensibleConfig.CaptureMetrics)
                            {
                                ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.End, DateTime.Now);
                                ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Count, list.Count);
                            }
                        }
                        else
                        {
                            var message = Exceptions.ComposeNullSqlCommand <T>(ModelActionOption.GetAllProjections, null, criterion, context, this.GetType().FullName);
                            context.SetError(500, message.ToPublish());
                            EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                        }
                    }
                    catch (Exception ex)
                    {
                        if (eXtensibleConfig.CaptureMetrics)
                        {
                            ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.End, DateTime.Now);
                        }
                        string database = String.Format("server:{0};database:{1}", cn.DataSource, cn.Database);
                        var    message  = Exceptions.ComposeSqlException <T>(ModelActionOption.GetAllProjections, ex, null, criterion, context, this.GetType().FullName, database);
                        context.SetError(500, message.ToPublish());
                        context.SetStacktrace(ex.StackTrace);
                        EventWriter.WriteError(message.ToLog(), SeverityType.Error);
                    }
                }
            }


            return(list);
        }
Ejemplo n.º 5
0
        public virtual ICriterion Delete(ICriterion criterion, IContext context)
        {
            Criterion item = new Criterion();

            if (criterion != null)
            {
                if (DbConnection != null)
                {
                    IRequestContext ctx = context as IRequestContext;
                    //ISqlCommandContext<T> resolver = (ISqlCommandContext<T>)this;
                    SqlCommand cmd = null;
                    int        i   = 0;
                    using (SqlConnection cn = DbConnection)
                    {
                        try
                        {
                            cn.Open();
                            cmd = DeleteSqlCommand(cn, criterion, context);

                            if (cmd != null)
                            {
                                if (eXtensibleConfig.CaptureMetrics)
                                {
                                    ctx.SetMetric(cmd.CommandType.ToString(), XFConstants.Metrics.Database.Command, cmd.CommandText);
                                    ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.Begin, DateTime.Now);
                                }
                                i = cmd.ExecuteNonQuery();
                                bool b = (i == 1) ? true : false;
                                item.AddItem("Success", b);
                                if (eXtensibleConfig.CaptureMetrics)
                                {
                                    ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.End, DateTime.Now);
                                    ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Count, i);
                                }
                            }
                            else
                            {
                                var message = Exceptions.ComposeNullSqlCommand <T>(ModelActionOption.Delete, null, criterion, context, this.GetType().FullName);
                                context.SetError(500, message.ToPublish());
                                EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                            }
                        }
                        catch (Exception ex)
                        {
                            if (eXtensibleConfig.CaptureMetrics)
                            {
                                ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Scope.Command.End, DateTime.Now);
                            }
                            string database = String.Format("server:{0};database:{1}", cn.DataSource, cn.Database);
                            var    message  = Exceptions.ComposeSqlException <T>(ModelActionOption.Delete, ex, null, criterion, context, this.GetType().FullName, database);
                            context.SetError(500, message.ToPublish());
                            context.SetStacktrace(ex.StackTrace);
                            EventWriter.WriteError(message.ToLog(), SeverityType.Error, XFConstants.Category.DataAccess, context.ToDictionary(message.Id));
                        }
                    }
                }
            }
            else if (eXtensibleConfig.Inform)
            {
                EventWriter.Inform(String.Format("No criterion was provided for {0}.Delete method in {1}", GetModelType().FullName, this.GetType().FullName));
            }

            return(item);
        }
Ejemplo n.º 6
0
        public bool Load(out T t, IEnumerable <string> folderpaths)
        {
            bool b = false;

            t = new T();
            using (AggregateCatalog catalog = new AggregateCatalog())
            {
                AddCatalogs(catalog, folderpaths);
                AddTypes(catalog);

                using (CompositionContainer container = new CompositionContainer(catalog, true))
                {
                    try
                    {
                        container.ComposeParts(t);
                        b = true;
                        if (eXtensibleConfig.Inform)
                        {
                            EventWriter.Inform("TypeCache loaded successfully");
                        }
                    }
                    catch (System.IO.FileLoadException fileEx)
                    {
                        string message = (fileEx.InnerException != null) ? fileEx.InnerException.Message : fileEx.Message;
                        EventWriter.WriteError(message, SeverityType.Critical);
                        if (eXtensibleConfig.Inform)
                        {
                            EventWriter.Inform(message);
                        }
                    }
                    catch (System.TypeLoadException loadEx)
                    {
                        string message = (loadEx.InnerException != null) ? loadEx.InnerException.Message : loadEx.Message;
                        EventWriter.WriteError(message, SeverityType.Critical);
                        if (eXtensibleConfig.Inform)
                        {
                            EventWriter.Inform(message);
                        }
                    }
                    catch (ReflectionTypeLoadException reflexEx)
                    {
                        StringBuilder sb = new StringBuilder();
                        foreach (var item in reflexEx.LoaderExceptions)
                        {
                            sb.AppendLine(item.Message);
                        }
                        EventWriter.WriteError(sb.ToString(), SeverityType.Critical);
                        if (eXtensibleConfig.Inform)
                        {
                            EventWriter.Inform(sb.ToString());
                        }
                    }
                    catch (Exception ex)
                    {
                        string message = (ex.InnerException != null) ? ex.InnerException.Message : ex.Message;
                        EventWriter.WriteError(message, SeverityType.Critical);
                        if (eXtensibleConfig.Inform)
                        {
                            EventWriter.Inform(message);
                        }
                    }
                }
            }
            return(b);
        }