[cneuwirt commit: 4857]: /trunk/InversionOfControl/

2 views
Skip to first unread message

notifi...@castleproject.org

unread,
Feb 28, 2008, 9:01:12 PM2/28/08
to castle-proj...@googlegroups.com
User: cneuwirt
Date: 2008/02/28 07:01 PM

Added:
/trunk/InversionOfControl/Castle.MicroKernel/Registration/
IRegistration.cs

Removed:
/trunk/InversionOfControl/Castle.MicroKernel/Registration/
IComponentRegistration.cs

Modified:
/trunk/InversionOfControl/
Changes.txt
/trunk/InversionOfControl/Castle.MicroKernel/
Castle.MicroKernel-vs2005.csproj, DefaultKernel.cs, IKernel.cs
/trunk/InversionOfControl/Castle.MicroKernel/Registration/
AttributeDescriptor.cs, ComponentDescriptor.cs, ComponentRegistration.cs
/trunk/InversionOfControl/Castle.MicroKernel/Registration/Lifestyle/
LifestyleDescriptor.cs
/trunk/InversionOfControl/Castle.Windsor/
IWindsorContainer.cs, WindsorContainer.cs

Log:
Added IRegistration interface for generalized kernel registration. This will allow more specialized registrations (i.e all controllers in an assembly)

File Changes:

Directory: /trunk/InversionOfControl/Castle.MicroKernel/
========================================================

File [modified]: Castle.MicroKernel-vs2005.csproj
Delta lines: +1 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Castle.MicroKernel-vs2005.csproj 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/Castle.MicroKernel-vs2005.csproj 2008-02-29 02:01:12 UTC (rev 4857)
@@ -320,7 +320,7 @@
<Compile Include="Registration\Parameter.cs" />
<Compile Include="Registration\CustomDependencyDescriptor.cs" />
<Compile Include="Registration\ExtendedPropertiesDescriptor.cs" />
- <Compile Include="Registration\IComponentRegistration.cs" />
+ <Compile Include="Registration\IRegistration.cs" />
<Compile Include="Registration\Interceptor\InterceptorDescriptor.cs" />
<Compile Include="Registration\Interceptor\InterceptorGroup.cs" />
<Compile Include="Registration\Lifestyle\Custom.cs" />

