hammett 2004/04/03 15:03:03 Modified: avalon-net/Castle/MicroKernel Apache.Avalon.Castle.MicroKernel.csproj BaseKernel.cs IResolver.cs Kernel.cs Added: avalon-net/Castle/MicroKernel AvalonKernel.cs DefaultKernel.cs avalon-net/Castle/MicroKernel/Aspects AspectInvocationHandler.cs AspectPointCutFlags.cs IAspect.cs avalon-net/Castle/MicroKernel/Assemble Assembler.cs avalon-net/Castle/MicroKernel/Concerns ConcernManager.cs ICommissionConcern.cs IConcern.cs ICreationConcern.cs IDecommissionConcern.cs avalon-net/Castle/MicroKernel/Concerns/Default AbstractConcern.cs ConfigureConcern.cs ContextConcern.cs CreationConcern.cs EnableLoggerConcern.cs EnableLookupConcern.cs InitializeConcern.cs ShutdownConcern.cs StartConcern.cs avalon-net/Castle/MicroKernel/Factory/Default ConcernChainComponentFactory.cs SimpleComponentFactory.cs avalon-net/Castle/MicroKernel/Factory IComponentFactory.cs avalon-net/Castle/MicroKernel/Model/Default DefaultComponentModel.cs DefaultComponentModelBuilder.cs DefaultConstructionModel.cs DefaultDependencyModel.cs avalon-net/Castle/MicroKernel/Model IComponentModel.cs IComponentModelBuilder.cs IConstructionModel.cs IDependencyModel.cs ModelBuilderException.cs Removed: avalon-net/Castle/MicroKernel AspectInvocationHandler.cs AspectPointCutFlags.cs ConstructionInfo.cs HandlerException.cs IAspect.cs IComponentFactory.cs IHandler.cs IHandlerFactory.cs ILifestyleManager.cs ILifestyleManagerFactory.cs State.cs avalon-net/Castle/MicroKernel/Factory SimpleComponentFactory.cs avalon-net/Castle/MicroKernel/Handlers AbstractHandler.cs SimpleHandler.cs SimpleHandlerFactory.cs avalon-net/Castle/MicroKernel/LifestyleManagers SimpleLifestyleManagerFactory.cs TransientLifestyleManager.cs Log: MicroKernel for Avalon Castle - Improvements. Revision Changes Path 1.3 +172 -22 avalon-sandbox/avalon-net/Castle/MicroKernel/Apache.Avalon.Castle.MicroKernel.csproj Index: Apache.Avalon.Castle.MicroKernel.csproj =================================================================== RCS file: /home/cvs/avalon-sandbox/avalon-net/Castle/MicroKernel/Apache.Avalon.Castle.MicroKernel.csproj,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- Apache.Avalon.Castle.MicroKernel.csproj 31 Mar 2004 03:45:08 -0000 1.2 +++ Apache.Avalon.Castle.MicroKernel.csproj 3 Apr 2004 23:03:02 -0000 1.3 @@ -84,117 +84,267 @@ AssemblyName = "Apache.Avalon.DynamicProxy" HintPath = "..\bin\Apache.Avalon.DynamicProxy.dll" /> + <Reference + Name = "Apache.Avalon.Framework" + AssemblyName = "Apache.Avalon.Framework" + HintPath = "..\bin\Apache.Avalon.Framework.dll" + /> </References> </Build> <Files> <Include> <File - RelPath = "AspectInvocationHandler.cs" + RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "AspectPointCutFlags.cs" + RelPath = "AssertUtil.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "AssemblyInfo.cs" + RelPath = "AvalonKernel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "AssertUtil.cs" + RelPath = "BaseKernel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "BaseKernel.cs" + RelPath = "DefaultKernel.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "IResolver.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "ConstructionInfo.cs" + RelPath = "Kernel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "HandlerException.cs" + RelPath = "Aspects\AspectInvocationHandler.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "IAspect.cs" + RelPath = "Aspects\AspectPointCutFlags.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "IComponentFactory.cs" + RelPath = "Aspects\IAspect.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "IHandler.cs" + RelPath = "Assemble\Assembler.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "IHandlerFactory.cs" + RelPath = "Concerns\ConcernManager.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "ILifestyleManager.cs" + RelPath = "Concerns\ICommissionConcern.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "ILifestyleManagerFactory.cs" + RelPath = "Concerns\IConcern.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "IResolver.cs" + RelPath = "Concerns\ICreationConcern.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "Kernel.cs" + RelPath = "Concerns\IDecommissionConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\AbstractConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\ConfigureConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\ContextConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\CreationConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\EnableLoggerConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\EnableLookupConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\InitializeConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\ShutdownConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Concerns\Default\StartConcern.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Factory\IComponentFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Factory\Default\ConcernChainComponentFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Factory\Default\SimpleComponentFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\AbstractHandler.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\ConstructionInfo.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\HandlerException.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\IHandler.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\IHandlerFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\State.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\Default\DefaultHandler.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\Default\DefaultHandlerFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\Default\SimpleHandler.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Handler\Default\SimpleHandlerFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Lifestyle\ILifestyleManager.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Lifestyle\ILifestyleManagerFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Lifestyle\Default\SimpleLifestyleManagerFactory.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Lifestyle\Default\TransientLifestyleManager.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Model\IComponentModel.cs" + SubType = "Code" + BuildAction = "Compile" + /> + <File + RelPath = "Model\IComponentModelBuilder.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "State.cs" + RelPath = "Model\IConstructionModel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "Factory\SimpleComponentFactory.cs" + RelPath = "Model\IDependencyModel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "Handlers\AbstractHandler.cs" + RelPath = "Model\ModelBuilderException.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "Handlers\SimpleHandler.cs" + RelPath = "Model\Default\DefaultComponentModel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "Handlers\SimpleHandlerFactory.cs" + RelPath = "Model\Default\DefaultComponentModelBuilder.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "LifestyleManagers\SimpleLifestyleManagerFactory.cs" + RelPath = "Model\Default\DefaultConstructionModel.cs" SubType = "Code" BuildAction = "Compile" /> <File - RelPath = "LifestyleManagers\TransientLifestyleManager.cs" + RelPath = "Model\Default\DefaultDependencyModel.cs" SubType = "Code" BuildAction = "Compile" /> 1.3 +32 -9 avalon-sandbox/avalon-net/Castle/MicroKernel/BaseKernel.cs Index: BaseKernel.cs =================================================================== RCS file: /home/cvs/avalon-sandbox/avalon-net/Castle/MicroKernel/BaseKernel.cs,v retrieving revision 1.2 retrieving revision 1.3 diff -u -r1.2 -r1.3 --- BaseKernel.cs 31 Mar 2004 03:45:08 -0000 1.2 +++ BaseKernel.cs 3 Apr 2004 23:03:02 -0000 1.3 @@ -19,33 +19,42 @@ using System.Collections.Specialized; using Apache.Avalon.DynamicProxy; + using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for BaseKernel. /// </summary> public class BaseKernel : Kernel { - protected ArrayList m_aspectBefore = new ArrayList(); + protected ArrayList m_aspectBefore; - protected ArrayList m_aspectAfter = new ArrayList(); + protected ArrayList m_aspectAfter; - protected ArrayList m_concerns = new ArrayList(); + protected Hashtable m_components; - protected Hashtable m_components = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); + protected Hashtable m_services; - protected Hashtable m_services = new Hashtable(); + protected Hashtable m_dependencyToSatisfy; - protected Hashtable m_dependencyToSatisfy = new Hashtable(); + protected IHandlerFactory m_handlerFactory; - protected IHandlerFactory m_handlerFactory = new Handlers.SimpleHandlerFactory(); + protected ILifestyleManagerFactory m_lifestyleManagerFactory; - protected ILifestyleManagerFactory m_lifestyleManagerFactory = new LifestyleManagers.SimpleLifestyleManagerFactory(); + protected IComponentModelBuilder m_componentModelBuilder; /// <summary> /// /// </summary> public BaseKernel() { + m_aspectBefore = new ArrayList(); + m_aspectAfter = new ArrayList(); + m_components = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); + m_services = new Hashtable(); + m_dependencyToSatisfy = new Hashtable(); + m_handlerFactory = new Handler.Default.SimpleHandlerFactory(); + m_lifestyleManagerFactory = new Lifestyle.Default.SimpleLifestyleManagerFactory(); + m_componentModelBuilder = new Model.Default.DefaultComponentModelBuilder( this ); } #region Kernel Members @@ -75,8 +84,9 @@ throw new ArgumentException("The specified implementation does not implement the service interface"); } - IHandler handler = HandlerFactory.CreateHandler( service, implementation ); + IComponentModel model = ModelBuilder.BuildModel( key, service, implementation ); + IHandler handler = HandlerFactory.CreateHandler( model ); handler.Init ( this ); m_components[ key ] = handler; @@ -146,6 +156,19 @@ { AssertUtil.ArgumentNotNull( value, "value" ); m_lifestyleManagerFactory = value; + } + } + + public IComponentModelBuilder ModelBuilder + { + get + { + return m_componentModelBuilder; + } + set + { + AssertUtil.ArgumentNotNull( value, "value" ); + m_componentModelBuilder = value; } } 1.2 +1 -1 avalon-sandbox/avalon-net/Castle/MicroKernel/IResolver.cs Index: IResolver.cs =================================================================== RCS file: /home/cvs/avalon-sandbox/avalon-net/Castle/MicroKernel/IResolver.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- IResolver.cs 31 Mar 2004 00:43:30 -0000 1.1 +++ IResolver.cs 3 Apr 2004 23:03:02 -0000 1.2 @@ -23,6 +23,6 @@ { object Resolve(); - void Release(); + void Release( object instance ); } } 1.2 +12 -1 avalon-sandbox/avalon-net/Castle/MicroKernel/Kernel.cs Index: Kernel.cs =================================================================== RCS file: /home/cvs/avalon-sandbox/avalon-net/Castle/MicroKernel/Kernel.cs,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- Kernel.cs 31 Mar 2004 00:43:30 -0000 1.1 +++ Kernel.cs 3 Apr 2004 23:03:02 -0000 1.2 @@ -16,6 +16,8 @@ { using System; + using Apache.Avalon.Castle.MicroKernel.Model; + public delegate void DependencyListenerDelegate( Type service, IHandler handler ); /// <summary> @@ -34,9 +36,18 @@ /// <summary> /// Adds an aspect to kernel. /// </summary> - /// <param name="flags">May be Before, After or both.</param> + /// <param name="flags">Can be Before, After or both.</param> /// <param name="aspect">The aspect implementation</param> void AddAspect( AspectPointCutFlags flags, IAspect aspect ); + + /// <summary> + /// IComponentModel instance builder. + /// </summary> + IComponentModelBuilder ModelBuilder + { + get; + set; + } /// <summary> /// Obtains a component handler using the 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/AvalonKernel.cs Index: AvalonKernel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel { using System; using Apache.Avalon.Castle.MicroKernel.Concerns; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for AvalonKernel. /// </summary> public interface AvalonKernel : Kernel { ConcernManager Concerns { get; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/DefaultKernel.cs Index: DefaultKernel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel { using System; using Apache.Avalon.Castle.MicroKernel.Concerns; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Specialization of BaseKernel to adhere to Avalon /// constraints and semantics. /// </summary> public class DefaultKernel : BaseKernel, AvalonKernel { protected ConcernManager m_concerns = new ConcernManager(); /// <summary> /// /// </summary> public DefaultKernel() { m_handlerFactory = new Handler.Default.DefaultHandlerFactory(); m_lifestyleManagerFactory = new Lifestyle.Default.SimpleLifestyleManagerFactory(); } #region AvalonKernel Members public ConcernManager Concerns { get { return m_concerns; } } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Aspects/AspectInvocationHandler.cs Index: AspectInvocationHandler.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Aspects { using System; using System.Reflection; /// <summary> /// Summary description for AspectInvocationHandler. /// </summary> public class AspectInvocationHandler : DynamicProxy.IInvocationHandler { private Object m_target; protected IAspect[] m_before; protected IAspect[] m_after; public AspectInvocationHandler( IAspect[] before, IAspect[] after, Object target ) { m_before = before; m_after = after; m_target = target; } #region IInvocationHandler Members public object Invoke(object proxy, MethodBase method, params object[] arguments) { Type[] parameters = new Type[arguments.Length]; for(int i=0; i < arguments.Length; i++ ) { parameters[i] = arguments[i].GetType(); } MethodInfo targetMethod = m_target.GetType().GetMethod( method.Name, parameters ); Object returnValue = null; Exception exceptionThrowed = null; Perform( m_before, AspectPointCutFlags.Before, method, returnValue, exceptionThrowed, arguments ); try { targetMethod.Invoke( m_target, arguments ); } catch(Exception ex) { exceptionThrowed = ex; } Perform( m_after, AspectPointCutFlags.After, method, returnValue, exceptionThrowed, arguments ); return null; } #endregion protected void Perform( IAspect[] aspects, AspectPointCutFlags pointcut, MethodBase method, Object returnValue, Exception exceptionThrowed, params object[] arguments ) { foreach( IAspect aspect in aspects ) { try { aspect.Perform( pointcut, m_target, method, returnValue, exceptionThrowed, arguments ); } catch(Exception) { // Exceptions throwed while executing aspects will be // ignored } } } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Aspects/AspectPointCutFlags.cs Index: AspectPointCutFlags.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel { using System; /// <summary> /// Summary description for AspectPointCutFlags. /// </summary> [Flags] public enum AspectPointCutFlags { Before = 1, After = 2, } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Aspects/IAspect.cs Index: IAspect.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel { using System; using System.Reflection; /// <summary> /// Summary description for IAspect. /// </summary> public interface IAspect { void Perform( AspectPointCutFlags pointcut, object componentInstance, MethodBase method, object returnValue, Exception exception, params object[] arguments); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Assemble/Assembler.cs Index: Assembler.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Assemble { using System; using System.Reflection; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// /// </summary> public delegate void ResolveTypeHandler( IComponentModel model, Type typeRequest, String argumentOrPropertyName, out object value ); /// <summary> /// Summary description for Assembler. /// </summary> public class Assembler { private Assembler() { } /// <summary> /// /// </summary> /// <param name="typeSpecified"></param> /// <param name="argumentOrPropertyName"></param> /// <returns></returns> public static bool IsKnown( Type typeSpecified, String argumentOrPropertyName ) { if (IsLogger( typeSpecified ) || IsContext( typeSpecified ) || IsConfiguration( typeSpecified )) { return true; } // TODO: Support injection of Context entries / Configuration // directly into Constructor arguments or setmethods. return false; } /// <summary> /// /// </summary> /// <param name="model"></param> /// <param name="resolver"></param> /// <returns></returns> public static object[] BuildConstructorArguments( IComponentModel model, ResolveTypeHandler resolver ) { AssertUtil.ArgumentNotNull( model, "model" ); AssertUtil.ArgumentNotNull( resolver, "resolver" ); ParameterInfo[] parameters = model.ConstructionModel.SelectedConstructor.GetParameters(); object[] args = new object[ parameters.Length ]; for(int i=0; i < args.Length; i++) { ParameterInfo parameter = parameters[i]; Type service = parameter.ParameterType; String argumentName = parameter.Name; object value = Resolve( model, service, argumentName, resolver ); args[ parameter.Position ] = value; } return args; } public static void AssembleProperties( object instance, IComponentModel model, ResolveTypeHandler resolver ) { AssertUtil.ArgumentNotNull( model, "model" ); AssertUtil.ArgumentNotNull( resolver, "resolver" ); foreach( PropertyInfo property in model.ConstructionModel.SelectedProperties ) { Type service = property.PropertyType; String propertyName = property.Name; object value = Resolve( model, service, propertyName, resolver ); if (value != null) { property.SetValue( instance, value, null ); } } } /// <summary> /// /// </summary> /// <param name="model"></param> /// <param name="type"></param> /// <param name="argumentOrPropertyName"></param> /// <param name="resolver"></param> /// <returns></returns> private static object Resolve( IComponentModel model, Type type, String argumentOrPropertyName, ResolveTypeHandler resolver ) { if (IsLogger( type )) { return model.Logger; } if (IsContext( type )) { return model.Context; } if (IsConfiguration( type )) { return model.Configuration; } object value = null; resolver( model, type, argumentOrPropertyName, out value ); return value; } private static bool IsLogger( Type typeSpecified ) { return typeSpecified == typeof(ILogger); } private static bool IsContext( Type typeSpecified ) { return typeSpecified == typeof(IContext); } private static bool IsConfiguration( Type typeSpecified ) { return typeSpecified == typeof(IConfiguration); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/ConcernManager.cs Index: ConcernManager.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns { using System; using System.Collections; using Apache.Avalon.Castle.MicroKernel.Concerns.Default; /// <summary> /// Summary description for ConcernManager. /// </summary> public class ConcernManager { private Type m_creationConcern; private ArrayList m_commissionConcerns = new ArrayList(); private ArrayList m_decommissionConcerns = new ArrayList(); /// <summary> /// /// </summary> public ConcernManager() { // Configure the default behavior Add( typeof(CreationConcern) ); Add( typeof(EnableLoggerConcern) ); Add( typeof(ContextConcern) ); Add( typeof(EnableLookupConcern) ); Add( typeof(ConfigureConcern) ); Add( typeof(InitializeConcern) ); Add( typeof(StartConcern) ); Add( typeof(ShutdownConcern) ); } public void Add( Type concern ) { AssertUtil.ArgumentNotNull( concern, "concern" ); if ( typeof(ICreationConcern).IsAssignableFrom(concern) ) { m_creationConcern = concern; } else if ( typeof(ICommissionConcern).IsAssignableFrom(concern) ) { m_commissionConcerns.Add( concern ); } else if ( typeof(IDecommissionConcern).IsAssignableFrom(concern) ) { m_decommissionConcerns.Add( concern ); } else { throw new ArgumentException( "Concern implementation must implement " + "ICreationConcern, ICommissionConcern or IDecommissionConcern", "concern"); } } public IList CommissionConcerns { get { return m_commissionConcerns; } } public IList DecommissionConcerns { get { return m_decommissionConcerns; } } public IConcern GetCommissionChain( Kernel kernel ) { ArrayList concerns = new ArrayList( m_commissionConcerns ); concerns.Insert( 0, m_creationConcern ); Type[] concernTypes = (Type[]) concerns.ToArray( typeof(Type) ); Array.Reverse( concernTypes ); IConcern next = null; foreach( Type concernType in concernTypes ) { next = (IConcern) Activator.CreateInstance( concernType, new object[] { next } ); next.Init( kernel ); } return next; } public IConcern GetDecommissionChain( Kernel kernel ) { ArrayList concerns = new ArrayList( m_decommissionConcerns ); Type[] concernTypes = (Type[]) concerns.ToArray( typeof(Type) ); Array.Reverse( concernTypes ); IConcern next = null; foreach( Type concernType in concernTypes ) { next = (IConcern) Activator.CreateInstance( concernType, new object[] { next } ); next.Init( kernel ); } return next; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/ICommissionConcern.cs Index: ICommissionConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns { using System; /// <summary> /// Summary description for ICommissionConcern. /// </summary> public interface ICommissionConcern : IConcern { } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/IConcern.cs Index: IConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns { using System; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for IConcern. /// </summary> public interface IConcern { void Init( Kernel kernel ); void Apply( IComponentModel model, object component ); IConcern Next { get; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/ICreationConcern.cs Index: ICreationConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns { using System; using Apache.Avalon.Castle.MicroKernel.Model; using Apache.Avalon.Castle.MicroKernel.Factory; /// <summary> /// Summary description for ICreationConcern. /// </summary> public interface ICreationConcern : IConcern { object Apply( IComponentModel model, IComponentFactory factory ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/IDecommissionConcern.cs Index: IDecommissionConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns { using System; /// <summary> /// Summary description for IDecommissionConcern. /// </summary> public interface IDecommissionConcern : IConcern { } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/AbstractConcern.cs Index: AbstractConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for AbstractConcern. /// </summary> public abstract class AbstractConcern : IConcern { protected IConcern m_next; protected Kernel m_kernel; public AbstractConcern(IConcern next) { m_next = next; } #region IConcern Members public virtual void Init(Kernel kernel) { m_kernel = kernel; } public virtual void Apply(IComponentModel model, object component) { if (Next != null) { Next.Apply( model, component ); } } public IConcern Next { get { return m_next; } } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/ConfigureConcern.cs Index: ConfigureConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for ConfigureConcern. /// </summary> public class ConfigureConcern : AbstractConcern, ICommissionConcern { public ConfigureConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { ContainerUtil.Configure( component, model.Configuration ); base.Apply( model, component ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/ContextConcern.cs Index: ContextConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for ContextConcern. /// </summary> public class ContextConcern : AbstractConcern, ICommissionConcern { public ContextConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { ContainerUtil.Contextualize( component, model.Context ); base.Apply( model, component ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/CreationConcern.cs Index: CreationConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for CreationConcern. /// </summary> public class CreationConcern : AbstractConcern, ICreationConcern { public CreationConcern(IConcern next) : base(next) { } #region ICreationConcern Members public object Apply(IComponentModel model, IComponentFactory factory) { return factory.Incarnate(); } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/EnableLoggerConcern.cs Index: EnableLoggerConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for EnableLoggerConcern. /// </summary> public class EnableLoggerConcern : AbstractConcern, ICommissionConcern { public EnableLoggerConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { ContainerUtil.EnableLogging( component, model.Logger ); base.Apply( model, component ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/EnableLookupConcern.cs Index: EnableLookupConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using System.Collections; using System.Collections.Specialized; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for EnableLookupConcern. /// </summary> public class EnableLookupConcern : AbstractConcern, ICommissionConcern { public EnableLookupConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { if (component is ILookupEnabled) { IDependencyModel[] dependencies = GetLookupDependencies(model); LookupManager manager = new LookupManager( m_kernel, dependencies ); ContainerUtil.Service( component, manager ); } base.Apply( model, component ); } protected virtual IDependencyModel[] GetLookupDependencies(IComponentModel model) { IDependencyModel[] dependencies = model.Dependencies; ArrayList lookupDependencies = new ArrayList(); foreach(IDependencyModel dependency in dependencies) { if (dependency.LookupKey == null || dependency.LookupKey == String.Empty) { continue; } lookupDependencies.Add( dependency ); } return (IDependencyModel[]) lookupDependencies.ToArray( typeof(IDependencyModel) ); } /// <summary> /// /// </summary> public class LookupManager : ILookupManager { Kernel m_kernel; HybridDictionary m_key2handler = new HybridDictionary(); public LookupManager( Kernel kernel, IDependencyModel[] dependencies ) { m_kernel = kernel; foreach(IDependencyModel dependency in dependencies ) { if (!kernel.HasService( dependency.Service )) { if (!dependency.Optional) { throw new LookupException( dependency.LookupKey, "Kernel can't supply specified service."); } else { continue; } } m_key2handler[ dependency.LookupKey ] = kernel.GetHandlerForService( dependency.Service ); } } #region ILookupManager Members public object this[string role] { get { if (!Contains( role )) { throw new LookupException( role, "Key not found." ); } IHandler handler = (IHandler) m_key2handler[ role ]; object instance = handler.Resolve(); return instance; } } public void Release(object instance) { if ( instance == null ) { return; } foreach(IHandler handler in m_key2handler.Values) { if (handler.IsOwner( instance )) { handler.Release( instance ); return; } } throw new LookupException( "The specified instance was not created by this LookupManager" ); } public bool Contains(string role) { return m_key2handler.Contains( role ); } #endregion } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/InitializeConcern.cs Index: InitializeConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for InitializeConcern. /// </summary> public class InitializeConcern : AbstractConcern, ICommissionConcern { public InitializeConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { ContainerUtil.Initialize( component ); base.Apply( model, component ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/ShutdownConcern.cs Index: ShutdownConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for ShutdownConcern. /// </summary> public class ShutdownConcern : AbstractConcern, IDecommissionConcern { public ShutdownConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { ContainerUtil.Shutdown( component ); base.Apply( model, component ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Concerns/Default/StartConcern.cs Index: StartConcern.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Concerns.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for StartConcern. /// </summary> public class StartConcern : AbstractConcern, ICommissionConcern { public StartConcern(IConcern next) : base(next) { } public override void Apply(IComponentModel model, object component) { ContainerUtil.Start( component ); base.Apply( model, component ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Factory/Default/ConcernChainComponentFactory.cs Index: ConcernChainComponentFactory.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Factory.Default { using System; using Apache.Avalon.Castle.MicroKernel.Model; using Apache.Avalon.Castle.MicroKernel.Concerns; /// <summary> /// Summary description for ConcernChainComponentFactory. /// </summary> public class ConcernChainComponentFactory : IComponentFactory { private IConcern m_commissionChain; private IConcern m_decomissionChain; private IComponentModel m_model; private IComponentFactory m_innerFactory; public ConcernChainComponentFactory( IConcern commissionChain, IConcern decomissionChain, IComponentModel model, IComponentFactory innerFactory) { AssertUtil.ArgumentNotNull( commissionChain, "commissionChain" ); AssertUtil.ArgumentNotNull( decomissionChain, "decomissionChain" ); AssertUtil.ArgumentNotNull( model, "model" ); AssertUtil.ArgumentNotNull( innerFactory, "innerFactory" ); m_commissionChain = commissionChain; m_decomissionChain = decomissionChain; m_model = model; m_innerFactory = innerFactory; } #region IComponentFactory Members public Object Incarnate() { ICreationConcern creationConcern = (ICreationConcern) m_commissionChain; object instance = creationConcern.Apply( m_model, m_innerFactory ); creationConcern.Apply( m_model, instance ); return instance; } public void Etherialize( object instance ) { m_decomissionChain.Apply( m_model, instance ); } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Factory/Default/SimpleComponentFactory.cs Index: SimpleComponentFactory.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Factory.Default { using System; using System.Collections; using System.Reflection; using Apache.Avalon.Castle.MicroKernel.Assemble; using Apache.Avalon.Castle.MicroKernel.Handler; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for SimpleComponentFactory. /// </summary> public class SimpleComponentFactory : IComponentFactory { protected IAspect[] m_before; protected IAspect[] m_after; protected IComponentModel m_componentModel; protected Hashtable m_serv2handler; public SimpleComponentFactory( IAspect[] before, IAspect[] after, IComponentModel componentModel, Hashtable serv2handler) { AssertUtil.ArgumentNotNull( before, "before" ); AssertUtil.ArgumentNotNull( after, "after" ); AssertUtil.ArgumentNotNull( componentModel, "componentModel" ); AssertUtil.ArgumentNotNull( serv2handler, "serv2handler" ); m_before = before; m_after = after; m_componentModel = componentModel; m_serv2handler = serv2handler; } #region IComponentFactory Members public virtual Object Incarnate() { try { object[] arguments = BuildArguments(); Object instance = Activator.CreateInstance( m_componentModel.ConstructionModel.Implementation, arguments ); if (m_before.Length != 0 || m_after.Length != 0 ) { instance = DynamicProxy.ProxyGenerator.CreateProxy( new Type[] { m_componentModel.Service }, new Aspects.AspectInvocationHandler( m_before, m_after, instance ) ); } SetupProperties( instance ); return instance; } catch(Exception ex) { throw new HandlerException("Exception while attempting to instantiate type", ex); } } public virtual void Etherialize( object instance ) { // TODO: Obtain InstanceGarbageHolder and release it // See below (ResolveType) } #endregion protected virtual object[] BuildArguments() { return Assembler.BuildConstructorArguments( m_componentModel, new ResolveTypeHandler( ResolveType )); } protected virtual void SetupProperties( object instance ) { Assembler.AssembleProperties( instance, m_componentModel, new ResolveTypeHandler( ResolveType )); } private void ResolveType( IComponentModel model, Type typeRequest, String argumentOrPropertyName, out object value ) { value = null; if (m_serv2handler.ContainsKey( typeRequest )) { IHandler handler = (IHandler) m_serv2handler[ typeRequest ]; value = handler.Resolve(); // TODO: Enqueue handlers for dispose in Etherialize() // Criar um InstanceGarbageHolder or something like that. } } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Factory/IComponentFactory.cs Index: IComponentFactory.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel { using System; using System.Reflection; /// <summary> /// Summary description for IComponentFactory. /// </summary> public interface IComponentFactory { Object Incarnate( ); void Etherialize( object instance ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/Default/DefaultComponentModel.cs Index: DefaultComponentModel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model.Default { using System; using Apache.Avalon.Framework; using Apache.Avalon.Castle.MicroKernel.Model; /// <summary> /// Summary description for DefaultComponentModel. /// </summary> public class DefaultComponentModel : IComponentModel { private Type m_service; private Lifestyle m_lifestyle; private ILogger m_logger; private IConfiguration m_config; private IContext m_context; private IDependencyModel[] m_dependencies; private IConstructionModel m_constructionModel; public DefaultComponentModel( Type service, Lifestyle lifestyle, ILogger logger, IConfiguration configuration, IContext context, IDependencyModel[] dependencies, IConstructionModel constructionModel) { AssertUtil.ArgumentNotNull( service, "service" ); AssertUtil.ArgumentNotNull( logger, "logger" ); AssertUtil.ArgumentNotNull( configuration, "configuration" ); AssertUtil.ArgumentNotNull( context, "context" ); AssertUtil.ArgumentNotNull( dependencies, "dependencies" ); AssertUtil.ArgumentNotNull( constructionModel, "constructionModel" ); m_service = service; m_lifestyle = lifestyle; m_logger = logger; m_config = configuration; m_context = context; m_dependencies = dependencies; m_constructionModel = constructionModel; } #region IComponentModel Members public Type Service { get { return m_service; } } public Lifestyle SupportedLifestyle { get { return m_lifestyle; } } public ILogger Logger { get { return m_logger; } } public IConfiguration Configuration { get { return m_config; } } public IContext Context { get { return m_context; } } public IDependencyModel[] Dependencies { get { return m_dependencies; } } public IConstructionModel ConstructionModel { get { return m_constructionModel; } } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/Default/DefaultComponentModelBuilder.cs Index: DefaultComponentModelBuilder.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model.Default { using System; using System.Collections; using System.Reflection; using Apache.Avalon.Framework; /// <summary> /// Summary description for DefaultComponentModelBuilder. /// </summary> public class DefaultComponentModelBuilder : IComponentModelBuilder { private Kernel m_kernel; public DefaultComponentModelBuilder(Kernel kernel) { m_kernel = kernel; } #region IComponentModelBuilder Members public IComponentModel BuildModel(String key, Type service, Type implementation) { // TODO: This code sucks. Refactor it! ArrayList dependencies = new ArrayList(); ConstructorInfo constructor = InspectConstructors(implementation, dependencies); PropertyInfo[] properties = InspectSetMethods(service, dependencies); InspectAvalonAttributes( implementation, dependencies ); IDependencyModel[] dependenciesArray = (IDependencyModel[]) dependencies.ToArray( typeof(IDependencyModel) ); IConstructionModel constructionModel = new DefaultConstructionModel( implementation, constructor, properties ); // TODO: Consoler. Context and Configuration should be created by // a separated entity - how to reach it? Kernel? DefaultComponentModel model = new DefaultComponentModel( service, Avalon.Framework.Lifestyle.Transient, new ConsoleLogger( service.Name, LoggerLevel.Debug ), new DefaultConfiguration(), new DefaultContext(), dependenciesArray, constructionModel ); return model; } #endregion protected void InspectAvalonAttributes( Type implementation, IList dependencies ) { if (!implementation.IsDefined( typeof(AvalonComponentAttribute), false )) { return; } AvalonComponentAttribute componentAttribute = GetComponentAttribute( implementation ); AvalonLoggerAttribute loggerAttribute = GetLoggerAttribute( implementation ); AvalonDependencyAttribute[] dependencyAttributes = GetDependencyAttributes( implementation ); foreach( AvalonDependencyAttribute dependency in dependencyAttributes ) { AddDependency( dependencies, dependency.DependencyType, dependency.Key, dependency.IsOptional ); } } protected AvalonComponentAttribute GetComponentAttribute( Type implementation ) { return (AvalonComponentAttribute) GetAttribute( implementation, typeof( AvalonComponentAttribute ) ); } protected AvalonDependencyAttribute[] GetDependencyAttributes( Type implementation ) { return (AvalonDependencyAttribute[]) GetAttributes( implementation, typeof( AvalonDependencyAttribute ) ); } protected AvalonLoggerAttribute GetLoggerAttribute( Type implementation ) { return (AvalonLoggerAttribute) GetAttribute( implementation, typeof( AvalonLoggerAttribute ) ); } protected object GetAttribute( Type implementation, Type attribute ) { object[] attrs = implementation.GetCustomAttributes( attribute, false ); if (attrs.Length != 0) { return attrs[0]; } return null; } protected object[] GetAttributes( Type implementation, Type attribute ) { return implementation.GetCustomAttributes( attribute, false ); } protected ConstructorInfo InspectConstructors( Type implementation, IList dependencies ) { ConstructorInfo constructor = null; ConstructorInfo[] constructors = implementation.GetConstructors(); // TODO: Try to sort the array // by the arguments lenght in descendent order foreach(ConstructorInfo item in constructors) { if (IsEligible( item )) { constructor = item; ParameterInfo[] parameters = constructor.GetParameters(); foreach(ParameterInfo parameter in parameters) { if (!parameter.ParameterType.IsInterface) { continue; } AddDependency( dependencies, parameter.ParameterType ); } break; } } if ( constructor == null ) { throw new ModelBuilderException( String.Format("Handler could not find an eligible constructor for type {0}", implementation.FullName) ); } return constructor; } protected PropertyInfo[] InspectSetMethods( Type service, IList dependencies ) { ArrayList selected = new ArrayList(); PropertyInfo[] properties = service.GetProperties(); foreach(PropertyInfo property in properties) { if (IsEligible( property )) { AddDependency( dependencies, property.PropertyType ); selected.Add( property ); } } return (PropertyInfo[]) selected.ToArray( typeof(PropertyInfo) ); } protected bool IsEligible( PropertyInfo property ) { // TODO: An attribute could say to use // that the property is optional. if (!property.CanWrite || !property.PropertyType.IsInterface) { return false; } return true; } protected bool IsEligible( ConstructorInfo constructor ) { ParameterInfo[] parameters = constructor.GetParameters(); foreach(ParameterInfo parameter in parameters) { if (parameter.ParameterType == typeof(String) && !parameter.Name.Equals("contextdir")) // Just a sample { return false; } if (!parameter.ParameterType.IsInterface) { return false; } } return true; } protected virtual void AddDependency( IList dependencies, Type type, String key, bool optional ) { if (type == typeof(ILogger) || type == typeof(IContext) || type == typeof(IConfiguration) || type == typeof(ILookupManager)) { return; } DefaultDependencyModel dependecy = new DefaultDependencyModel( type, key, optional ); if (!dependencies.Contains( dependecy )) { dependencies.Add( dependecy ); } } protected virtual void AddDependency( IList dependencies, Type type ) { AddDependency( dependencies, type, String.Empty, false ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/Default/DefaultConstructionModel.cs Index: DefaultConstructionModel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model.Default { using System; using System.Reflection; /// <summary> /// Summary description for DefaultConstructionModel. /// </summary> public class DefaultConstructionModel : IConstructionModel { private Type m_implementation; private ConstructorInfo m_constructor; private PropertyInfo[] m_properties; public DefaultConstructionModel( Type implementation, ConstructorInfo constructor, PropertyInfo[] properties ) { AssertUtil.ArgumentNotNull( implementation, "implementation" ); AssertUtil.ArgumentNotNull( constructor, "constructor" ); AssertUtil.ArgumentNotNull( properties, "properties" ); m_implementation = implementation; m_constructor = constructor; m_properties = properties; } #region IConstructionModel Members public Type Implementation { get { return m_implementation; } } public ConstructorInfo SelectedConstructor { get { return m_constructor; } } public PropertyInfo[] SelectedProperties { get { return m_properties; } } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/Default/DefaultDependencyModel.cs Index: DefaultDependencyModel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model.Default { using System; /// <summary> /// Summary description for DefaultDependencyModel. /// </summary> public class DefaultDependencyModel : IDependencyModel { private Type m_service; private bool m_optional; private String m_key; /// <summary> /// /// </summary> /// <param name="service"></param> /// <param name="key"></param> /// <param name="optional"></param> public DefaultDependencyModel( Type service, String key, bool optional ) { AssertUtil.ArgumentNotNull( service, "service" ); m_service = service; m_optional = optional; m_key = key; } #region IDependencyModel Members public Type Service { get { return m_service; } } public String LookupKey { get { return m_key; } } public bool Optional { get { return m_optional; } } #endregion public override bool Equals(object obj) { DefaultDependencyModel other = obj as DefaultDependencyModel; if (other == null) { return false; } return other.Service.Equals( m_service ); } public override int GetHashCode() { return m_service.GetHashCode(); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/IComponentModel.cs Index: IComponentModel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model { using System; using Apache.Avalon.Framework; /// <summary> /// Summary description for IComponentModel. /// </summary> public interface IComponentModel { Lifestyle SupportedLifestyle { get; } ILogger Logger { get; } IConfiguration Configuration { get; } IContext Context { get; } Type Service { get; } IDependencyModel[] Dependencies { get; } IConstructionModel ConstructionModel { get; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/IComponentModelBuilder.cs Index: IComponentModelBuilder.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model { using System; /// <summary> /// Summary description for IComponentModelBuilder. /// </summary> public interface IComponentModelBuilder { IComponentModel BuildModel( String key, Type service, Type implementation ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/IConstructionModel.cs Index: IConstructionModel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model { using System; using System.Reflection; /// <summary> /// Summary description for IConstructionModel. /// </summary> public interface IConstructionModel { Type Implementation { get; } ConstructorInfo SelectedConstructor { get; } PropertyInfo[] SelectedProperties { get; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/IDependencyModel.cs Index: IDependencyModel.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model { using System; /// <summary> /// Summary description for IDependencyModel. /// </summary> public interface IDependencyModel { Type Service { get; } String LookupKey { get; } bool Optional { get; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Model/ModelBuilderException.cs Index: ModelBuilderException.cs =================================================================== // Copyright 2004 The Apache Software Foundation // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Apache.Avalon.Castle.MicroKernel.Model { using System; /// <summary> /// Summary description for ModelBuilderException. /// </summary> [Serializable] public class ModelBuilderException : System.Exception { public ModelBuilderException() { } public ModelBuilderException(String message) : base(message) { } } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]