示例#1
0
 private void AddCatalogsEachDll(AggregateCatalog catalog, IEnumerable <string> folderpaths)
 {
     foreach (var path in folderpaths)
     {
         if (Directory.Exists(path))
         {
             DirectoryInfo directorInfo = new DirectoryInfo(path);
             var           fileInfos    = directorInfo.GetFileSystemInfos("*.dll");
             foreach (FileInfo info in fileInfos)
             {
                 try
                 {
                     var assemblyCatalog = new AssemblyCatalog(Assembly.LoadFile(info.FullName));
                     var parts           = assemblyCatalog.Parts.ToArray();
                     catalog.Catalogs.Add(assemblyCatalog);
                 }
                 catch (System.IO.FileLoadException fileEx)
                 {
                     string message = (fileEx.InnerException != null) ? fileEx.InnerException.Message : fileEx.Message;
                     EventWriter.WriteError(fileEx.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);
                     }
                 }
             }
         }
     }
 }
        public static IConfigStrategyResolver Load(string sectionGroupName)
        {
            IConfigStrategyResolver resolver = new DatabaseKeyResolver();

            string filepath = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
            var    fileMap  = new ExeConfigurationFileMap()
            {
                ExeConfigFilename = filepath
            };

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            eXtensibleStrategySectionGroup group = config.SectionGroups[sectionGroupName] as eXtensibleStrategySectionGroup;

            resolver.Initialize(group);
            if (eXtensibleConfig.Inform)
            {
                EventWriter.Inform("DatabaseKeyResolver loaded");
            }

            return(resolver);
        }
示例#3
0
        static SqlResolver()
        {
            List <XF.Common.Db.DbConfig> list = new List <Db.DbConfig>();

            string[] filepaths = null;
            // look for dbConfigs in special folder
            if (Directory.Exists(eXtensibleConfig.DbConfigs))
            {
                filepaths = Directory.GetFiles(eXtensibleConfig.DbConfigs, "*.dbconfig.xml", SearchOption.AllDirectories);
            }
            if (filepaths == null)
            {
                filepaths = Directory.GetFiles(eXtensibleConfig.BaseDirectory, "$.dbconfig.xml", SearchOption.TopDirectoryOnly);
            }
            if (filepaths != null && filepaths.Length > 0)
            {
                foreach (var filepath in filepaths)
                {
                    if (File.Exists(filepath))
                    {
                        try
                        {
                            var config = GenericSerializer.ReadGenericItem <XF.Common.Db.DbConfig>(filepath);
                            list.Add(config);
                        }
                        catch (Exception ex)
                        {
                            EventWriter.WriteError(ex.Message, SeverityType.Error, "DataAccess", eXtensibleConfig.GetProperties());
                            EventWriter.Inform(String.Format("Unable to load: ", filepath));
                        }
                    }
                }
            }
            Configs = new Db.DbConfigCollection();
            foreach (var item in list)
            {
                Configs.Add(item);
            }
        }
示例#4
0
        private void DiscoverTypes()
        {
            if (eXtensibleConfig.Inform)
            {
                EventWriter.Inform(String.Format("{0}|{1}|Typecache is discovering pluggable types...", System.AppDomain.CurrentDomain.FriendlyName, "type-cache"));
            }
            TypeMapContainer container             = null;
            ModuleLoader <TypeMapContainer> loader = new ModuleLoader <TypeMapContainer>();

            if (folderpaths != null && folderpaths.Count > 0)
            {
                loader.Folderpaths = folderpaths;
            }
            _TypeCache = new Dictionary <string, List <Type> >();
            if (loader.Load(out container))
            {
                foreach (var item in container.TypeMaps)
                {
                    string key = String.IsNullOrWhiteSpace(item.Domain) ? item.KeyType.FullName : item.Domain;

                    if (!_TypeCache.ContainsKey(key))
                    {
                        _TypeCache.Add(key, new List <Type>());
                    }

                    _TypeCache[key].Add(item.TypeResolution);

                    if (eXtensibleConfig.Inform && !item.TypeResolution.ToString().Contains("XF."))
                    {
                        string name    = System.AppDomain.CurrentDomain.FriendlyName;
                        string message = String.Format("{0}|{1}|Typecache associated {2} with {3}", name, "type-cache", key, item.TypeResolution);
                        EventWriter.Inform(message);
                    }
                }
            }
        }
        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));
                        }
                    }
                }
            }
        }
        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);
        }
示例#7
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);
        }