File [modified]: DefaultKernel.cs
Delta lines: +2 -2
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/DefaultKernel.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/DefaultKernel.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -551,14 +551,14 @@
/// <param name="registrations">The component registrations.</param>
/// <returns>The kernel.</returns>
/// </summary>
- public IKernel Register(params IComponentRegistration[] registrations)
+ public IKernel Register(params IRegistration[] registrations)
{
if (registrations == null)
{
throw new ArgumentNullException("registrations");
}

- foreach(IComponentRegistration registration in registrations)
+ foreach(IRegistration registration in registrations)
{
registration.Register(this);
}

File [modified]: IKernel.cs
Delta lines: +1 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/IKernel.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/IKernel.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -304,7 +304,7 @@
/// <param name="registrations">The component registrations.</param>
/// <returns>The kernel.</returns>
/// </summary>
- IKernel Register(params IComponentRegistration[] registrations);
+ IKernel Register(params IRegistration[] registrations);

/// <summary>
/// Returns true if the specified component was

Directory: /trunk/InversionOfControl/Castle.Windsor/
====================================================

File [modified]: IWindsorContainer.cs
Delta lines: +1 -1
===================================================================
--- trunk/InversionOfControl/Castle.Windsor/IWindsorContainer.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.Windsor/IWindsorContainer.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -204,7 +204,7 @@
/// <param name="registrations">The component registrations.</param>
/// <returns>The container.</returns>
/// </summary>
- IWindsorContainer Register(params IComponentRegistration[] registrations);
+ IWindsorContainer Register(params IRegistration[] registrations);

/// <summary>
/// Returns a component instance by the key

File [modified]: WindsorContainer.cs
Delta lines: +1 -1
===================================================================
--- trunk/InversionOfControl/Castle.Windsor/WindsorContainer.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.Windsor/WindsorContainer.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -482,7 +482,7 @@
/// <param name="registrations">The component registrations.</param>
/// <returns>The container.</returns>
/// </summary>
- public IWindsorContainer Register(params IComponentRegistration[] registrations)
+ public IWindsorContainer Register(params IRegistration[] registrations)
{
Kernel.Register(registrations);
return this;

Directory: /trunk/InversionOfControl/
=====================================

File [modified]: Changes.txt
Delta lines: +2 -0
===================================================================
--- trunk/InversionOfControl/Changes.txt 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Changes.txt 2008-02-29 02:01:12 UTC (rev 4857)
@@ -1,6 +1,8 @@
RC 4
====

+- Added IRegistration interface to allow alternate registration mechanisms.
+
- Fixed CORE-16 (Should be Facilities)
"The FactorySupportFacility does not create proxies if interceptors are present"

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/Lifestyle/
===============================================================================

File [modified]: LifestyleDescriptor.cs
Delta lines: +1 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Lifestyle/LifestyleDescriptor.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Lifestyle/LifestyleDescriptor.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -28,7 +28,7 @@

protected internal override void ApplyToConfiguration(IKernel kernel, IConfiguration configuration)
{
- if (configuration.Attributes["lifestyle"] == null || Registration.Overwrite)
+ if (configuration.Attributes["lifestyle"] == null || IsOverWrite)
{
configuration.Attributes["lifestyle"] = lifestyle.ToString();
ApplyLifestyleDetails(configuration);

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/
=====================================================================

File [modified]: AttributeDescriptor.cs
Delta lines: +1 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/AttributeDescriptor.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/AttributeDescriptor.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -30,7 +30,7 @@

protected internal override void ApplyToConfiguration(IKernel kernel, IConfiguration configuration)
{
- if (configuration.Attributes[name] == null || Registration.Overwrite)
+ if (configuration.Attributes[name] == null || IsOverWrite)
{
configuration.Attributes[name] = value;
}

File [modified]: ComponentDescriptor.cs
Delta lines: +10 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentDescriptor.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentDescriptor.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -21,12 +21,17 @@
{
private ComponentRegistration<S> registration;

- internal ComponentRegistration<S> Registration
+ internal protected ComponentRegistration<S> Registration
{
get { return registration; }
set { registration = value; }
}

+ internal protected bool IsOverWrite
+ {
+ get { return registration.IsOverWrite; }
+ }
+
protected internal virtual void ApplyToConfiguration(IKernel kernel, IConfiguration configuration)
{
}
@@ -35,4 +40,8 @@
{
}
}
+
+ public abstract class ComponentDescriptor : ComponentDescriptor<object>
+ {
+ }
}
\ No newline at end of file

File [modified]: ComponentRegistration.cs
Delta lines: +19 -4
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegistration.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegistration.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -25,11 +25,11 @@
///
/// </summary>
/// <typeparam name="S">The service type</typeparam>
- public class ComponentRegistration<S> : IComponentRegistration
+ public class ComponentRegistration<S> : IRegistration
{
private String name;
private bool overwrite;
- private readonly Type serviceType;
+ private Type serviceType;
private Type classType;
private readonly List<ComponentDescriptor<S>> descriptors;
private ComponentModel componentModel;
@@ -49,11 +49,16 @@
descriptors = new List<ComponentDescriptor<S>>();
}

- internal bool Overwrite
+ internal bool IsOverWrite
{
get { return overwrite; }
}

+ protected Type ServiceType
+ {
+ set { serviceType = value; }
+ }
+
/// <summary>
/// With the overwrite.
/// </summary>
@@ -253,7 +258,7 @@
/// Registers this component with the <see cref="IKernel"/>.
/// </summary>
/// <param name="kernel">The kernel.</param>
- void IComponentRegistration.Register(IKernel kernel)
+ void IRegistration.Register(IKernel kernel)
{
if (componentModel == null)
{
@@ -365,9 +370,19 @@

public class ComponentRegistration : ComponentRegistration<object>
{
+ public ComponentRegistration()
+ {
+ }
+
public ComponentRegistration(Type serviceType)
: base( serviceType )
{
}
+
+ public ComponentRegistration For(Type serviceType)
+ {
+ ServiceType = serviceType;
+ return this;
+ }
}
}

File [removed]: IComponentRegistration.cs
Delta lines: +0 -28
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/IComponentRegistration.cs 2008-02-28 20:47:20 UTC (rev 4856)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/IComponentRegistration.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -1,28 +0,0 @@
-// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
-//
-// 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 Castle.MicroKernel.Registration
-{
- /// <summary>
- /// The contract for all component registrations with the kernel.
- /// </summary>
- public interface IComponentRegistration
- {
- /// <summary>
- /// Registers this component with the <see cref="IKernel"/>.
- /// </summary>
- /// <param name="kernel">The kernel.</param>
- void Register(IKernel kernel);
- }
-}

File [added]: IRegistration.cs
Delta lines: +28 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/IRegistration.cs (rev 0)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/IRegistration.cs 2008-02-29 02:01:12 UTC (rev 4857)
@@ -0,0 +1,28 @@
+// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
+//
+// 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 Castle.MicroKernel.Registration
+{
+ /// <summary>
+ /// The contract for all registrations with the kernel.
+ /// </summary>
+ public interface IRegistration
+ {
+ /// <summary>
+ /// Performs the registration in the <see cref="IKernel"/>.
+ /// </summary>
+ /// <param name="kernel">The kernel.</param>
+ void Register(IKernel kernel);
+ }
+}

notifi...@castleproject.org

unread,
Mar 1, 2008, 11:49:01 AM3/1/08
to castle-proj...@googlegroups.com
User: cneuwirt
Date: 2008/03/01 09:49 AM

Added:
/trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
AllTypesOfTestCase.cs
/trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/
AllTypesOf.cs, ServiceDescriptor.cs, TypesDescriptor.cs

Modified:
/trunk/InversionOfControl/
Changes.txt
/trunk/InversionOfControl/Castle.MicroKernel.Tests/
Castle.MicroKernel.Tests-vs2008.csproj
/trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/
CustomerChain.cs
/trunk/InversionOfControl/Castle.MicroKernel/
Castle.MicroKernel-vs2008.csproj
/trunk/InversionOfControl/Castle.MicroKernel/Registration/
ComponentRegistration.cs

Log:
Added a generalized strategy to register components in the kernel. This allows you to capture some common resusable strategies for populating the container.

File Changes:

Directory: /trunk/InversionOfControl/Castle.MicroKernel/
========================================================

File [modified]: Castle.MicroKernel-vs2008.csproj
Delta lines: +3 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Castle.MicroKernel-vs2008.csproj 2008-03-01 16:44:27 UTC (rev 4860)
+++ trunk/InversionOfControl/Castle.MicroKernel/Castle.MicroKernel-vs2008.csproj 2008-03-01 16:49:01 UTC (rev 4861)
@@ -342,6 +342,9 @@
<Compile Include="Registration\RegistrationGroup.cs" />
<Compile Include="Registration\ServiceOverride.cs" />
<Compile Include="Registration\ServiceOverrideDescriptor.cs" />
+ <Compile Include="Registration\Strategies\AllTypesOf.cs" />
+ <Compile Include="Registration\Strategies\ServiceDescriptor.cs" />
+ <Compile Include="Registration\Strategies\TypesDescriptor.cs" />
<Compile Include="Releasers\AllComponentsReleasePolicy.cs">
<SubType>Code</SubType>
</Compile>

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/
==============================================================

File [modified]: Castle.MicroKernel.Tests-vs2008.csproj
Delta lines: +1 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2008.csproj 2008-03-01 16:44:27 UTC (rev 4860)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2008.csproj 2008-03-01 16:49:01 UTC (rev 4861)
@@ -255,6 +255,7 @@
<SubType>Code</SubType>
</Compile>
<Compile Include="Registration\ComponentRegistrationTestCase.cs" />
+ <Compile Include="Registration\AllTypesOfTestCase.cs" />
<Compile Include="RuntimeParametersTestCase.cs" />
<Compile Include="RuntimeParameters\ComponentWithParameters.cs" />
<Compile Include="SerializationTestCase.cs">

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/
==============================================================================

File [modified]: CustomerChain.cs
Delta lines: +4 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/CustomerChain.cs 2008-03-01 16:44:27 UTC (rev 4860)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/CustomerChain.cs 2008-03-01 16:49:01 UTC (rev 4861)
@@ -14,6 +14,8 @@

namespace Castle.MicroKernel.Tests.ClassComponents
{
+ using System;
+
public class CustomerChain1 : CustomerImpl
{
public ICustomer CustomerBase;
@@ -66,6 +68,7 @@
}
}

+ [Serializable]
public class CustomerChain8 : CustomerChain1
{
public CustomerChain8(ICustomer customer) : base(customer)
@@ -73,6 +76,7 @@
}
}

+ [Serializable]
public class CustomerChain9 : CustomerChain1
{
public CustomerChain9(ICustomer customer) : base(customer)

Directory: /trunk/InversionOfControl/
=====================================

File [modified]: Changes.txt
Delta lines: +3 -0
===================================================================
--- trunk/InversionOfControl/Changes.txt 2008-03-01 16:44:27 UTC (rev 4860)
+++ trunk/InversionOfControl/Changes.txt 2008-03-01 16:49:01 UTC (rev 4861)
@@ -1,6 +1,9 @@
RC 4
====

+- Added AllTypesOf registration stratgey to simplify custom registration
+ scenarios.
+


- Added IRegistration interface to allow alternate registration mechanisms.

- Fixed CORE-16 (Should be Facilities)

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/
=====================================================================

File [modified]: ComponentRegistration.cs
Delta lines: +24 -13
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegistration.cs 2008-03-01 16:44:27 UTC (rev 4860)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegistration.cs 2008-03-01 16:49:01 UTC (rev 4861)
@@ -30,7 +30,7 @@


private String name;
private bool overwrite;

private Type serviceType;
- private Type classType;
+ private Type implementation;


private readonly List<ComponentDescriptor<S>> descriptors;
private ComponentModel componentModel;

@@ -49,16 +49,27 @@


descriptors = new List<ComponentDescriptor<S>>();
}

- internal bool IsOverWrite
+ public String Name
{
- get { return overwrite; }
+ get { return name; }
}
+
+ public Type ServiceType
+ {
+ get { return serviceType; }
+ protected set { serviceType = value; }
+ }

- protected Type ServiceType
+ public Type Implementation
{
- set { serviceType = value; }
+ get { return implementation; }
}
-
+
+ internal bool IsOverWrite
+ {
+ get { return overwrite; }


+ }
+
/// <summary>
/// With the overwrite.
/// </summary>

@@ -95,14 +106,14 @@

public ComponentRegistration<S> ImplementedBy(Type type)
{
- if (classType != null)
+ if (implementation != null)
{
String message = String.Format("This component has " +
- "already been assigned implementation {0}", classType.FullName);
+ "already been assigned implementation {0}", implementation.FullName);
throw new ComponentRegistrationException(message);
}

- classType = type;
+ implementation = type;
return this;
}

@@ -281,7 +292,7 @@
}

ComponentModel model = kernel.ComponentModelBuilder.BuildModel(
- name, serviceType, classType, null);
+ name, serviceType, implementation, null);
foreach(ComponentDescriptor<S> descriptor in descriptors)
{
descriptor.ApplyToModel(kernel, model);
@@ -333,14 +344,14 @@

private void InitializeDefaults()
{
- if (classType == null)
+ if (implementation == null)
{
- classType = serviceType;
+ implementation = serviceType;
}

if (String.IsNullOrEmpty(name))
{
- name = classType.FullName;
+ name = implementation.FullName;
}
}

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
===========================================================================

File [added]: AllTypesOfTestCase.cs
Delta lines: +164 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs (rev 0)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-01 16:49:01 UTC (rev 4861)
@@ -0,0 +1,164 @@


+// Copyright 2004-2008 Castle Project - http://www.castleproject.org/
+//
+// 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 Castle.MicroKernel.Tests.Registration
+{
+ using System;
+ using System.Linq;
+ using System.Reflection;
+ using Castle.Core;
+ using Castle.MicroKernel.Registration;
+ using Castle.MicroKernel.Tests.ClassComponents;
+ using NUnit.Framework;
+
+ [TestFixture]
+ public class AllTypesOfTestCase
+ {
+ private IKernel kernel;
+
+ [SetUp]
+ public void Init()
+ {
+ kernel = new DefaultKernel();
+ }
+
+ [Test]
+ public void RegisterAssemblyTypes_NoService_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<ICommon>
+ .FromAssembly(Assembly.GetExecutingAssembly())
+ );
+
+ IHandler[] handlers = kernel.GetHandlers(typeof(ICommon));
+ Assert.AreEqual(0, handlers.Length);
+
+ handlers = kernel.GetAssignableHandlers(typeof(ICommon));
+ Assert.AreNotEqual(0, handlers.Length);
+ }
+
+ [Test]
+ public void RegisterAssemblyTypes_FirstInterfaceService_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<ICommon>
+ .FromAssembly(Assembly.GetExecutingAssembly())
+ .WithService.FirstInterface()
+ );
+
+ IHandler[] handlers = kernel.GetHandlers(typeof(ICommon));
+ Assert.AreNotEqual(0, handlers.Length);
+
+ handlers = kernel.GetAssignableHandlers(typeof(ICommon));
+ Assert.AreNotEqual(0, handlers.Length);
+ }
+
+ [Test]
+ public void RegisterAssemblyTypes_DefaultService_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<ICommon>
+ .FromAssembly(Assembly.GetExecutingAssembly())
+ .WithService.Base()
+ );
+
+ IHandler[] handlers = kernel.GetHandlers(typeof(ICommon));
+ Assert.AreNotEqual(0, handlers.Length);
+
+ handlers = kernel.GetAssignableHandlers(typeof(ICommon));
+ Assert.AreNotEqual(0, handlers.Length);
+ }
+
+ [Test]
+ public void RegisterAssemblyTypes_WithConfiguration_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<ICommon>
+ .FromAssembly(Assembly.GetExecutingAssembly())
+ .Configure(delegate(ComponentRegistration component)
+ {
+ component.LifeStyle.Transient
+ .Named(component.Implementation.FullName + "XYZ");
+ })
+ );
+
+ foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICommon)))
+ {
+ Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
+ Assert.AreEqual(handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name);
+ }
+ }
+
+ #if DOTNET35
+
+ [Test]
+ public void RegisterAssemblyTypes_IfCondition_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<ICustomer>
+ .FromAssembly(Assembly.GetExecutingAssembly())
+ .If(t => t.FullName.Contains("Chain"))
+ );
+
+ IHandler[] handlers = kernel.GetAssignableHandlers(typeof(ICustomer));
+ Assert.AreNotEqual(0, handlers.Length);
+
+ foreach (IHandler handler in handlers)
+ {
+ Assert.IsTrue(handler.ComponentModel.Implementation.FullName.Contains("Chain"));
+ }
+ }
+
+ [Test]
+ public void RegisterAssemblyTypes_UnlessCondition_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<ICustomer>
+ .FromAssembly(Assembly.GetExecutingAssembly())
+ .Unless(t => typeof(CustomerChain1).IsAssignableFrom(t))
+ );
+
+ foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICustomer)))
+ {
+ Assert.IsFalse(typeof(CustomerChain1).IsAssignableFrom(handler.ComponentModel.Implementation));
+ }
+ }
+
+ [Test]
+ public void RegisterTypes_WithLinq_RegisteredInContainer()
+ {
+ kernel.Register(AllTypesOf<CustomerChain1>
+ .Pick(from type in Assembly.GetExecutingAssembly().GetExportedTypes()
+ where type.IsDefined(typeof(SerializableAttribute), true)
+ select type
+ ));
+
+ IHandler[] handlers = kernel.GetAssignableHandlers(typeof(CustomerChain1));
+ Assert.AreEqual(2, handlers.Length);
+ }
+
+ [Test]
+ public void RegisterAssemblyTypes_WithKLinqConfiguration_RegisteredInContainer()
+ {
+ kernel.Register( AllTypesOf<ICommon>
+ .FromAssembly( Assembly.GetExecutingAssembly() )
+ .Configure( component => component.LifeStyle.Transient
+ .Named( component.Implementation.FullName + "XYZ" )
+ )
+ );
+
+ foreach ( IHandler handler in kernel.GetAssignableHandlers( typeof( ICommon ) ) )
+ {
+ Assert.AreEqual( LifestyleType.Transient, handler.ComponentModel.LifestyleType );
+ Assert.AreEqual( handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name );
+ }
+ }
+
+ #endif

