Example #1
0
        //[TestMethod]
        public void NotifyPropertyChangedTest()
        {
            IInjector injector = Injector.Create();

            //injector.AddBuilder(new ProxyBuilderPipeline()
            //    .Add<IPropertyData, PropertyData>());
            //injector.AddCallPolicy(null)
            //    /*.AddCallHander<NotifyPropertyChangedBehavior>();*/
            //    .AddBehaviour<NotifyPropertyChangedBehavior>();
            injector.RegisterType <IPropertyData, PropertyData>();


            var obj = injector.CreateInstance <IPropertyData>();

            Assert.IsInstanceOfType(obj, typeof(ProxyPropertyData));

            sb = new StringBuilder();

            obj.PropertyChanged += Obj_PropertyChanged;


            obj.IntProperty = 123;
            StringAssert.EndsWith(sb.ToString(), "IntProperty=123");

            obj.BoolProperty = true;
            StringAssert.EndsWith(sb.ToString(), "BoolProperty=" + true.ToString());

            obj.StringProperty = "hello world";
            StringAssert.EndsWith(sb.ToString(), "StringProperty=hello world");
        }
Example #2
0
        public void Aop_Transparent_Proxy_Named()
        {
            IInjector injector = Injector.Create();

            injector.RegisterType <ISay, AopTransparentSayHello>("hello");
            injector.RegisterType <ISay, AopTransparentSayWorld>("world");


            ISay obj;

            obj = injector.CreateInstance <ISay>("hello");
            Assert.AreEqual("Hello", obj.Say());

            obj = injector.CreateInstance <ISay>("world");
            Assert.AreEqual("World", obj.Say());
        }
        public void Aop_Custom_Proxy_Named()
        {
            using (IInjector injector = Injector.Create())
            {
                injector.RegisterType <ISay, AopCustomSayHello>("hello");
                injector.RegisterType <ISay, AopCustomSayWorld>("world");


                var o = injector.CreateInstance <ISay>("hello");
                Assert.IsInstanceOfType(o, typeof(AopCustomProxySay));
                Assert.AreEqual("Hello", o.Say());

                o = injector.CreateInstance <ISay>("world");
                Assert.IsInstanceOfType(o, typeof(AopCustomProxySay));
                Assert.AreEqual("World", o.Say());
            }
        }
Example #4
0
 public object SpawnInstance(IObjectResolver resolver)
 {
     if (specificInstance != null)
     {
         injector.Inject(specificInstance, resolver, parameters);
         return(specificInstance);
     }
     return(injector.CreateInstance(resolver, parameters));
 }
Example #5
0
        public void Aop_Method()
        {
            using (IInjector injector = Injector.Create())
            {
                injector.RegisterType <IMethodClass, MethodClass>();

                var target = injector.CreateInstance <IMethodClass>();

                Assert.IsNotNull(target);
                Assert.IsInstanceOfType(target, typeof(ProxyMethodClass));
                Assert.AreEqual("Hello World", target.Method());
            }
        }
