private void RegisterTemplate(Type templateType) { ITemplate template = (ITemplate)state.aopEngine.CreateProxy(templateType); template.Context = this; template.Initialize(); this.template = (TEMPLATE)template; foreach (MethodInfo method in template.GetType().GetMethods()) { FactoryMethodAttribute attrib = method.GetCustomAttributes(typeof(FactoryMethodAttribute), true).FirstOrDefault() as FactoryMethodAttribute; if (attrib == null) { continue; //not a factory method } if (!method.IsVirtual) { throw new Exception(string.Format("Factory method '{0}.{1}' must be marked as virtual", template.GetType().Name, method.Name)); } RegisterObjectFactoryMethod(template, method, attrib); } foreach (MethodInfo method in template.GetType().GetMethods()) { ConfigurationMethodAttribute attrib = method.GetCustomAttributes(typeof(ConfigurationMethodAttribute), true).FirstOrDefault() as ConfigurationMethodAttribute; if (attrib == null) { continue; //not a configuration method } RegisterObjectConfigurationMethod(template, method, attrib); } }
private object NamedFactoryCall(MethodInvocation call, FactoryMethodAttribute attrib,IContext context) { string factoryId = GetFactoryId(call, attrib); //get from context cache if (attrib.InstanceMode == InstanceMode.PerContext && context.State.namedPerContextObjects.ContainsKey(factoryId)) return context.State.namedPerContextObjects[factoryId]; //get from graph cache if (attrib.InstanceMode == InstanceMode.PerGraph && context.State.namedPerGraphObjects.ContainsKey(factoryId)) return context.State.namedPerGraphObjects[factoryId]; //get from thread cache if (attrib.InstanceMode == InstanceMode.PerThread && context.State.namedPerThreadObjects.ContainsKey(factoryId)) return context.State.namedPerThreadObjects[factoryId]; object res = call.Proceed(); if (res is IRunnable) RunnableEngine.RunRunnable(res as IRunnable); //add to context cache if (attrib.InstanceMode == InstanceMode.PerContext) context.State.namedPerContextObjects.Add(factoryId, res); //add to graph cache if (attrib.InstanceMode == InstanceMode.PerGraph) context.State.namedPerGraphObjects.Add(factoryId, res); //add to thread cache if (attrib.InstanceMode == InstanceMode.PerThread) context.State.namedPerThreadObjects.Add(factoryId, res); return res; }
private string GetFactoryId(MethodInvocation call, FactoryMethodAttribute attrib) { string factoryId = null; if (attrib.FactoryId != null) factoryId = attrib.FactoryId; else factoryId = call.Method.Name; return factoryId; }
private object TypedFactoryCall(MethodInvocation call, FactoryMethodAttribute attrib, IContext context) { Type factoryType = GetFactoryType(call, attrib); //get from context cache if (attrib.InstanceMode == InstanceMode.PerContext && context.State.typedPerContextObjects.ContainsKey(factoryType)) { return(context.State.typedPerContextObjects[factoryType]); } //get from graph cache if (attrib.InstanceMode == InstanceMode.PerGraph && context.State.typedPerGraphObjects.ContainsKey(factoryType)) { return(context.State.typedPerGraphObjects[factoryType]); } //get from thread cache if (attrib.InstanceMode == InstanceMode.PerThread && context.State.typedPerThreadObjects.ContainsKey(factoryType)) { return(context.State.typedPerThreadObjects[factoryType]); } object res = call.Proceed(); if (res is IRunnable) { RunnableEngine.RunRunnable(res as IRunnable); } //add to context cache if (attrib.InstanceMode == InstanceMode.PerContext) { context.State.typedPerContextObjects.Add(factoryType, res); } //add to graph cache if (attrib.InstanceMode == InstanceMode.PerGraph) { context.State.typedPerGraphObjects.Add(factoryType, res); } //add to thread cache if (attrib.InstanceMode == InstanceMode.PerThread) { context.State.typedPerThreadObjects.Add(factoryType, res); } return(res); }
private string GetFactoryId(MethodInvocation call, FactoryMethodAttribute attrib) { string factoryId = null; if (attrib.FactoryId != null) { factoryId = attrib.FactoryId; } else { factoryId = call.Method.Name; } return(factoryId); }
public object HandleCall(MethodInvocation call) { ITemplate template = call.Target as ITemplate; IContext context = template.Context; lock (context.State) { FactoryMethodAttribute attrib = call.Method.GetCustomAttributes(typeof(FactoryMethodAttribute), true).FirstOrDefault() as FactoryMethodAttribute; if (attrib == null) { return(call.Proceed()); //not a factory method; } if (attrib.RegisterAs == FactoryType.DefaultForType) { return(TypedFactoryCall(call, attrib, context)); } else { return(NamedFactoryCall(call, attrib, context)); } } }
private Type GetFactoryType(MethodInvocation call, FactoryMethodAttribute attrib) { MethodInfo method = call.Method as MethodInfo; return method.ReturnType; }
private void RegisterObjectFactoryMethod(ITemplate factory, MethodInfo method, FactoryMethodAttribute attrib) { Type objectType = method.ReturnType; if (method.GetParameters().Length != 0) { throw ExceptionHelper.FactoryParameterCountException(); } if (attrib.FactoryId != null) { FactoryDelegate factoryDelegate = CreateMethodFactoryDelegate(factory, method); RegisterObjectFactoryMethod(attrib.FactoryId, factoryDelegate, attrib.InstanceMode); } else if (attrib.RegisterAs == FactoryType.DefaultForType) { FactoryDelegate factoryDelegate = CreateMethodFactoryDelegate(factory, method); Type defaultType = method.ReturnType; RegisterObjectFactoryMethod(defaultType, factoryDelegate, attrib.InstanceMode); } else { string objectId = method.Name; FactoryDelegate factoryDelegate = CreateMethodFactoryDelegate(factory, method); RegisterObjectFactoryMethod(objectId, factoryDelegate, attrib.InstanceMode); } }
private Type GetFactoryType(MethodInvocation call, FactoryMethodAttribute attrib) { MethodInfo method = call.Method as MethodInfo; return(method.ReturnType); }