+ }
+}
\ No newline at end of file

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/
================================================================================

File [added]: AllTypesOf.cs
Delta lines: +101 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs (rev 0)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs 2008-03-01 16:49:01 UTC (rev 4861)
@@ -0,0 +1,101 @@
+// Copyright 2004-2008 Castle Project - http://www.castleproject.org/


+//
+// 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 Castle.MicroKernel.Registration
+{

+ using System;
+ using System.IO;
+ using System.Reflection;
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// The starting point to describe types to register.
+ /// </summary>
+ /// <typeparam name="T">The base type to match against.</typeparam>
+ public static class AllTypesOf<T>
+ {
+ /// <summary>
+ /// Prepares to register types from an assembly.
+ /// </summary>
+ /// <param name="assemblyName">The assembly name.</param>
+ /// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>
+ public static TypesDescriptor<T> FromAssembly(string assemblyName)
+ {
+ Assembly assembly;
+ String extension = Path.GetExtension(assemblyName);
+
+ if (extension == ".dll" || extension == ".exe")
+ {
+ if (Path.GetDirectoryName(assemblyName) == AppDomain.CurrentDomain.BaseDirectory)
+ {
+ assembly = Assembly.Load(Path.GetFileNameWithoutExtension(assemblyName));
+ }
+ else
+ {
+ assembly = Assembly.LoadFile(assemblyName);
+ }
+ }
+ else
+ {
+ assembly = Assembly.Load(assemblyName);
+ }
+
+ return FromAssembly(assembly);
+ }
+
+ /// <summary>
+ /// Prepares to register types from an assembly.
+ /// </summary>
+ /// <param name="assembly">The assembly.</param>
+ /// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>
+ public static TypesDescriptor<T> FromAssembly(Assembly assembly)
+ {
+ if (assembly == null)
+ {
+ throw new ArgumentNullException("assembly");
+ }
+ return From(assembly.GetExportedTypes());
+ }
+
+ /// <summary>
+ /// Prepares to register types from a list of types.
+ /// </summary>
+ /// <param name="types">The list of types.</param>
+ /// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>
+ public static TypesDescriptor<T> From(IEnumerable<Type> types)
+ {
+ return new TypesDescriptor<T>(types);
+ }
+
+ /// <summary>
+ /// Prepares to register types from a list of types.
+ /// </summary>
+ /// <param name="types">The list of types.</param>
+ /// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>
+ public static TypesDescriptor<T> Pick( IEnumerable<Type> types )
+ {
+ return new TypesDescriptor<T>( types );
+ }
+
+ /// <summary>
+ /// Prepares to register types from a list of types.
+ /// </summary>
+ /// <param name="types">The list of types.</param>
+ /// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>
+ public static TypesDescriptor<T> From( params Type[] types )
+ {
+ return new TypesDescriptor<T>( types );
+ }
+ }
+}

