public static IDatabaseProvider GetDatabaseProvider(DatabaseConfig config) { ArgumentAssertion.IsNotNull(config, "config"); var providerName = config.ProviderName; if (providerTypes.ContainsKey(providerName) == false) { lock (syncRoot) { if (providerTypes.ContainsKey(providerName) == false) { var providerType = TypeExtension.GetMapType(providerName); if (null == providerType) { throw new ArgumentOutOfRangeException("providerName", providerName , string.Format("没有定义数据库{0}的驱动:{1}", config.ConfigName, providerName)); } else { providerTypes[providerName] = providerType; } } } } var dbPrproviderType = providerTypes[providerName]; var dbProvider = (IDatabaseProvider)Activator.CreateInstance(dbPrproviderType); dbProvider.ConnectionString = config.ConnectionString; return(dbProvider); }
static Type GetMapType(Type source, string typeAlias) { var result = source; if (string.IsNullOrEmpty(typeAlias)) { result = source.GetMapType(); } else { result = Type.GetType(typeAlias); if (null == result) { var aliasKeys = new string[] { string.Format("{0}.{1}", source.Name, typeAlias), typeAlias }; if (source.IsInterface && source.Name.StartsWith("I")) { aliasKeys = new string[] { string.Format("{0}.{1}", source.Name, typeAlias) , string.Format("{0}.{1}", source.Name.Substring(1), typeAlias), typeAlias }; } aliasKeys.FirstOrDefault <string>(aliasKey => { result = TypeExtension.GetMapType(aliasKey); return(result != null); }); } } return(result); }
/// <summary> /// /// </summary> /// <param name="exception"></param> /// <param name="handlingInstanceId"></param> /// <param name="bizInfo"></param> /// <returns></returns> public Exception HandleException(Exception exception, Guid handlingInstanceId, IDictionary bizInfo) { if (null == exception) { return(null); } var msg = this.ReplaceMessage; if (string.IsNullOrEmpty(msg)) { msg = exception.Message; } else if (msg.Contains(MesssageKey)) { msg = msg.Replace(MesssageKey, exception.Message); } if (string.IsNullOrEmpty(this.ReplaceType)) { throw new ConfigException("node define the ReplaceType"); } var targetExceptionType = TypeExtension.GetMapType(this.ReplaceType); if (null == targetExceptionType) { throw new ConfigException(string.Format("cannot find the ExceptionType :{0}", this.ReplaceType)); } object[] extraParameters = new object[1] { msg }; return((Exception)Activator.CreateInstance(targetExceptionType, extraParameters)); }
public void RegisterTypeTest() { AppInstance.RegisterTypeAlias <ServerGroup>(); var actualType = TypeExtension.GetMapType("ServerGroup"); Assert.IsNotNull(actualType); Assert.AreEqual(typeof(ServerGroup), actualType); }
/// <summary> /// 获取IRepository的实例 /// </summary> /// <returns></returns> public static TRepository GetRepository <TRepository>(string categoryName) { var repositoryFactoryName = string.Concat("RepositoryFactory.", categoryName); if (null == TypeExtension.GetMapType(repositoryFactoryName)) { return(ObjectIOCFactory.GetSingleton <TRepository>()); } else { return(ObjectIOCFactory.GetSingleton <IRepositoryFactory>("RepositoryFactory." + categoryName).GetRepository <TRepository>()); } }
/// <summary> /// 加载指定的配置信息 /// </summary> /// <param name="name"></param> /// <returns></returns> protected override IConfigNode LoadConfigInfo(string name) { if (this.LogConfigMap.ContainsKey(name) == false) { var nodePath = string.Format("/loggers/logger[@name='{0}']", name); var configNode = AppInstance.GetConfigNode(this.ModuleKey, nodePath); if (null == configNode) { var defaultCategory = this.GetDefaultCategory(); if (name == defaultCategory) { return(null); } else { return(this.LoadConfigInfo(defaultCategory)); } } if (string.IsNullOrEmpty(configNode.MetaType)) { configNode.MetaType = string.Format("{0}.{1}", AppConfig.LoggingKey, typeof(Logger).Name); } else if (configNode.MetaType.StartsWith(AppConfig.LoggingKey) == false) { configNode.MetaType = string.Format("{0}.{1}", AppConfig.LoggingKey, configNode.MetaType); } this.LogConfigMap[name] = configNode; #region LogSessionMap var logType = TypeExtension.GetMapType(configNode.MetaType); if (null == logType) { throw new ArgumentOutOfRangeException("name", name, "cannot find the type"); } if (logType.GetInterface("ISessionLog") != null) { this.LogSessionEnabledStateMap[name] = true; } else { this.LogSessionEnabledStateMap[name] = false; } #endregion } return(this.LogConfigMap[name]); }
public void ResolveServerGroupTest() { AppInstance.RegisterTypeAlias <ServerGroup>(); var targetType = TypeExtension.GetMapType("ServerGroup"); Assert.IsNotNull(targetType); Assert.AreEqual(typeof(ServerGroup), targetType); var configXmlTemplete = @"<configuration xmlns:c='http://m2sa.net/Schema/Config'> <serverGroup groupName='@groupName' c:type='serverGroup'> <servers> <server serverName='@serverName0' serverIP='@serverIP0' servicePort='@servicePort0'/> <server serverName='@serverName1' serverIP='@serverIP1' servicePort='@servicePort1'/> </servers> </serverGroup></configuration>"; var groupName = TestHelper.RandomizeString("group-"); var serverName0 = TestHelper.RandomizeString("server-"); var serverIP0 = TestHelper.RandomizeString("ip-"); var servicePort0 = TestHelper.RandomizeInt(); var serverName1 = TestHelper.RandomizeString("server-"); var serverIP1 = TestHelper.RandomizeString("ip-"); var servicePort1 = TestHelper.RandomizeInt(); var configInfo = configXmlTemplete.Replace("@groupName", groupName); configInfo = configInfo.Replace("@serverName0", serverName0).Replace("@serverIP0", serverIP0).Replace("@servicePort0", servicePort0.ToString()); configInfo = configInfo.Replace("@serverName1", serverName1).Replace("@serverIP1", serverIP1).Replace("@servicePort1", servicePort1.ToString()); var configXml = new XmlDocument(); configXml.LoadXml(configInfo); var node = configXml.SelectSingleNode("/configuration/serverGroup"); var serverGroup = new ServerGroup(); serverGroup.Initialize(new ConfigNode(node)); Assert.AreEqual(groupName, serverGroup.GroupName); Assert.AreEqual(2, serverGroup.Servers.Count); Assert.AreEqual(serverName0, serverGroup.Servers[0].ServerName); Assert.AreEqual(serverIP0, serverGroup.Servers[0].ServerIP); Assert.AreEqual(servicePort0, serverGroup.Servers[0].ServicePort); Assert.AreEqual(serverName1, serverGroup.Servers[1].ServerName); Assert.AreEqual(serverIP1, serverGroup.Servers[1].ServerIP); Assert.AreEqual(servicePort1, serverGroup.Servers[1].ServicePort); }
/// <summary> /// 创建对象实例 /// </summary> /// <param name="name"></param> /// <returns></returns> protected virtual TType ResolveObject(string name) { TType obj = default(TType); Type implementType = null; var config = this.LoadConfigInfo(name); if (null == config) { throw new ConfigException(string.Format("not find the config : {0}", name)); } if (string.IsNullOrEmpty(config.MetaType)) { implementType = instanceType.GetMapType(); } else { implementType = TypeExtension.GetMapType(config.MetaType); } if (implementType == null) { throw new NotImplementedException(string.Format("cannot find the type : {0}[{1}] !", name, config.MetaType)); } if (implementType.CanCreated()) { obj = (TType)Activator.CreateInstance(implementType, true); } else { throw new NotImplementedException(string.Format("{0} cannot be created !", implementType)); } if (null != obj) { obj.Initialize(config); } return(obj); }
private void InitPolicyMap() { if (this.PolicyEntries == null) { this.policyMap = new Dictionary <Type, ExceptionPolicyEntry>(0); } else { this.policyMap = new Dictionary <Type, ExceptionPolicyEntry>(this.PolicyEntries.Count); foreach (var item in this.PolicyEntries) { var type = TypeExtension.GetMapType(item.ExceptionType); if (null == type) { throw new ConfigException(string.Format("cannot find the ExceptionType :{0}", item.ExceptionType)); } this.policyMap[type] = item; } } }