Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Commit

Permalink
Resolver refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
osoykan committed Aug 30, 2017
1 parent d692dfd commit 12f5b13
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 123 deletions.
236 changes: 118 additions & 118 deletions src/Autofac.Extras.IocManager/Resolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,133 +7,133 @@

namespace Autofac.Extras.IocManager
{
internal class Resolver : IResolver
{
/// <summary>
/// The component context
/// </summary>
private readonly IComponentContext _componentContext;
internal class Resolver : IResolver
{
/// <summary>
/// The component context
/// </summary>
private readonly IComponentContext _componentContext;

/// <summary>
/// Initializes a new instance of the <see cref="Resolver" /> class.
/// </summary>
/// <param name="componentContext">The component context.</param>
public Resolver(IComponentContext componentContext)
{
_componentContext = componentContext;
}
/// <summary>
/// Initializes a new instance of the <see cref="Resolver" /> class.
/// </summary>
/// <param name="componentContext">The component context.</param>
public Resolver(IComponentContext componentContext)
{
_componentContext = componentContext;
}

/// <summary>
/// Resolves this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>()
{
return _componentContext.Resolve<T>();
}
/// <summary>
/// Resolves this instance.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public T Resolve<T>()
{
return _componentContext.Resolve<T>();
}

/// <summary>
/// Resolves the specified service type.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <returns></returns>
public object Resolve(Type serviceType)
{
return _componentContext.Resolve(serviceType);
}
/// <summary>
/// Resolves the specified service type.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <returns></returns>
public object Resolve(Type serviceType)
{
return _componentContext.Resolve(serviceType);
}

/// <summary>
/// Resolves all.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <returns></returns>
public IEnumerable<object> ResolveAll(Type serviceType)
{
Type enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType);
return ((IEnumerable)_componentContext.Resolve(enumerableType)).OfType<object>().ToList();
}
/// <summary>
/// Resolves all.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <returns></returns>
public IEnumerable<object> ResolveAll(Type serviceType)
{
Type enumerableType = typeof(IEnumerable<>).MakeGenericType(serviceType);
return ((IEnumerable)_componentContext.Resolve(enumerableType)).OfType<object>().ToList();
}

/// <summary>
/// Gets the registered services.
/// </summary>
/// <returns></returns>
public IEnumerable<Type> GetRegisteredServices()
{
return _componentContext.ComponentRegistry.Registrations
.SelectMany(x => x.Services)
.OfType<TypedService>()
.Where(x => !x.ServiceType.Name.StartsWith("Autofac"))
.Select(x => x.ServiceType);
}
/// <summary>
/// Gets the registered services.
/// </summary>
/// <returns></returns>
public IEnumerable<Type> GetRegisteredServices()
{
return _componentContext.ComponentRegistry.Registrations
.SelectMany(x => x.Services)
.OfType<TypedService>()
.Select(x => x.ServiceType)
.ToList();
}

/// <summary>
/// Determines whether this instance is registered.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>
/// <c>true</c> if this instance is registered; otherwise, <c>false</c>.
/// </returns>
public bool IsRegistered<T>()
where T : class
{
return IsRegistered(typeof(T));
}
/// <summary>
/// Determines whether this instance is registered.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>
/// <c>true</c> if this instance is registered; otherwise, <c>false</c>.
/// </returns>
public bool IsRegistered<T>()
where T : class
{
return IsRegistered(typeof(T));
}

/// <summary>
/// Determines whether the specified type is registered.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if the specified type is registered; otherwise, <c>false</c>.
/// </returns>
public bool IsRegistered(Type type)
{
return GetRegisteredServices().Any(t => t == type);
}
/// <summary>
/// Determines whether the specified type is registered.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>
/// <c>true</c> if the specified type is registered; otherwise, <c>false</c>.
/// </returns>
public bool IsRegistered(Type type)
{
return _componentContext.IsRegistered(type);
}

/// <summary>
/// Resolves the specified argument as anonymous type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argumentAsAnonymousType">Type of the argument as anonymous.</param>
/// <returns></returns>
public T Resolve<T>(object argumentAsAnonymousType)
{
return _componentContext.Resolve<T>(argumentAsAnonymousType.GetTypedResolvingParameters());
}
/// <summary>
/// Resolves the specified argument as anonymous type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="argumentAsAnonymousType">Type of the argument as anonymous.</param>
/// <returns></returns>
public T Resolve<T>(object argumentAsAnonymousType)
{
return _componentContext.Resolve<T>(argumentAsAnonymousType.GetTypedResolvingParameters());
}

/// <summary>
/// Resolves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="argumentAsAnonymousType">Type of the argument as anonymous.</param>
/// <returns></returns>
public object Resolve(Type type, object argumentAsAnonymousType)
{
return _componentContext.Resolve(type, argumentAsAnonymousType.GetTypedResolvingParameters());
}
/// <summary>
/// Resolves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <param name="argumentAsAnonymousType">Type of the argument as anonymous.</param>
/// <returns></returns>
public object Resolve(Type type, object argumentAsAnonymousType)
{
return _componentContext.Resolve(type, argumentAsAnonymousType.GetTypedResolvingParameters());
}

/// <summary>
/// Resolves the specified parameters.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public T Resolve<T>(params Parameter[] parameters)
{
return _componentContext.Resolve<T>(parameters);
}
/// <summary>
/// Resolves the specified parameters.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public T Resolve<T>(params Parameter[] parameters)
{
return _componentContext.Resolve<T>(parameters);
}

/// <summary>
/// Resolves the specified service type.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public object Resolve(Type serviceType, params Parameter[] parameters)
{
return _componentContext.Resolve(serviceType, parameters);
}
}
/// <summary>
/// Resolves the specified service type.
/// </summary>
/// <param name="serviceType">Type of the service.</param>
/// <param name="parameters">The parameters.</param>
/// <returns></returns>
public object Resolve(Type serviceType, params Parameter[] parameters)
{
return _componentContext.Resolve(serviceType, parameters);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Data.Common;
using System.Linq;

using Autofac.Extensions.DependencyInjection;
using Autofac.Extras.IocManager.TestBase;
Expand Down Expand Up @@ -47,12 +48,16 @@ public void serviceprovider_should_resolve_lambda()

using (ILifetimeScope beginLifetimeScope = serviceProvider.GetService<ILifetimeScope>().BeginLifetimeScope(
"message",
ctx =>
{
ctx.RegisterType<UserCreatedConsumer>().AsSelf().InstancePerDependency();
}))
ctx => { ctx.RegisterType<UserCreatedConsumer>().AsSelf().InstancePerDependency(); }))
{
beginLifetimeScope.Resolve<UserCreatedConsumer>().Consume();
using (var scopeResolver = beginLifetimeScope.Resolve<IScopeResolver>())
{
Type service = scopeResolver.GetRegisteredServices().FirstOrDefault(x => x == typeof(IStoveDbContextConfigurer<ContextBoundObject>));
scopeResolver.IsRegistered<IStoveDbContextConfigurer<ContextBoundObject>>().Should().Be(true);
service = scopeResolver.GetRegisteredServices().FirstOrDefault(x => x == typeof(IStoveDbContextConfigurer<ContextBoundObject>));
scopeResolver.Resolve<ILifetimeScope>().IsRegistered<IStoveDbContextConfigurer<ContextBoundObject>>().Should().Be(true);
scopeResolver.Resolve<UserCreatedConsumer>().Consume();
}
}
}
}
Expand Down

0 comments on commit 12f5b13

Please sign in to comment.