File [added]: ServiceDescriptor.cs
Delta lines: +84 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs (rev 0)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs 2008-03-01 16:49:01 UTC (rev 4861)
@@ -0,0 +1,84 @@
+// Copyright 2004-2008 Castle Project - http://www.castleproject.org/


+//
+// 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 Castle.MicroKernel.Registration
+{

+ using System;
+
+ /// <summary>
+ /// Describes how to select a types service.
+ /// </summary>
+ /// <typeparam name="T">The base type to match against.</typeparam>
+ public class ServiceDescriptor<T>
+ {
+ public delegate Type ServiceSelector(Type type);
+
+ private readonly TypesDescriptor<T> typesDescriptor;
+ private ServiceSelector serviceSelector;
+
+ internal ServiceDescriptor(TypesDescriptor<T> typesDescriptor)
+ {
+ this.typesDescriptor = typesDescriptor;
+ }
+
+ /// <summary>
+ /// Uses the base type matched on.
+ /// </summary>
+ /// <returns></returns>
+ public TypesDescriptor<T> Base()
+ {
+ return Select(delegate(Type type)
+ {
+ return typeof(T);
+ });
+ }
+
+ /// <summary>
+ /// Uses the first interface of a type.
+ /// </summary>
+ /// <returns></returns>
+ public TypesDescriptor<T> FirstInterface()
+ {
+ return Select(delegate(Type type)
+ {
+ Type[] interfaces = type.GetInterfaces();
+ if (interfaces.Length > 0)
+ {
+ return interfaces[0];
+ }
+ return null;
+ });
+ }
+
+ /// <summary>
+ /// Assigns a custom service selection strategy.
+ /// </summary>
+ /// <param name="selector"></param>
+ /// <returns></returns>
+ public TypesDescriptor<T> Select(ServiceSelector selector)
+ {
+ this.serviceSelector = selector;
+ return typesDescriptor;
+ }
+
+ internal Type GetService(Type type)
+ {
+ if (serviceSelector != null)
+ {
+ return serviceSelector(type) ?? type;
+ }
+ return type;
+ }
+ }
+}

