hammett 2004/03/30 16:43:31 Added: avalon-net/Castle/MicroKernel Apache.Avalon.Castle.MicroKernel.csproj AspectInvocationHandler.cs AspectPointCutFlags.cs AssemblyInfo.cs AssertUtil.cs BaseKernel.cs ConstructionInfo.cs HandlerException.cs IAspect.cs IComponentFactory.cs IHandler.cs IHandlerFactory.cs ILifestyleManager.cs ILifestyleManagerFactory.cs IResolver.cs Kernel.cs MicroKernel.sln State.cs avalon-net/Castle/MicroKernel/Factory SimpleComponentFactory.cs avalon-net/Castle/MicroKernel/Handlers SimpleHandler.cs SimpleHandlerFactory.cs avalon-net/Castle/MicroKernel/LifestyleManagers SimpleLifestyleManagerFactory.cs TransientLifestyleManager.cs avalon-net/Castle/MicroKernel/MicroKernelTest AssemblyInfo.cs BaseKernelTestCase.cs MicroKernelTest.csproj SimpleComponentFactoryTestCase.cs avalon-net/Castle/MicroKernel/MicroKernelTest/Components AvalonMailService.cs AvalonSpamService.cs IMailService.cs ISpamService.cs SimpleMailService.cs SimpleSpamService.cs Log: MicroKernel for Avalon Castle. - proposal. Revision Changes Path 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Apache.Avalon.Castle.MicroKernel.csproj Index: Apache.Avalon.Castle.MicroKernel.csproj =================================================================== <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "Apache.Avalon.Castle.MicroKernel" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "Apache.Avalon.Castle.MicroKernel" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "..\bin\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "bin\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> <Reference Name = "Apache.Avalon.DynamicProxy" AssemblyName = "Apache.Avalon.DynamicProxy" HintPath = "..\bin\Apache.Avalon.DynamicProxy.dll" /> </References> </Build> <Files> <Include> <File RelPath = "AspectInvocationHandler.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AspectPointCutFlags.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "AssertUtil.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "BaseKernel.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ConstructionInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "HandlerException.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IAspect.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IComponentFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IHandler.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IHandlerFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ILifestyleManager.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "ILifestyleManagerFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "IResolver.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Kernel.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "State.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Factory\SimpleComponentFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Handlers\SimpleHandler.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Handlers\SimpleHandlerFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "LifestyleManagers\SimpleLifestyleManagerFactory.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "LifestyleManagers\TransientLifestyleManager.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/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 { 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/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/AssemblyInfo.cs Index: AssemblyInfo.cs =================================================================== // Copyright 2004 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. using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyTitle("AvalonFramework")] [assembly: AssemblyDescription("Avalon Framework defines the lifecycle interfaces and contracts that all components must obey.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Apache Software Foundation")] [assembly: AssemblyProduct("Avalon Framework")] [assembly: AssemblyCopyright("Copyright (c) 2003 Apache Software Foundation")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: AssemblyVersion("1.0.0.0")] #if STRONG [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("../../../../ApacheAvalon.snk")] [assembly: AssemblyKeyName("")] #endif 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/AssertUtil.cs Index: AssertUtil.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 AssertUtil. /// </summary> internal class AssertUtil { private AssertUtil() { } public static void ArgumentNotNull( object argValue, String argName ) { if (argValue == null) { throw new ArgumentNullException(argName, String.Format("Argument {0} can't be null", argName) ); } } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/BaseKernel.cs Index: BaseKernel.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.Collections; using System.Collections.Specialized; using Apache.Avalon.DynamicProxy; /// <summary> /// Summary description for BaseKernel. /// </summary> public class BaseKernel : Kernel { protected ArrayList m_aspectBefore = new ArrayList(); protected ArrayList m_aspectAfter = new ArrayList(); protected ArrayList m_concerns = new ArrayList(); protected Hashtable m_components = new Hashtable(CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default); protected Hashtable m_services = new Hashtable(); protected Hashtable m_dependencyToSatisfy = new Hashtable(); protected IHandlerFactory m_handlerFactory = new Handlers.SimpleHandlerFactory(); protected ILifestyleManagerFactory m_lifestyleManagerFactory = new LifestyleManagers.SimpleLifestyleManagerFactory(); /// <summary> /// /// </summary> public BaseKernel() { } #region Kernel Members /// <summary> /// /// </summary> /// <param name="key"></param> /// <param name="service"></param> /// <param name="implementation"></param> public void AddComponent( String key, Type service, Type implementation ) { AssertUtil.ArgumentNotNull( key, "key" ); AssertUtil.ArgumentNotNull( service, "service" ); AssertUtil.ArgumentNotNull( implementation, "implementation" ); if (!service.IsInterface) { throw new ArgumentException("service must be an interface"); } if (implementation.IsInterface) { throw new ArgumentException("implementation can't be an interface"); } if (!service.IsAssignableFrom(implementation)) { throw new ArgumentException("The specified implementation does not implement the service interface"); } IHandler handler = HandlerFactory.CreateHandler( service, implementation ); handler.Init ( this ); m_components[ key ] = handler; OnNewHandler( service, handler ); } /// <summary> /// /// </summary> /// <param name="flags"></param> /// <param name="aspect"></param> public void AddAspect( AspectPointCutFlags flags, IAspect aspect ) { AssertUtil.ArgumentNotNull( aspect, "aspect" ); if ((AspectPointCutFlags.Before & flags) != 0) { lock(m_aspectBefore) { m_aspectBefore.Add( aspect ); } } if ((AspectPointCutFlags.After & flags) != 0) { lock(m_aspectAfter) { m_aspectAfter.Add( aspect ); } } } /// <summary> /// /// </summary> public IHandler this [ String key ] { get { return (IHandler) m_components[ key ]; } } public IHandlerFactory HandlerFactory { get { return m_handlerFactory; } set { AssertUtil.ArgumentNotNull( value, "value" ); m_handlerFactory = value; } } /// <summary> /// /// </summary> public ILifestyleManagerFactory LifestyleManagerFactory { get { return m_lifestyleManagerFactory; } set { AssertUtil.ArgumentNotNull( value, "value" ); m_lifestyleManagerFactory = value; } } public IAspect[] GetAspects( AspectPointCutFlags pointcut ) { if (pointcut == AspectPointCutFlags.Before) { return (IAspect[]) m_aspectBefore.ToArray( typeof(IAspect) ); } else if (pointcut == AspectPointCutFlags.After) { return (IAspect[]) m_aspectAfter.ToArray( typeof(IAspect) ); } return new IAspect[0]; } /// <summary> /// /// </summary> /// <param name="service"></param> /// <returns></returns> public bool HasService( Type service ) { return m_services.Contains( service ); } /// <summary> /// /// </summary> /// <param name="service"></param> /// <param name="depDelegate"></param> public void AddDependencyListener( Type service, DependencyListenerDelegate depDelegate ) { lock(m_dependencyToSatisfy) { Delegate del = m_dependencyToSatisfy[ service ] as Delegate; if (del == null) { m_dependencyToSatisfy[ service ] = depDelegate; } else { del = Delegate.Combine( del, depDelegate ); m_dependencyToSatisfy[ service ] = del; } } } /// <summary> /// /// </summary> /// <param name="service"></param> /// <returns></returns> public IHandler GetHandlerForService( Type service ) { return (IHandler) m_services[ service ]; } #endregion private void OnNewHandler( Type service, IHandler handler ) { m_services[ service ] = handler; lock(m_dependencyToSatisfy) { if (!m_dependencyToSatisfy.Contains( service )) { return; } DependencyListenerDelegate del = (DependencyListenerDelegate) m_dependencyToSatisfy[ service ]; del( service, handler ); } } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/ConstructionInfo.cs Index: ConstructionInfo.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.Collections; using System.Reflection; /// <summary> /// Summary description for ConstructionInfo. /// </summary> public class ConstructionInfo { private ConstructorInfo m_constructor; private Hashtable m_service2handler; public ConstructionInfo(ConstructorInfo constructor, Hashtable service2handler) { m_constructor = constructor; m_service2handler = service2handler; } public ConstructorInfo Constructor { get { return m_constructor; } } public IHandler this [ Type service ] { get { return (IHandler) m_service2handler[ service ]; } } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/HandlerException.cs Index: HandlerException.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 HandlerException. /// </summary> [Serializable] public class HandlerException : System.Exception { public HandlerException( String message ) : base(message) { } public HandlerException( String message, Exception ex ) : base(message, ex) { } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/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/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( ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/IHandler.cs Index: IHandler.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 IHandler. /// </summary> public interface IHandler : IResolver { /// <summary> /// /// </summary> /// <param name="kernel"></param> void Init( Kernel kernel ); /// <summary> /// /// </summary> State ActualState { get; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/IHandlerFactory.cs Index: IHandlerFactory.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 IHandlerFactory. /// </summary> public interface IHandlerFactory { IHandler CreateHandler( Type service, Type implementation ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/ILifestyleManager.cs Index: ILifestyleManager.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 ILifestyleManager. /// </summary> public interface ILifestyleManager : IResolver { } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/ILifestyleManagerFactory.cs Index: ILifestyleManagerFactory.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 ILifestyleManagerFactory. /// </summary> public interface ILifestyleManagerFactory { ILifestyleManager Create( IComponentFactory factory ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/IResolver.cs Index: IResolver.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 IResolver. /// </summary> public interface IResolver { object Resolve(); void Release(); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Kernel.cs Index: Kernel.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; public delegate void DependencyListenerDelegate( Type service, IHandler handler ); /// <summary> /// Defines the Kernel service /// </summary> public interface Kernel { /// <summary> /// Adds a component to kernel. /// </summary> /// <param name="key">The unique key that identifies the component</param> /// <param name="service">The service exposed by this component</param> /// <param name="implementation">The actual implementation</param> void AddComponent( String key, Type service, Type implementation ); /// <summary> /// Adds an aspect to kernel. /// </summary> /// <param name="flags">May be Before, After or both.</param> /// <param name="aspect">The aspect implementation</param> void AddAspect( AspectPointCutFlags flags, IAspect aspect ); /// <summary> /// Obtains a component handler using the /// unique component key /// </summary> IHandler this [ String key ] { get; } /// <summary> /// Gets or Sets the IHandlerFactory implementation /// </summary> IHandlerFactory HandlerFactory { get; set; } /// <summary> /// Gets or Sets the ILifestyleManagerFactory implementation /// </summary> ILifestyleManagerFactory LifestyleManagerFactory { get; set; } /// <summary> /// Returns an array of aspects /// interested in a specific point cut /// </summary> /// <param name="pointcut"></param> /// <returns></returns> IAspect[] GetAspects( AspectPointCutFlags pointcut ); /// <summary> /// Returns true if kernel "knows" the specified /// service /// </summary> /// <param name="service">The service interface</param> /// <returns>true if is already registered</returns> bool HasService( Type service ); /// <summary> /// Used by handlers to register itself as /// and dependency to be satisfied. /// </summary> /// <param name="service">The service interface</param> /// <param name="depDelegate">Delegate to be invoked</param> void AddDependencyListener( Type service, DependencyListenerDelegate depDelegate ); /// <summary> /// Returns a IHandler implementation for /// the specified service /// </summary> /// <param name="service">The service interface</param> /// <returns>IHandler implementation</returns> IHandler GetHandlerForService( Type service ); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernel.sln Index: MicroKernel.sln =================================================================== Microsoft Visual Studio Solution File, Format Version 8.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Apache.Avalon.Castle.MicroKernel", "Apache.Avalon.Castle.MicroKernel.csproj", "{1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}" ProjectSection(ProjectDependencies) = postProject EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MicroKernelTest", "MicroKernelTest\MicroKernelTest.csproj", "{50442F0D-987F-4A8D-B38C-DFA855B9249E}" ProjectSection(ProjectDependencies) = postProject EndProjectSection EndProject Global GlobalSection(SolutionConfiguration) = preSolution Debug = Debug Release = Release EndGlobalSection GlobalSection(ProjectConfiguration) = postSolution {1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}.Debug.ActiveCfg = Debug|.NET {1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}.Debug.Build.0 = Debug|.NET {1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}.Release.ActiveCfg = Release|.NET {1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}.Release.Build.0 = Release|.NET {50442F0D-987F-4A8D-B38C-DFA855B9249E}.Debug.ActiveCfg = Debug|.NET {50442F0D-987F-4A8D-B38C-DFA855B9249E}.Debug.Build.0 = Debug|.NET {50442F0D-987F-4A8D-B38C-DFA855B9249E}.Release.ActiveCfg = Release|.NET {50442F0D-987F-4A8D-B38C-DFA855B9249E}.Release.Build.0 = Release|.NET EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution EndGlobalSection GlobalSection(ExtensibilityAddIns) = postSolution EndGlobalSection EndGlobal 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/State.cs Index: State.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 State. /// </summary> public enum State { /// <summary> /// /// </summary> Valid, /// <summary> /// /// </summary> WaitingDependency } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Factory/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 { using System; using System.Reflection; /// <summary> /// Summary description for SimpleComponentFactory. /// </summary> public class SimpleComponentFactory : IComponentFactory { protected Type m_service; protected Type m_implementation; protected IAspect[] m_before; protected IAspect[] m_after; protected ConstructionInfo m_info; public SimpleComponentFactory( Type service, Type implementation, IAspect[] before, IAspect[] after, ConstructionInfo info ) { AssertUtil.ArgumentNotNull( service, "service" ); AssertUtil.ArgumentNotNull( implementation, "implementation" ); AssertUtil.ArgumentNotNull( before, "before" ); AssertUtil.ArgumentNotNull( after, "after" ); AssertUtil.ArgumentNotNull( info, "info" ); m_service = service; m_implementation = implementation; m_before = before; m_after = after; m_info = info; } #region IComponentFactory Members public Object Incarnate() { try { object[] arguments = BuildArguments(); Object instance = Activator.CreateInstance( m_implementation, arguments ); if (m_before.Length != 0 || m_after.Length != 0 ) { instance = DynamicProxy.ProxyGenerator.CreateProxy( new Type[] { m_service }, new AspectInvocationHandler( m_before, m_after, instance ) ); } return instance; } catch(Exception ex) { throw new HandlerException("Exception while attempting to instantiate type", ex); } } public void Etherialize() { } #endregion protected object[] BuildArguments() { // TODO: Enqueue handlers for dispose in Etherialize() ParameterInfo[] parameters = m_info.Constructor.GetParameters(); object[] args = new object[ parameters.Length ]; for(int i=0; i < args.Length; i++) { ParameterInfo parameter = parameters[i]; Type service = parameter.ParameterType; IHandler handler = m_info[ service ]; args[ parameter.Position ] = handler.Resolve(); } return args; } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Handlers/SimpleHandler.cs Index: SimpleHandler.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.Handlers { using System; using System.Collections; using System.Reflection; /// <summary> /// Summary description for SimpleHandler. /// </summary> public class SimpleHandler : IHandler { protected Type m_service; protected Type m_implementation; protected Kernel m_kernel; private State m_state = State.Valid; private ConstructorInfo m_constructor; private ArrayList m_dependencies = new ArrayList(); private Hashtable m_serv2handler = new Hashtable(); private ILifestyleManager m_lifestyleManager; /// <summary> /// /// </summary> /// <param name="service"></param> /// <param name="implementation"></param> public SimpleHandler(Type service, Type implementation) { AssertUtil.ArgumentNotNull( service, "service" ); AssertUtil.ArgumentNotNull( implementation, "implementation" ); m_service = service; m_implementation = implementation; } #region IHandler Members public void Init( Kernel kernel ) { m_kernel = kernel; ConstructorInfo[] constructors = m_implementation.GetConstructors(); // TODO: Try to sort the array // by the arguments lenght in descendent order // TODO: Should also support SetMethods foreach(ConstructorInfo constructor in constructors) { if (IsEligible( constructor )) { m_constructor = constructor; break; } } if ( m_constructor == null ) { throw new HandlerException( String.Format("Handler could not find an eligible constructor for type {0}", m_implementation.FullName) ); } // Now we check with the kernel if // we have the necessary implementations // for the services requested by the constructor EnsureHaveRequiredImplementations(); CreateComponentFactoryAndLifestyleManager(); } public object Resolve() { if (m_state == State.WaitingDependency) { throw new HandlerException("Can't Resolve component. It has dependencies to be satisfied."); } try { return m_lifestyleManager.Resolve(); } catch(Exception ex) { throw new HandlerException("Exception while attempting to instantiate type", ex); } } public void Release() { m_lifestyleManager.Release(); } /// <summary> /// /// </summary> public State ActualState { get { return m_state; } } #endregion protected bool IsEligible( ConstructorInfo constructor ) { ParameterInfo[] parameters = constructor.GetParameters(); foreach(ParameterInfo parameter in parameters) { if (parameter.ParameterType == typeof(String) && !parameter.Name.Equals("contextdir")) // Just as sample { return false; } if (!parameter.ParameterType.IsInterface) { return false; } } return true; } protected void EnsureHaveRequiredImplementations() { ParameterInfo[] parameters = m_constructor.GetParameters(); foreach(ParameterInfo parameter in parameters) { if (parameter.ParameterType.IsInterface) { if (m_kernel.HasService( parameter.ParameterType )) { m_serv2handler[parameter.ParameterType] = m_kernel.GetHandlerForService( parameter.ParameterType ); } else { m_kernel.AddDependencyListener( parameter.ParameterType, new DependencyListenerDelegate(DependencySatisfied) ); m_state = State.WaitingDependency; m_dependencies.Add( parameter.ParameterType ); } } } } private void DependencySatisfied( Type service, IHandler handler ) { m_serv2handler[ service ] = handler; m_dependencies.Remove( service ); if (m_dependencies.Count == 0) { m_state = State.Valid; } } private void CreateComponentFactoryAndLifestyleManager() { ConstructionInfo info = new ConstructionInfo( m_constructor, m_serv2handler ); IComponentFactory factory = new Factory.SimpleComponentFactory( m_service, m_implementation, m_kernel.GetAspects(AspectPointCutFlags.Before), m_kernel.GetAspects(AspectPointCutFlags.After), info); m_lifestyleManager = m_kernel.LifestyleManagerFactory.Create( factory ); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/Handlers/SimpleHandlerFactory.cs Index: SimpleHandlerFactory.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.Handlers { using System; /// <summary> /// Summary description for SimpleHandlerFactory. /// </summary> public class SimpleHandlerFactory : IHandlerFactory { public SimpleHandlerFactory() { } #region IHandlerFactory Members public IHandler CreateHandler(Type service, Type implementation) { SimpleHandler handler = new SimpleHandler( service, implementation ); return handler; } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/LifestyleManagers/SimpleLifestyleManagerFactory.cs Index: SimpleLifestyleManagerFactory.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.LifestyleManagers { using System; /// <summary> /// Summary description for SimpleLifestyleManagerFactory. /// </summary> public class SimpleLifestyleManagerFactory : ILifestyleManagerFactory { public SimpleLifestyleManagerFactory() { } #region ILifestyleManagerFactory Members public ILifestyleManager Create(IComponentFactory factory) { return new TransientLifestyleManager( factory ); } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/LifestyleManagers/TransientLifestyleManager.cs Index: TransientLifestyleManager.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.LifestyleManagers { using System; /// <summary> /// Summary description for TransientLifestyleManager. /// </summary> public class TransientLifestyleManager : ILifestyleManager { private IComponentFactory m_componentFactory; public TransientLifestyleManager(IComponentFactory componentFactory) { m_componentFactory = componentFactory; } #region IResolver Members public object Resolve() { return m_componentFactory.Incarnate(); } public void Release() { m_componentFactory.Etherialize(); } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/AssemblyInfo.cs Index: AssemblyInfo.cs =================================================================== // Copyright 2004 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. using System.Reflection; using System.Runtime.CompilerServices; [assembly: AssemblyTitle("AvalonFramework")] [assembly: AssemblyDescription("Avalon Framework defines the lifecycle interfaces and contracts that all components must obey.")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("Apache Software Foundation")] [assembly: AssemblyProduct("Avalon Framework")] [assembly: AssemblyCopyright("Copyright (c) 2003 Apache Software Foundation")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] [assembly: AssemblyVersion("1.0.0.0")] #if STRONG [assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile("../../../../ApacheAvalon.snk")] [assembly: AssemblyKeyName("")] #endif 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/BaseKernelTestCase.cs Index: BaseKernelTestCase.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.Test { using System; using System.Reflection; using System.Collections; using NUnit.Framework; using Apache.Avalon.Castle.MicroKernel; using Apache.Avalon.Castle.MicroKernel.Test.Components; /// <summary> /// Summary description for BaseKernelTestCase. /// </summary> [TestFixture] public class BaseKernelTestCase : Assertion { /// <summary> /// Just a simple Service resolution. /// No concerns or aspects involved. /// </summary> [Test] public void SimpleUsage() { BaseKernel container = new BaseKernel(); container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) ); IHandler handler = container[ "a" ]; IMailService service = handler.Resolve() as IMailService; AssertNotNull( service ); service.Send("hammett at apache dot org", "johndoe at yahoo dot org", "Aloha!", "What's up?"); handler.Release( ); } /// <summary> /// Adds a InterceptionAspect to calling chain and check if is has been called /// during method execution /// </summary> [Test] public void AddingAspect() { BaseKernel container = new BaseKernel(); InterceptionAspect aspect = new InterceptionAspect(); container.AddAspect( AspectPointCutFlags.Before|AspectPointCutFlags.After, aspect ); container.AddComponent( "a", typeof(IMailService), typeof(SimpleMailService) ); IHandler handler = container[ "a" ]; IMailService service = handler.Resolve() as IMailService; AssertNotNull( service ); service.Send("hammett at apache dot org", "johndoe at yahoo dot org", "Aloha!", "What's up?"); handler.Release( ); AssertEquals( 2, aspect.m_invocations.Count ); AssertEquals( "Before Send", aspect.m_invocations[0] ); AssertEquals( "After Send", aspect.m_invocations[1] ); } public class InterceptionAspect : IAspect { public ArrayList m_invocations = new ArrayList(); public void Perform( AspectPointCutFlags pointcut, object componentInstance, MethodBase method, object returnValue, Exception exception, params object[] arguments) { if (pointcut == AspectPointCutFlags.Before) { m_invocations.Add("Before " + method.Name); } else if (pointcut == AspectPointCutFlags.After) { m_invocations.Add("After " + method.Name); } } } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/MicroKernelTest.csproj Index: MicroKernelTest.csproj =================================================================== <VisualStudioProject> <CSHARP ProjectType = "Local" ProductVersion = "7.10.3077" SchemaVersion = "2.0" ProjectGuid = "{50442F0D-987F-4A8D-B38C-DFA855B9249E}" > <Build> <Settings ApplicationIcon = "" AssemblyKeyContainerName = "" AssemblyName = "Apache.Avalon.Castle.MicroKernel.Test" AssemblyOriginatorKeyFile = "" DefaultClientScript = "JScript" DefaultHTMLPageLayout = "Grid" DefaultTargetSchema = "IE50" DelaySign = "false" OutputType = "Library" PreBuildEvent = "" PostBuildEvent = "" RootNamespace = "Apache.Avalon.Castle.MicroKernel.Test" RunPostBuildEvent = "OnBuildSuccess" StartupObject = "" > <Config Name = "Debug" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "DEBUG;TRACE" DocumentationFile = "" DebugSymbols = "true" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "false" OutputPath = "..\..\bin\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> <Config Name = "Release" AllowUnsafeBlocks = "false" BaseAddress = "285212672" CheckForOverflowUnderflow = "false" ConfigurationOverrideFile = "" DefineConstants = "TRACE" DocumentationFile = "" DebugSymbols = "false" FileAlignment = "4096" IncrementalBuild = "false" NoStdLib = "false" NoWarn = "" Optimize = "true" OutputPath = "bin\Release\" RegisterForComInterop = "false" RemoveIntegerChecks = "false" TreatWarningsAsErrors = "false" WarningLevel = "4" /> </Settings> <References> <Reference Name = "System" AssemblyName = "System" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.dll" /> <Reference Name = "System.Data" AssemblyName = "System.Data" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.Data.dll" /> <Reference Name = "System.XML" AssemblyName = "System.Xml" HintPath = "C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\System.XML.dll" /> <Reference Name = "nunit.framework" AssemblyName = "nunit.framework" HintPath = "..\..\..\..\..\..\..\..\dotnet\NUnit2\bin\nunit.framework.dll" /> <Reference Name = "MicroKernel" Project = "{1E57B734-BA4B-4ADE-B4C2-78C7D4993AD4}" Package = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" /> <Reference Name = "Apache.Avalon.Framework" AssemblyName = "Apache.Avalon.Framework" HintPath = "..\..\bin\Apache.Avalon.Framework.dll" /> </References> </Build> <Files> <Include> <File RelPath = "AssemblyInfo.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "BaseKernelTestCase.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "SimpleComponentFactoryTestCase.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Components\AvalonMailService.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Components\AvalonSpamService.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Components\IMailService.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Components\ISpamService.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Components\SimpleMailService.cs" SubType = "Code" BuildAction = "Compile" /> <File RelPath = "Components\SimpleSpamService.cs" SubType = "Code" BuildAction = "Compile" /> </Include> </Files> </CSHARP> </VisualStudioProject> 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/SimpleComponentFactoryTestCase.cs Index: SimpleComponentFactoryTestCase.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.Test { using System; using System.Reflection; using System.Collections; using NUnit.Framework; using Apache.Avalon.Castle.MicroKernel; using Apache.Avalon.Castle.MicroKernel.Factory; using Apache.Avalon.Castle.MicroKernel.Test.Components; /// <summary> /// Summary description for SimpleComponentFactoryTestCase. /// </summary> [TestFixture] public class SimpleComponentFactoryTestCase : Assertion { BaseKernel kernel; [SetUp] public void CreateKernel() { kernel = new BaseKernel(); } [Test] public void NoArgumentsConstructor() { Type service = typeof( IMailService ); Type implementation = typeof( SimpleMailService ); kernel.AddComponent( "a", service, implementation ); ConstructorInfo constructor = implementation.GetConstructor( Type.EmptyTypes ); ConstructionInfo info = new ConstructionInfo( constructor, null ); SimpleComponentFactory factory = new SimpleComponentFactory( service, implementation, new IAspect[0], new IAspect[0], info ); Object instance = factory.Incarnate(); AssertNotNull( instance ); AssertNotNull( instance as IMailService ); factory.Etherialize(); } [Test] public void DependencyInConstructor() { Type service = typeof( ISpamService ); Type implementation = typeof( SimpleSpamService ); Type serviceDep = typeof( IMailService ); Type implementationDep = typeof( SimpleMailService ); kernel.AddComponent( "a", service, implementation ); kernel.AddComponent( "b", serviceDep, implementationDep ); ConstructorInfo constructor = implementation.GetConstructor( new Type[] { typeof(IMailService) } ); Hashtable serv2Handler = new Hashtable(); serv2Handler[ serviceDep ] = kernel.GetHandlerForService( serviceDep ); ConstructionInfo info = new ConstructionInfo( constructor, serv2Handler ); SimpleComponentFactory factory = new SimpleComponentFactory( service, implementation, new IAspect[0], new IAspect[0], info ); Object instance = factory.Incarnate(); AssertNotNull( instance ); AssertNotNull( instance as ISpamService ); SimpleSpamService spamService = (SimpleSpamService) instance; AssertNotNull( spamService.m_mailService ); factory.Etherialize(); } } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/Components/AvalonMailService.cs Index: AvalonMailService.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.Test.Components { using System; using Apache.Avalon.Framework; /// <summary> /// Summary description for AvalonMailService. /// </summary> public class AvalonMailService : IMailService, IInitializable, IConfigurable, IDisposable { public AvalonMailService() { } #region IMailService Members public void Send(String from, String to, String subject, String message) { } #endregion #region IInitializable Members public void Initialize() { } #endregion #region IConfigurable Members public void Configure(IConfiguration config) { } #endregion #region IDisposable Members public void Dispose() { } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/Components/AvalonSpamService.cs Index: AvalonSpamService.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.Test.Components { using System; using Apache.Avalon.Framework; /// <summary> /// Summary description for AvalonSpamService. /// </summary> [AvalonComponent("spamservice", Lifestyle.Singleton)] [AvalonService( typeof(ISpamService) )] [AvalonDependency( typeof(IMailService), "mailservice", Optional.False) ] public class AvalonSpamService : ISpamService, ILookupEnabled { public IMailService m_mailService; public AvalonSpamService() { } #region ISpamService Members public void AnnoyPeople(String contents) { // TODO: Add AvalonSpamService.AnnoyPeople implementation } #endregion #region ILookupEnabled Members public void EnableLookups(ILookupManager manager) { m_mailService = (IMailService) manager[ "mailservice" ]; } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/Components/IMailService.cs Index: IMailService.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.Test.Components { using System; /// <summary> /// Contract for a MailService /// </summary> public interface IMailService { /// <summary> /// Sends an email. /// </summary> /// <param name="from">From</param> /// <param name="to">To</param> /// <param name="subject">Message's subject</param> /// <param name="message">Message's contents</param> void Send(String from, String to, String subject, String message); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/Components/ISpamService.cs Index: ISpamService.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.Test.Components { using System; /// <summary> /// Summary description for ISpamService. /// </summary> public interface ISpamService { /// <summary> /// /// </summary> /// <param name="contents"></param> void AnnoyPeople(String contents); } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/Components/SimpleMailService.cs Index: SimpleMailService.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.Test.Components { using System; /// <summary> /// Summary description for SimpleMailService. /// </summary> public class SimpleMailService : IMailService { public SimpleMailService() { } #region IMailService Members public void Send(String from, String to, String subject, String message) { } #endregion } } 1.1 avalon-sandbox/avalon-net/Castle/MicroKernel/MicroKernelTest/Components/SimpleSpamService.cs Index: SimpleSpamService.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.Test.Components { using System; /// <summary> /// Summary description for SimpleSpamService. /// </summary> public class SimpleSpamService : ISpamService { public IMailService m_mailService; public SimpleSpamService(IMailService mailService) { m_mailService = mailService; } #region ISpamService Members public void AnnoyPeople(String contents) { } #endregion } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]