Example #6
0
        internal static object GetValue(this IInjector source, BuilderParameterInfo parameterInfo, IEnumerable <IBuilderValue> values)
        {
            if (parameterInfo == null)
            {
                throw new ArgumentNullException(nameof(parameterInfo));
            }
            if (parameterInfo.HasValue)
            {
                return(parameterInfo.Value);
            }
            Type   valueType = parameterInfo.ParameterType;
            string name      = parameterInfo.Name;

            if (values != null)
            {
                foreach (var item in values)
                {
                    if (item.IsMatchValue(valueType, name))
                    {
                        return(item.GetValue(valueType, name));
                    }
                }
            }

            if (valueType != null)
            {
                object value;
                if (source.TryGetValue(valueType, name, out value))
                {
                    return(value);
                }

                if (source.IsTypeRegistered(valueType, name))
                {
                    return(source.CreateInstance(valueType, name, values.ToArray()));
                }
            }

            if (parameterInfo.HasDefaultValue)
            {
                return(parameterInfo.DefaultValue);
            }

            throw new BuilderValueNotFoundException(valueType, name);
        }
        public void Custom_Validator()
        {
            using (IInjector injector = Injector.Create())
            {
                injector.RegisterType <ICustomTest, CustomTest>();

                var obj = injector.CreateInstance <ICustomTest>();
                obj.SetString("hello world");
                try
                {
                    obj.SetString("world");
                    Assert.Fail();
                }
                catch (FailedParameterException ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
        public object SpawnInstance(IObjectResolver resolver)
        {
            if (world is null)
            {
                world = GetWorld(resolver);
            }

            if (instance is null)
            {
                instance = (ComponentSystemBase)injector.CreateInstance(resolver, parameters);
                world.AddSystem(instance);

                if (systemGroupType != null)
                {
                    var systemGroup = (ComponentSystemGroup)world.GetOrCreateSystem(systemGroupType);
                    systemGroup.AddSystemToUpdateList(instance);
                }

                return(instance);
            }
            return(world.GetExistingSystem(ImplementationType));
        }
Example #9
0
        private AopServer(IInjector injector, Type serverType, object target, bool hasTarget)
        {
            this.injector   = injector ?? throw new ArgumentNullException(nameof(injector));
            this.serverType = serverType ?? throw new ArgumentNullException(nameof(serverType));

            methodDatas = new Dictionary <MethodBase, MethodData>();


            if (hasTarget)
            {
                if (target == null)
                {
                    throw new ArgumentNullException(nameof(target));
                }
                AttachTarget(target);
            }
            else
            {
                target = injector.CreateInstance(serverType);

                AttachTarget(target);
            }
        }
Example #10
0
        public void Aop_Event()
        {
            using (IInjector injector = Injector.Create())
            {
                injector.RegisterType <IEventClass, EventClass>();

                var target = injector.CreateInstance <IEventClass>();

                int          n       = 0;
                EventHandler handler = new EventHandler((o, e) =>
                {
                    n++;
                });
                Assert.AreEqual(0, n);

                target.Event1 += handler;
                target.OnEvent1();
                Assert.AreEqual(1, n);

                target.Event1 -= handler;
                target.OnEvent1();
                Assert.AreEqual(1, n);
            }
        }
Example #11
0
        private Ret <Entity> RenderEntity(HttpContext httpContext, IInjector injector, Type paperType)
        {
            object paper = null;

            try
            {
                paper = injector.CreateInstance(paperType);

                var requestUri    = httpContext.Request.GetRequestUri();
                var paperContext  = new PaperContext(injector, paper, catalog, requestUri);
                var paperRenderer = new PaperRenderer(injector);

                var ret = paperRenderer.RenderEntity(paperContext);
                return(ret);
            }
            catch (Exception ex)
            {
                return(ex);
            }
            finally
            {
                (paper as IDisposable)?.Dispose();
            }
        }
Example #12
0
 /// <summary>
 /// Instancia o tipo indicado resolvendo os parâmetros indicados no construtor.
 /// </summary>
 /// <typeparam name="T">O tipo a ser instanciado.</typeparam>
 /// <param name="args">Argumentos adicionais do construtor não providos pelo injetor.</param>
 /// <returns>O tipo instanciado.</returns>
 public static T CreateInstance <T>(this IInjector injector, params object[] args)
 {
     return((T)injector.CreateInstance(typeof(T), args));
 }
Example #13
0
 public static T CreateInstance <T>(this IInjector source, params IBuilderValue[] values)
 {
     return((T)source.CreateInstance(typeof(T), null, values));
 }
Example #14
0
 public object SpawnInstance(IObjectResolver resolver)
 => injector.CreateInstance(resolver, parameters);
Example #15
0
 public static object CreateInstance(this IInjector source, Type interfaceType, string name = null)
 {
     return(source.CreateInstance(interfaceType, name, null));
 }
Example #16
0
 public static object CreateInstance(this IInjector source, Type interfaceType, params IBuilderValue[] values)
 {
     return(source.CreateInstance(interfaceType, null, values));
 }
Example #17
0
 public object CreateInstance(Type targetType, string name, IBuilderValue[] values)
 {
     return(injector.CreateInstance(targetType, name, CombinValues(values, this.values).ToArray()));
 }