File [added]: TypesDescriptor.cs
Delta lines: +124 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/TypesDescriptor.cs (rev 0)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/TypesDescriptor.cs 2008-03-01 16:49:01 UTC (rev 4861)
@@ -0,0 +1,124 @@
+// Copyright 2004-2008 Castle Project - http://www.castleproject.org/


+//
+// 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 Castle.MicroKernel.Registration
+{

+ using System;
+ using System.Collections.Generic;
+
+ /// <summary>
+ /// Describes how to register a group of types.
+ /// </summary>
+ /// <typeparam name="T">The The base type to match against.</typeparam>
+ public class TypesDescriptor<T> : IRegistration
+ {
+ private readonly IEnumerable<Type> types;
+ private readonly ServiceDescriptor<T> service;
+ private Action<ComponentRegistration> configurer;
+ private Predicate<Type> unlessFilter;
+ private Predicate<Type> ifFilter;
+
+ /// <summary>
+ /// Initializes a new instance of the TypesDescriptor.
+ /// </summary>
+ internal TypesDescriptor(IEnumerable<Type> types)
+ {
+ this.types = types;
+ service = new ServiceDescriptor<T>(this);
+ }
+
+ /// <summary>
+ /// Assigns a conditional predication which must be satisfied.
+ /// </summary>
+ /// <param name="ifFilter">The predicate to satisfy.</param>
+ /// <returns></returns>
+ public TypesDescriptor<T> If(Predicate<Type> ifFilter)
+ {
+ this.ifFilter = ifFilter;
+ return this;
+ }
+
+ /// <summary>
+ /// Assigns a conditional predication which must not be satisfied.
+ /// </summary>
+ /// <param name="unlessFilter">The predicate not to satisify.</param>
+ /// <returns></returns>
+ public TypesDescriptor<T> Unless( Predicate<Type> unlessFilter )
+ {
+ this.unlessFilter = unlessFilter;
+ return this;
+ }
+
+ /// <summary>
+ /// Gets the service descriptor.
+ /// </summary>
+ public ServiceDescriptor<T> WithService
+ {
+ get { return service; }
+ }
+
+ /// <summary>
+ /// Allows customized configurations of each matching type.
+ /// </summary>
+ /// <param name="configurer">The configuration action.</param>
+ /// <returns></returns>
+ public TypesDescriptor<T> Configure(Action<ComponentRegistration> configurer)
+ {
+ this.configurer = configurer;
+ return this;
+ }
+
+ #region IRegistration Members
+
+ void IRegistration.Register(IKernel kernel)
+ {
+ foreach (Type type in types)
+ {
+ if (!IsTypeAccepted(type))
+ {
+ continue;
+ }
+
+ Type serviceType = service.GetService(type);
+ ComponentRegistration registration = Component.For(serviceType);
+ registration.ImplementedBy(type);
+
+ if ( configurer != null )
+ {
+ configurer(registration);
+ }
+
+ if (String.IsNullOrEmpty(registration.Name))
+ {
+ registration.Named(type.FullName);
+ }
+
+ if (!kernel.HasComponent(registration.Name))
+ {
+ kernel.Register(registration);
+ }
+ }
+ }
+
+ #endregion
+
+ private bool IsTypeAccepted(Type type)
+ {
+ return type.IsClass && !type.IsAbstract && typeof(T).IsAssignableFrom(type)
+ && (ifFilter == null || ifFilter(type))
+ && (unlessFilter == null || !unlessFilter(type)
+ );
+ }
+ }
+}

