Ejemplo n.º 1
0
        /// <summary>
        /// コンポーネントのインスタンスを取得する
        /// </summary>
        /// <returns>コンポーネントのインスタンス</returns>
        /// <exception cref="CyclicDependencyException">
        /// コンポーネントの依存関係がコンストラクタインジェクションで
        /// 循環していてかつ、循環しているコンポーネントがSingletonモードの場合に発生する例外
        /// </exception>
        /// <remarks>
        /// このメソッドで新しいインスタンスを返すか既存のインスタンスを返すかは
        /// 実装で選択します。
        /// </remarks>
        public object GetInstance()
        {
            if (componentInstance == null)
            {
                if (instantiating && (injectionConstructorFunctor != null))
                {
                    throw new CyclicDependencyException();
                }

                instantiating = true;

                if (injectionConstructorFunctor == null)
                {
                    componentInstance = Activator.CreateInstance(componentType);
                }
                else
                {
                    componentInstance = injectionConstructorFunctor.Invoke();
                }

                foreach (IFunctor functor in injectionFactors)
                {
                    functor.Invoke(componentInstance);
                }

                foreach (IFunctor functor in initializationFactors)
                {
                    functor.Invoke(componentInstance);
                }

                instantiating = false;
            }

            return(componentInstance);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// コンポーネントのインスタンスを返す
        /// </summary>
        /// <returns>コンポーネントのインスタンス</returns>
        /// <exception cref="CyclicDependencyException">
        /// コンポーネントの依存関係が循環していてかつ、循環しているコンポーネントが
        /// Prototypeモードの場合に発生する例外
        /// </exception>
        /// <remarks>
        /// このメソッドで新しいインスタンスを返すか既存のインスタンスを返すかは
        /// 実装で選択します。
        /// </remarks>
        public object GetInstance()
        {
            if (instantiating)
            {
                instantiating = false;
                throw new CyclicDependencyException();
            }

            instantiating = true;

            object instance = injectionConstructorFunctor == null?
                              Activator.CreateInstance(componentType) :
                                  injectionConstructorFunctor.Invoke();

            foreach (IFunctor functor in injectionFactors)
            {
                functor.Invoke(instance);
            }

            foreach (IFunctor functor in initializationFactors)
            {
                functor.Invoke(instance);
            }

            instantiating = false;

            return(instance);
        }
Ejemplo n.º 3
0
        public void TestBindFunctor()
        {
            IFunctor methodFunctor = new MemberFunctor(typeof(TestClass).GetMethod("Print"));
            IFunctor bindFunctor   = new BindFunctor(
                methodFunctor,
                new NotBoundArgument(0),
                "BindFunctorTest Start",
                "1",
                "2",
                "BindFunctorTest End");

            bindFunctor.Invoke(new TestClass());
        }