Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- }
Add Comment
Please, Sign In to add comment