notifi...@castleproject.org

unread,
Mar 1, 2008, 11:56:10 AM3/1/08
to castle-proj...@googlegroups.com
User: cneuwirt
Date: 2008/03/01 09:56 AM

Modified:
/trunk/InversionOfControl/Castle.MicroKernel.Tests/
Castle.MicroKernel.Tests-vs2005.csproj
/trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
AllTypesOfTestCase.cs
/trunk/InversionOfControl/Castle.MicroKernel/
Castle.MicroKernel-vs2005.csproj

Log:
Updated vs2005 projects for AllTypesOf<> registration updates.

File Changes:

Directory: /trunk/InversionOfControl/Castle.MicroKernel/
========================================================

File [modified]: Castle.MicroKernel-vs2005.csproj


Delta lines: +3 -0
===================================================================

--- trunk/InversionOfControl/Castle.MicroKernel/Castle.MicroKernel-vs2005.csproj 2008-03-01 16:49:01 UTC (rev 4861)
+++ trunk/InversionOfControl/Castle.MicroKernel/Castle.MicroKernel-vs2005.csproj 2008-03-01 16:56:10 UTC (rev 4862)
@@ -337,6 +337,9 @@


<Compile Include="Registration\RegistrationGroup.cs" />
<Compile Include="Registration\ServiceOverride.cs" />
<Compile Include="Registration\ServiceOverrideDescriptor.cs" />
+ <Compile Include="Registration\Strategies\AllTypesOf.cs" />
+ <Compile Include="Registration\Strategies\ServiceDescriptor.cs" />
+ <Compile Include="Registration\Strategies\TypesDescriptor.cs" />
<Compile Include="Releasers\AllComponentsReleasePolicy.cs">
<SubType>Code</SubType>
</Compile>

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/
==============================================================

File [modified]: Castle.MicroKernel.Tests-vs2005.csproj


Delta lines: +1 -0
===================================================================

