Created
January 28, 2011 02:20
-
-
Save MiguelMadero/799706 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using Autofac; | |
using Autofac.Builder; | |
using Autofac.Core; | |
using Autofac.Features.Scanning; | |
namespace HiddenNamespace | |
{ | |
public static class RegistrationExtensions | |
{ | |
/// <summary> | |
/// Specifies that a type from a scanned assembly is registered using the default interface. | |
/// Following the convention of Type, IType. The default interface for ProductRepository will be | |
/// IProductRepository | |
/// </summary> | |
/// <typeparam name="TLimit">Registration limit type.</typeparam> | |
/// <typeparam name="TScanningActivatorData">Activator data type.</typeparam> | |
/// <param name="registration">Registration to set service mapping on.</param> | |
/// <returns>Registration builder allowing the registration to be configured.</returns> | |
public static IRegistrationBuilder<TLimit, TScanningActivatorData, DynamicRegistrationStyle> | |
AsDefaultInterface<TLimit, TScanningActivatorData>(this IRegistrationBuilder<TLimit, TScanningActivatorData, DynamicRegistrationStyle> registration) | |
where TScanningActivatorData : ScanningActivatorData | |
{ | |
if (registration == null) throw new ArgumentNullException("registration"); | |
return registration.As(t => t.GetInterfaces() | |
.Where(i => i != typeof(IDisposable)) | |
.Where(i => i.IsDefaultInterfaceFor(t)) | |
.Select(i => new TypedService(i)) | |
// ReSharper disable RedundantEnumerableCastCall | |
.Cast<Service>()); | |
// ReSharper restore RedundantEnumerableCastCall | |
} | |
/// <summary> | |
/// Specifies that a is registered using the default interface. | |
/// Following the convention of Type, IType. The default interface for ProductRepository will be | |
/// IProductRepository | |
/// </summary> | |
/// <typeparam name="TLimit">Registration limit type.</typeparam> | |
/// <typeparam name="TScanningActivatorData">Activator data type.</typeparam> | |
/// <param name="registration">Registration to set service mapping on.</param> | |
/// <returns>Registration builder allowing the registration to be configured.</returns> | |
public static IRegistrationBuilder<TLimit, TScanningActivatorData, SingleRegistrationStyle> | |
AsImplementedInterfaces<TLimit, TScanningActivatorData>(this IRegistrationBuilder<TLimit, TScanningActivatorData, SingleRegistrationStyle> registration) | |
{ | |
if (registration == null) throw new ArgumentNullException("registration"); | |
return registration.As(typeof(TLimit).GetInterfaces() | |
.Where(i=>i.IsDefaultInterfaceFor(typeof(TLimit))) | |
.Select(i => new TypedService(i)) | |
.Cast<Service>() | |
.ToArray()); | |
} | |
public static bool IsDefaultInterfaceFor(this Type interfaceType, Type type) | |
{ | |
return interfaceType.Name == "I" + type.Name; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment