Example #1
0
        /// <summary>
        /// コンポーネントの生成
        /// </summary>
        /// <param name="componentType">コンポーネント型(NotNull)</param>
        /// <returns>生成したコンポーネント</returns>
        public virtual object Create(Type componentType)
        {
            if (componentType == null)
            {
                throw new ArgumentNullException("componentType");
            }

            QM.OutputLog(GetType(), EnumMsgCategory.INFO,
                         string.Format("ComponentType:[{0}], Creator:[{1}]",
                                       componentType.FullName,
                                       _creatorMap.ContainsKey(componentType) ? "Defined" : "Default"));

            if (_creatorMap.ContainsKey(componentType))
            {
                return(_creatorMap[componentType](componentType));
            }

            // 特に生成処理が指定されていない型はそのままインスタンスを生成
            if (HasNoArgumentConstructor(componentType))
            {
                return(Activator.CreateInstance(componentType));
            }
            throw new QuillException(string.Format("{0}, componentType=[{1}]",
                                                   QMsg.IllegalConstructor.Get(), componentType));
        }
        /// <summary>
        /// Injection対象の型か判定
        /// </summary>
        /// <param name="componentType">コンポーネント型</param>
        /// <returns>true;Injection対象, false:Injection対象外</returns>
        public virtual bool IsTargetType(Type componentType)
        {
            // 必ずInjection対象とする型か?
            if (InjectionTargetTypes.Contains(componentType))
            {
                QM.OutputLog(GetType(), Message.EnumMsgCategory.DEBUG,
                             string.Format("[{0}] is injection target.",
                                           componentType == null ? "null" : componentType.Name));
                return(true);
            }

            // 必ずInjection非対象とする型か?
            if (componentType.FullName.StartsWith("System") ||
                NotInjectionTargetTypes.Contains(componentType))
            {
                QM.OutputLog(GetType(), Message.EnumMsgCategory.DEBUG,
                             string.Format("[{0}] is not injection target.",
                                           componentType == null ? "null" : componentType.Name));
                return(false);
            }

            // 上記どちらでもない場合はデフォルトの設定を適用
            QM.OutputLog(GetType(), Message.EnumMsgCategory.DEBUG,
                         string.Format("[{0}]:isInjectionTarget:{1}",
                                       componentType == null ? "null" : componentType.Name,
                                       IsTargetTypeDefault));
            return(IsTargetTypeDefault);
        }
Example #3
0
        /// <summary>
        /// DbCommandの生成
        /// </summary>
        /// <param name="connection">DB接続</param>
        /// <param name="sql">SQL</param>
        /// <param name="replaceToParamMark">パラメータ名からDB固有のSQLパラメータ名への変換処理</param>
        /// <param name="setParameter">パラメータ設定処理</param>
        /// <returns>生成したDbCommandインスタンス</returns>
        private static IDbCommand CreateCommand(
            IDbConnection connection, string sql,
            Func <string, string> replaceToParamMark,
            Action <int, string, IDataParameter> setParameter)
        {
            var command = connection.CreateCommand();
            var sqlProp = _cachedSqls.GetOrAdd(sql, new SqlProp(sql, replaceToParamMark));

            command.CommandText = sqlProp.ActualSql;

            if (setParameter != null)
            {
                var parameterNames = sqlProp.ParameterNames;
                for (int i = 0; i < parameterNames.Count(); i++)
                {
                    var parameter = command.CreateParameter();
                    parameter.ParameterName = parameterNames[i].Mark;
                    setParameter(i, parameterNames[i].Name, parameter);
                    command.Parameters.Add(parameter);
                }
            }

            // 実行SQLをログ出力
            QM.OutputLog(typeof(SqlUtils), EnumMsgCategory.DEBUG,
                         GetSqlLogString(sqlProp, command.Parameters));

            return(command);
        }
Example #4
0
        /// <summary>
        /// Injection実行
        /// </summary>
        /// <param name="target">Injection対象オブジェクト</param>
        public virtual void Inject(object target)
        {
            if (target == null)
            {
                throw new QuillException("Injection target is null.");
            }

            var targetType = target.GetType();

            if (_injectedTypes.Contains(targetType))
            {
                QM.OutputLog(GetType(), EnumMsgCategory.INFO,
                             string.Format("{0} targetType=[{1}]",
                                           QMsg.AlreadyInjected.Get(), targetType));
                return;
            }

            var fieldInfos = GetFields(target);

            // 処理対象フィールドの取得後、先にインジェクション済型に追加しておく
            // (無限ループを避けるため)
            _injectedTypes.Add(targetType);

            ForEachFields(fieldInfos, fieldInfo => {
                InjectToField(target, fieldInfo, targetType);
            });
        }
Example #5
0
 /// <summary>
 /// 接続終了
 /// </summary>
 /// <param name="connection">コネクション</param>
 public static void CloseConnection(this IDbConnection connection)
 {
     if (connection != null && connection.State == ConnectionState.Open)
     {
         connection.Close();
         QM.OutputLog(typeof(ConnectionUtils), EnumMsgCategory.DEBUG, QMsg.ConnectionClosed.Get());
     }
 }
Example #6
0
 /// <summary>
 /// トランザクション開始
 /// </summary>
 /// <param name="connection">コネクション</param>
 /// <returns>トランザクション開始フラグ</returns>
 protected virtual IDbTransaction Begin(IDbConnection connection)
 {
     if (!IsInTransaction(_transaction))
     {
         _transaction = connection.BeginTransaction();
         QM.OutputLog(GetType(), EnumMsgCategory.DEBUG, QMsg.BeginTx.Get());
     }
     return(_transaction);
 }
Example #7
0
        protected void Application_Start(object sender, EventArgs e)
        {
            QM.InitializeDefault();
            QM.ReplaceToParamMark = (pname => "@" + pname);
            QM.OutputLog          = OutputLog;
            //特殊なインスタンス生成をcallbackでセット
            QM.ComponentCreator = CreateComponentCreator();

            // インジェクション対象とするフィルターを設定
            QM.InjectionFilter = CreateInjectionFilter();
        }
Example #8
0
        /// <summary>
        /// コミット
        /// </summary>
        /// <param name="tx">トランザクション</param>
        protected virtual void Commit(IDbTransaction tx)
        {
            if (IsInTransaction(tx))
            {
                tx.Commit();
                tx.Dispose();
                InitTransaction();

                QM.OutputLog(GetType(), EnumMsgCategory.DEBUG, QMsg.Committed.Get());
            }
        }
Example #9
0
 public void Intercept(IInvocation invocation)
 {
     QM.OutputLog(GetType(), EnumMsgCategory.DEBUG, GetBasicTargetInfo(invocation, "Start"));
     try {
         invocation.Proceed();
     } catch (System.Exception ex) {
         QM.OutputLog(GetType(), EnumMsgCategory.DEBUG, GetBasicTargetInfo(invocation,
                                                                           string.Format("{0}, StackTrace:{1}", ex.Message, ex.StackTrace)));
     } finally {
         QM.OutputLog(GetType(), EnumMsgCategory.DEBUG, GetBasicTargetInfo(invocation, "End"));
     }
 }
Example #10
0
 private RETURN_TYPE Decorate <RETURN_TYPE>(Func <RETURN_TYPE> invoker, Type targetType, string methodName)
 {
     QM.OutputLog(targetType, EnumMsgCategory.DEBUG, string.Format("{0} start.", methodName));
     try {
         var result = invoker();
         QM.OutputLog(targetType, EnumMsgCategory.DEBUG, string.Format("{0} is successful.", methodName));
         return(result);
     } catch (System.Exception ex) {
         QM.OutputLog(targetType, EnumMsgCategory.DEBUG, string.Format("{0} is failure.", methodName));
         QM.OutputLog(targetType, EnumMsgCategory.ERROR,
                      string.Format("Message:{0}, StackTrace:{1}", ex.Message, ex.StackTrace));
         throw;
     }
 }
        /// <summary>
        /// メソッド情報の取得
        /// </summary>
        /// <param name="invoker">委譲処理</param>
        /// <param name="args">修飾処理内で引き継ぐ情報</param>
        /// <returns>メソッド情報</returns>
        protected virtual string GetDataSourceName(Delegate invoker,
                                                   IDictionary <string, object> args)
        {
            var dataSourceName = string.Empty;

            if (IsMultiDataSource)
            {
                // 複数データソース対応時のみメソッド情報を取り出す
                dataSourceName = (args != null && args.ContainsKey(QuillKey.DATA_SOURCE_NAME)) ?
                                 (string)args[QuillKey.DATA_SOURCE_NAME] : string.Empty;

                QM.OutputLog(GetType(), EnumMsgCategory.INFO,
                             string.Format("Target:{0}.GetDataSourceName, DataSourceName:{1}",
                                           GetType().Name, dataSourceName));
            }
            return(dataSourceName);
        }
        public void Inject_NotTarget_Test()
        {
            QM.OutputLog(GetType(), EnumMsgCategory.INFO, "Test start.");

            // Arrange
            QuillInjector target = new QuillInjector();
            var           filter = (InjectionFilterBase)QM.InjectionFilter;

            filter.IsTargetTypeDefault = false;
            var actual = new InjectionTargetMain();

            // Act
            target.Inject(actual);

            // Assert
            Assert.IsNull(actual.Actual, "インジェクション対象外の想定");
        }
Example #13
0
        /// <summary>
        /// コンポーネントの取得
        /// </summary>
        /// <param name="componentType">コンポーネントの型</param>
        /// <param name="isCache">true:キャッシュする, false:毎回インスタンス新規生成</param>
        /// <param name="withInjection">true:インジェクション済のコンポーネント取得, false:インジェクションなし</param>
        /// <returns>コンポーネントのインスタンス</returns>
        public virtual object GetComponent(Type componentType, bool isCache = true, bool withInjection = false)
        {
            if (isCache)
            {
                QM.OutputLog(GetType(), EnumMsgCategory.DEBUG,
                             string.Format("CacheType:{0}", _components.GetType().Name));

                if (_components is ConcurrentDictionary <Type, object> )
                {
                    var components = (ConcurrentDictionary <Type, object>)_components;
                    return(components.GetOrAdd(componentType, t => CreateComponent(t, withInjection)));
                }

                if (!_components.ContainsKey(componentType))
                {
                    _components.Add(componentType, CreateComponent(componentType, withInjection));
                }
                return(_components[componentType]);
            }
            return(CreateComponent(componentType, withInjection));
        }
Example #14
0
 protected void Application_End(object sender, EventArgs e)
 {
     QM.Dispose();
 }
Example #15
0
 /// <summary>
 /// 例外メッセージ、内部例外を指定してインスタンス生成(ログ出力付き)
 /// </summary>
 /// <param name="message">エラーメッセージ</param>
 /// <param name="innerException">内部例外</param>
 public QuillException(string message, System.Exception innerException)
     : base(message, innerException)
 {
     QM.OutputLog(GetType(), EnumMsgCategory.ERROR, message);
 }
Example #16
0
 /// <summary>
 /// 例外メッセージを指定してインスタンス生成(ログ出力付き)
 /// </summary>
 /// <param name="message">エラーメッセージ</param>
 public QuillException(string message) : base(message)
 {
     QM.OutputLog(GetType(), EnumMsgCategory.ERROR, message);
 }