--- trunk/InversionOfControl/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2005.csproj 2008-03-01 16:49:01 UTC (rev 4861)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/Castle.MicroKernel.Tests-vs2005.csproj 2008-03-01 16:56:10 UTC (rev 4862)
@@ -248,6 +248,7 @@
<Compile Include="Pools\PooledLifestyleManagerTestCase.cs">
<SubType>Code</SubType>
</Compile>


+ <Compile Include="Registration\AllTypesOfTestCase.cs" />

<Compile Include="Registration\ComponentRegistrationTestCase.cs" />


<Compile Include="RuntimeParametersTestCase.cs" />
<Compile Include="RuntimeParameters\ComponentWithParameters.cs" />

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
===========================================================================

File [modified]: AllTypesOfTestCase.cs
Delta lines: +4 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-01 16:49:01 UTC (rev 4861)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-01 16:56:10 UTC (rev 4862)
@@ -15,13 +15,16 @@
namespace Castle.MicroKernel.Tests.Registration
{
using System;
- using System.Linq;
using System.Reflection;
using Castle.Core;
using Castle.MicroKernel.Registration;
using Castle.MicroKernel.Tests.ClassComponents;
using NUnit.Framework;

+#if DOTNET35
+ using System.Linq;
+#endif
+
[TestFixture]
public class AllTypesOfTestCase
{

notifi...@castleproject.org

unread,
Mar 1, 2008, 1:28:28 PM3/1/08
to castle-proj...@googlegroups.com
User: cneuwirt
Date: 2008/03/01 11:28 AM

Modified:
/trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
AllTypesOfTestCase.cs
/trunk/InversionOfControl/Castle.MicroKernel/Registration/
ComponentRegistration.cs
/trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/
AllTypesOf.cs, TypesDescriptor.cs

Log:
Fixed some formatting issues.

File Changes:

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/
=====================================================================

File [modified]: ComponentRegistration.cs
Delta lines: +8 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegistration.cs 2008-03-01 18:17:26 UTC (rev 4863)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/ComponentRegistration.cs 2008-03-01 18:28:28 UTC (rev 4864)
@@ -386,12 +386,19 @@
}

public ComponentRegistration(Type serviceType)
- : base( serviceType )
+ : base(serviceType)
{
}

public ComponentRegistration For(Type serviceType)
{
+ if (ServiceType != null)
+ {
+ String message = String.Format("This component has " +
+ "already been assigned service type {0}", ServiceType.FullName);
+ throw new ComponentRegistrationException(message);


+ }
+
ServiceType = serviceType;

return this;
}

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
===========================================================================

File [modified]: AllTypesOfTestCase.cs
Delta lines: +8 -8
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-01 18:17:26 UTC (rev 4863)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-01 18:28:28 UTC (rev 4864)
@@ -139,7 +139,7 @@
.Pick(from type in Assembly.GetExecutingAssembly().GetExportedTypes()
where type.IsDefined(typeof(SerializableAttribute), true)
select type
- ));
+ ));

IHandler[] handlers = kernel.GetAssignableHandlers(typeof(CustomerChain1));
Assert.AreEqual(2, handlers.Length);
@@ -148,17 +148,17 @@
[Test]
public void RegisterAssemblyTypes_WithKLinqConfiguration_RegisteredInContainer()
{
- kernel.Register( AllTypesOf<ICommon>
- .FromAssembly( Assembly.GetExecutingAssembly() )
- .Configure( component => component.LifeStyle.Transient
- .Named( component.Implementation.FullName + "XYZ" )


+ kernel.Register(AllTypesOf<ICommon>
+ .FromAssembly(Assembly.GetExecutingAssembly())

+ .Configure(component => component.LifeStyle.Transient


+ .Named(component.Implementation.FullName + "XYZ")

)
);

- foreach ( IHandler handler in kernel.GetAssignableHandlers( typeof( ICommon ) ) )


+ foreach (IHandler handler in kernel.GetAssignableHandlers(typeof(ICommon)))

{
- Assert.AreEqual( LifestyleType.Transient, handler.ComponentModel.LifestyleType );
- Assert.AreEqual( handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name );


+ Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);
+ Assert.AreEqual(handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name);
}
}

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/
================================================================================

File [modified]: AllTypesOf.cs
Delta lines: +4 -4
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs 2008-03-01 18:17:26 UTC (rev 4863)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs 2008-03-01 18:28:28 UTC (rev 4864)
@@ -83,9 +83,9 @@
/// </summary>


/// <param name="types">The list of types.</param>

/// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>

- public static TypesDescriptor<T> Pick( IEnumerable<Type> types )
+ public static TypesDescriptor<T> Pick(IEnumerable<Type> types)
{
- return new TypesDescriptor<T>( types );


+ return new TypesDescriptor<T>(types);
}

/// <summary>
@@ -93,9 +93,9 @@
/// </summary>


/// <param name="types">The list of types.</param>

/// <returns>The corresponding <see cref="TypesDescriptor{T}"/></returns>

- public static TypesDescriptor<T> From( params Type[] types )
+ public static TypesDescriptor<T> From(params Type[] types)
{
- return new TypesDescriptor<T>( types );


+ return new TypesDescriptor<T>(types);
}
}
}

File [modified]: TypesDescriptor.cs


