public BehaviorFactory RegisterBehavior(BehaviorKey key, Func <IBehavior> factoryFunction)
        {
            Check.NotNull(key, nameof(key));
            Check.NotNull(factoryFunction, nameof(factoryFunction));

            _behaviors[key] = factoryFunction;
            return(this);
        }
Exemple #2
0
        private void AssertBehavior <TBehavior>(IVMPropertyDescriptor property, BehaviorKey key, Action <TBehavior> assertion) where TBehavior : IBehavior
        {
            Assert.IsNotNull(property);

            Config
            .PropertyConfigurations[property]
            .ConfigureBehavior <TBehavior>(key, assertion);
        }
        public BehaviorFactory RegisterBehavior <T>(
            BehaviorKey key
            ) where T : IBehavior, new()
        {
            Check.NotNull(key, nameof(key));

            RegisterBehavior(key, () => new T());
            return(this);
        }
        public void Key_CreatesKeyObjectWithExtractedFieldName()
        {
            Assert.IsNotNull(TestBehaviorKeys.TestKey);

            string expectedStringRepresentation = new BehaviorKey("TestKey").ToString();
            string actualStringRepresentation   = TestBehaviorKeys.TestKey.ToString();

            Assert.AreEqual(expectedStringRepresentation, actualStringRepresentation);
        }
            private static BehaviorChainConfiguration CreateRefreshDetectionPropertyChain()
            {
                var key = new BehaviorKey("RefreshDetector");
                var b   = new BehaviorChainConfiguration();

                b.Append(key, new RefreshDetectorBehavior());
                b.Enable(key);
                return(b);
            }
Exemple #6
0
        public static IMethodLevelBehavior <T> Create <A>(Type beanType, AnnotationCache <A> annotationCache, Type behaviourType,
                                                          IBehaviorTypeExtractor <A, T> behaviourTypeExtractor, IBeanContextFactory beanContextFactory, IServiceContext beanContext) where A : Attribute
        {
            BehaviorKey key = new BehaviorKey(beanType, behaviourType);

            IMethodLevelBehavior <T> behavior = (IMethodLevelBehavior <T>)beanTypeToBehavior.Get(key);

            if (behavior != null)
            {
                if (behavior == noBehavior)
                {
                    return(null);
                }
                return(behavior);
            }
            A annotation = annotationCache.GetAnnotation(beanType);

            if (annotation == null)
            {
                beanTypeToBehavior.Put(key, noBehavior);
                return(null);
            }
            T defaultBehaviour = behaviourTypeExtractor.ExtractBehaviorType(annotation);
            MethodLevelHashMap <T> methodLevelBehaviour = null;

            MethodInfo[] methods = ReflectUtil.GetMethods(beanType);
            for (int a = methods.Length; a-- > 0;)
            {
                MethodInfo method             = methods[a];
                A          annotationOnMethod = annotationCache.GetAnnotation(method);
                if (annotationOnMethod != null)
                {
                    if (methodLevelBehaviour == null)
                    {
                        methodLevelBehaviour = new MethodLevelHashMap <T>();
                    }
                    T behaviourTypeOnMethod = behaviourTypeExtractor.ExtractBehaviorType(annotationOnMethod);
                    if (behaviourTypeOnMethod != null)
                    {
                        methodLevelBehaviour.Put(method, behaviourTypeOnMethod);
                    }
                }
            }
            if (methodLevelBehaviour == null)
            {
                methodLevelBehaviour = new MethodLevelHashMap <T>(0);
            }
            behavior = new MethodLevelBehavior <T>(defaultBehaviour, methodLevelBehaviour);
            beanTypeToBehavior.Put(key, behavior);
            return(behavior);
        }
        public IBehavior Create(BehaviorKey key)
        {
            Func <IBehavior> factoryFunction;

            if (!_behaviors.TryGetValue(key, out factoryFunction))
            {
                throw new ArgumentException(ECommon
                                            .BehaviorForKeyCannotBeCreated
                                            .FormatWith(key)
                                            );
            }

            return(factoryFunction());
        }
Exemple #8
0
            public override bool Equals(Object obj)
            {
                if (Object.ReferenceEquals(obj, this))
                {
                    return(true);
                }
                if (!(obj is BehaviorKey))
                {
                    return(false);
                }
                BehaviorKey other = (BehaviorKey)obj;

                return(beanType.Equals(other.beanType) && behaviourType.Equals(other.behaviourType));
            }
Exemple #9
0
 public void Setup()
 {
     Factory = new BehaviorFactory();
     TestKey = new BehaviorKey("Test key");
 }
Exemple #10
0
 private void AssertBehavior(IVMPropertyDescriptor property, BehaviorKey key, Action <IBehavior> assertion)
 {
     AssertBehavior <IBehavior>(property, key, assertion);
 }