예제 #1
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 = ConfigurationManager.ConnectionStrings.Count;
                if (connectionStringCount > 0)
                {
                    var found = ConfigurationManager.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 = ConfigurationManager.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));
                        }
                    }
                }
            }
        }
예제 #2
0
        void IModelDataGatewayInitializeable.Initialize<T>(ModelActionOption option, IContext context, T t, ICriterion criterion, ResolveDbKey<T> dbkeyResolver)
        {
            string key = ResolveConnectionStringKey(context);
            if (String.IsNullOrWhiteSpace(key))
            {
                key = dbkeyResolver.Invoke(context);
            }
            if (String.IsNullOrEmpty(key))
            {
                var message = Exceptions.ComposeDbConnectionStringKeyResolutionError<T>(option, t, context);
                context.SetError(500, message.ToPublish() );
                EventWriter.WriteError(message.ToLog(), SeverityType.Error);
            }
            else
            {
                var cn = ConfigurationManager.ConnectionStrings[key];
                if (cn != null)
                {
                    MongoConnectionInfo info = new MongoConnectionInfo();
                    if (info.Initialize(cn))
                    {
                        MongoDb = info.GetDatabase();
                    }

                    if (MongoDb == null)
                    {
                        var message = Exceptions.ComposeDbConnectionCreationError<T>(option, t, context, key);
                        context.SetError(500, message.ToPublish());
                        EventWriter.WriteError(message.ToLog(), SeverityType.Error);
                    }
                    else
                    {
                        Collection = GetCollection();
                        if (Collection == null)
                        {
                            context.SetError(500, "MongoDB.Collection is Null");
                        }
                        else if (eXtensibleConfig.CaptureMetrics)
                        {
                            IRequestContext ctx = context as IRequestContext;
                            ctx.SetMetric(XFConstants.Metrics.Scope.DataStore, XFConstants.Metrics.Database.Datasource, String.Format("server:{0};database:{1};", cn.ConnectionString, key)); ;
                        }
                    }
                }
                else
                {
                    var message = Exceptions.ComposeDbConnectionCreationError<T>(option, t, context, key);
                    context.SetError(500, message.ToPublish());
                    EventWriter.WriteError(message.ToLog(), SeverityType.Error);
                }

            }
        }