Delta lines: +1 -1
===================================================================

--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/TypesDescriptor.cs 2008-03-01 18:17:26 UTC (rev 4863)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/TypesDescriptor.cs 2008-03-01 18:28:28 UTC (rev 4864)
@@ -94,7 +94,7 @@
ComponentRegistration registration = Component.For(serviceType);
registration.ImplementedBy(type);

- if ( configurer != null )
+ if (configurer != null)
{
configurer(registration);
}

notifi...@castleproject.org

unread,
Mar 1, 2008, 8:18:18 PM3/1/08
to castle-proj...@googlegroups.com
User: cneuwirt
Date: 2008/03/01 06:18 PM

Modified:
/trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/
Repository.cs
/trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
AllTypesOfTestCase.cs
/trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/
AllTypesOf.cs, ServiceDescriptor.cs

Log:
Added testcase for generic registrations and a workaround for the CLR GetInterfaces() bug in which interfaces with no implementations are returned.

File Changes:

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/
==============================================================================

File [modified]: Repository.cs
Delta lines: +15 -0
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/Repository.cs 2008-03-01 18:33:15 UTC (rev 4865)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/ClassComponents/Repository.cs 2008-03-02 01:18:16 UTC (rev 4866)
@@ -18,6 +18,12 @@
{
}

+ public interface IRepository<T> : IRepository
+ where T : class
+ {
+ T Find();
+ }
+
public class DecoratedRepository : IRepository
{
public DecoratedRepository()
@@ -69,4 +75,13 @@
_inner = inner;
}
}
+
+ public class DefaultRepository<T> : IRepository<T>
+ where T : class, new()
+ {
+ public T Find()
+ {
+ return new T();


+ }
+ }
}
\ No newline at end of file

Directory: /trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/
===========================================================================

File [modified]: AllTypesOfTestCase.cs
Delta lines: +16 -3
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-01 18:33:15 UTC (rev 4865)
+++ trunk/InversionOfControl/Castle.MicroKernel.Tests/Registration/AllTypesOfTestCase.cs 2008-03-02 01:18:16 UTC (rev 4866)
@@ -97,11 +97,24 @@
Assert.AreEqual(LifestyleType.Transient, handler.ComponentModel.LifestyleType);


Assert.AreEqual(handler.ComponentModel.Implementation.FullName + "XYZ", handler.ComponentModel.Name);
}

- }
-
- #if DOTNET35
+ }

[Test]
+ public void RegisterGenericTypes_WithGenericDefinition_RegisteredInContainer()
+ {
+ kernel.Register(AllTypes
+ .From(typeof(DefaultRepository<>))


+ .WithService.FirstInterface()
+ );
+

+ Type t = typeof(IRepository<CustomerImpl>);
+
+ IRepository<CustomerImpl> repository = kernel.Resolve<IRepository<CustomerImpl>>();
+ }
+
+#if DOTNET35
+
+ [Test]
public void RegisterAssemblyTypes_IfCondition_RegisteredInContainer()
{
kernel.Register(AllTypesOf<ICustomer>

Directory: /trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/
================================================================================

File [modified]: AllTypesOf.cs
Delta lines: +5 -1
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs 2008-03-01 18:33:15 UTC (rev 4865)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/AllTypesOf.cs 2008-03-02 01:18:16 UTC (rev 4866)
@@ -23,7 +23,7 @@


/// The starting point to describe types to register.

/// </summary>


/// <typeparam name="T">The base type to match against.</typeparam>

- public static class AllTypesOf<T>
+ public class AllTypesOf<T>
{
/// <summary>


/// Prepares to register types from an assembly.

@@ -98,4 +98,8 @@


return new TypesDescriptor<T>(types);
}
}

+
+ public class AllTypes : AllTypesOf<object>
+ {
+ }
}

File [modified]: ServiceDescriptor.cs
Delta lines: +13 -7
===================================================================
--- trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs 2008-03-01 18:33:15 UTC (rev 4865)
+++ trunk/InversionOfControl/Castle.MicroKernel/Registration/Strategies/ServiceDescriptor.cs 2008-03-02 01:18:16 UTC (rev 4866)
@@ -38,10 +38,7 @@
/// <returns></returns>
public TypesDescriptor<T> Base()
{
- return Select(delegate(Type type)
- {
- return typeof(T);
- });
+ return Select(delegate { return typeof(T); });
}

/// <summary>
@@ -52,12 +49,21 @@
{
return Select(delegate(Type type)
{
+ Type first = null;
Type[] interfaces = type.GetInterfaces();
if (interfaces.Length > 0)
{
- return interfaces[0];
+ first = interfaces[0];
+
+ // This is a workaround for a CLR bug in
+ // which GetInterfaces() returns interfaces
+ // with no implementations.
+ if (first.IsGenericType && first.ReflectedType == null)
+ {
+ first = first.GetGenericTypeDefinition();
+ }
}
- return null;
+ return first;
});
}

@@ -68,7 +74,7 @@
/// <returns></returns>


public TypesDescriptor<T> Select(ServiceSelector selector)

{
- this.serviceSelector = selector;
+ serviceSelector = selector;
return typesDescriptor;
}

Reply all
Reply to author
Forward
0 new messages