/// <summary> /// resolves an instance of this type /// </summary> /// <param name="Container">Container holding the registerd object</param> /// <param name="RegisteredObjectToBuild">Registered Object To Get The Instance Of</param> /// <returns>The resolved instance</returns> public object ResolveInstance(ToracDIContainer Container, RegisteredUnTypedObject RegisteredObjectToBuild) { //**so expression tree is slower if you are just running resolve a handful of times. You would need to get into the 10,000 resolves before it starts getting faster. //**since an asp.net mvc site will handle request after request the pool won't get recycled before 10,000. So we are going to build it for scalability with expression trees //instead of using activator, we are going to use an expression tree which is a ton faster. //so we are going to build a func that takes a params object[] and then we just set it to each item. //if we haven't already built the expression, then let's build and compile it now //singleton will only create it once, so singleton's will use the regular activator because it won't benefit of creating the object once. The cost //of the expression tree compile is too hight. //do we need to create an instance? This handles the derived per thread scoped object if (Instance != null) { //we have a valid instance, return it return Instance; } //go build the instance and store it Instance = Activator.CreateInstance(RegisteredObjectToBuild.ConcreteType, RegisteredObjectToBuild.ResolveConstructorParametersLazy(Container).ToArray()); //now return the object we created. Don't return the instance which could be a derived typed return Instance; }
/// <summary> /// resolves an instance of this type /// </summary> /// <param name="Container">Container holding the registerd object</param> /// <param name="RegisteredObjectToBuild">Registered Object To Get The Instance Of</param> /// <returns>The resolved instance</returns> public object ResolveInstance(ToracDIContainer Container, RegisteredUnTypedObject RegisteredObjectToBuild) { //if we have a valid object and its still alive then return it if (Instance == null || !Instance.IsAlive) { //at this point we don't have a valid object, we need to create it and put it in the property Instance = new WeakReference(CachedActivator.Invoke(RegisteredObjectToBuild.ResolveConstructorParametersLazy(Container).ToArray())); } //now just return the instance's target. return Instance.Target; }
/// <summary> /// resolves an instance of this type /// </summary> /// <param name="Container">Container holding the registerd object</param> /// <param name="RegisteredObjectToBuild">Registered Object To Get The Instance Of</param> /// <returns>The resolved instance</returns> public object ResolveInstance(ToracDIContainer Container, RegisteredUnTypedObject RegisteredObjectToBuild) { //use the activator and go create the instance //return Activator.CreateInstance(RegisteredObjectToBuild.ConcreteType, ConstructorParameters); //**so expression tree is slower if you are just running resolve a handful of times. You would need to get into the 10,000 resolves before it starts getting faster. //**since an asp.net mvc site will handle request after request the pool won't get recycled before 10,000. So we are going to build it for scalability with expression trees //instead of using activator, we are going to use an expression tree which is a ton faster. //so we are going to build a func that takes a params object[] and then we just set it to each item. //if we haven't already built the expression, then let's build and compile it now //transients will benefit from the expression tree. singleton will only create it once, so singleton's will use the regular activator //we have the expression, so let's go invoke it and return the results return CachedActivator.Invoke(RegisteredObjectToBuild.ResolveConstructorParametersLazy(Container).ToArray()); }