Created
April 21, 2010 18:03
-
-
Save darrenkopp/374171 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.Collections.Generic; | |
using System.Linq; | |
using Autofac; | |
using Autofac.Builder; | |
using NServiceBus.ObjectBuilder.Autofac.Internal; | |
using Autofac.Core; | |
namespace NServiceBus.ObjectBuilder.Autofac | |
{ | |
///<summary> | |
/// Autofac implementation of IContainer. | |
///</summary> | |
internal class AutofacObjectBuilder : Common.IContainer | |
{ | |
/// <summary> | |
/// The container itself. | |
/// </summary> | |
private readonly IContainer container; | |
///<summary> | |
/// Instantiates the class saving the given container. | |
///</summary> | |
///<param name="container"></param> | |
public AutofacObjectBuilder(IContainer container) | |
{ | |
this.container = container; | |
} | |
///<summary> | |
/// Instantites the class with a new Autofac container. | |
///</summary> | |
public AutofacObjectBuilder() | |
: this(new ContainerBuilder().Build()) | |
{ | |
} | |
///<summary> | |
/// Build an instance of a given type using Autofac. | |
///</summary> | |
///<param name="typeToBuild"></param> | |
///<returns></returns> | |
public object Build(Type typeToBuild) | |
{ | |
return container.Resolve(typeToBuild); | |
} | |
///<summary> | |
/// Build all instances of a given type using Autofac. | |
///</summary> | |
///<param name="typeToBuild"></param> | |
///<returns></returns> | |
public IEnumerable<object> BuildAll(Type typeToBuild) | |
{ | |
return container.ResolveAll(typeToBuild); | |
} | |
void Common.IContainer.Configure(Type component, ComponentCallModelEnum callModel) | |
{ | |
IComponentRegistration registration = GetComponentRegistration(component); | |
if (registration == null) | |
{ | |
Type[] services = component.GetAllServices().ToArray(); | |
var builder = RegistrationBuilder | |
.ForType(component) | |
.As(services) | |
.OnActivating(x => x.Instance = x.Context.InjectUnsetProperties(x.Instance)); | |
if (callModel == ComponentCallModelEnum.Singleton) | |
builder = builder.SingleInstance(); | |
container.ComponentRegistry.Register(builder.CreateRegistration()); | |
} | |
//IComponentRegistration registration = GetComponentRegistration(component); | |
//if (registration == null) | |
//{ | |
// var builder = new ContainerBuilder(); | |
// InstanceScope instanceScope = GetInstanceScopeFrom(callModel); | |
// Type[] services = component.GetAllServices().ToArray(); | |
// builder.Register(component).As(services).WithScope(instanceScope).OnActivating(ActivatingHandler.InjectUnsetProperties); | |
// builder.Build(container); | |
//} | |
} | |
///<summary> | |
/// Configure the value of a named component property. | |
///</summary> | |
///<param name="component"></param> | |
///<param name="property"></param> | |
///<param name="value"></param> | |
public void ConfigureProperty(Type component, string property, object value) | |
{ | |
var registration = GetComponentRegistration(component); | |
if (registration == null) | |
{ | |
return; | |
} | |
registration.Activating += (sender, e) => e.Instance.SetPropertyValue(property, value); | |
} | |
///<summary> | |
/// Register a singleton instance of a dependency within Autofac. | |
///</summary> | |
///<param name="lookupType"></param> | |
///<param name="instance"></param> | |
public void RegisterSingleton(Type lookupType, object instance) | |
{ | |
var builder = RegistrationBuilder.ForDelegate(lookupType, (context, parameters) => instance) | |
.ExternallyOwned() | |
.OnActivating(x => x.Context.InjectUnsetProperties(x.Instance)); | |
container.ComponentRegistry.Register(builder.CreateRegistration()); | |
//var builder = new ContainerBuilder(); | |
//builder.Register(instance).As(lookupType).ExternallyOwned().OnActivating(ActivatingHandler.InjectUnsetProperties); | |
//builder.Build(container); | |
} | |
public bool HasComponent(Type componentType) | |
{ | |
return container.IsRegistered(componentType); | |
} | |
private IComponentRegistration GetComponentRegistration(Type concreteComponent) | |
{ | |
return container.GetAllRegistrations().Where(x => x.Activator.LimitType == concreteComponent).FirstOrDefault(); | |
} | |
} | |
} | |
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.Collections.Generic; | |
using System.Linq; | |
using Autofac; | |
using System.Reflection; | |
using Autofac.Core; | |
namespace NServiceBus.ObjectBuilder.Autofac.Internal | |
{ | |
///<summary> | |
/// Autofac IContainer extensions | |
///</summary> | |
internal static class ContainerExtensions | |
{ | |
///<summary> | |
/// Retrieve all registrations across container hierarchies as a single sequence | |
///</summary> | |
///<param name="container"></param> | |
///<returns></returns> | |
public static IEnumerable<IComponentRegistration> GetAllRegistrations(this IContainer container) | |
{ | |
return container.ComponentRegistry.Registrations; | |
//foreach (var registration in container.ComponentRegistry.Registrations) | |
// yield return registration; | |
} | |
///<summary> | |
/// Resolve all components registered for the type. | |
///</summary> | |
///<param name="container"></param> | |
///<param name="componentType"></param> | |
///<returns></returns> | |
public static IEnumerable<object> ResolveAll(this IContainer container, Type componentType) | |
{ | |
return container.Resolve(typeof(IEnumerable<>).MakeGenericType(componentType)) as IEnumerable<object>; | |
} | |
} | |
} |
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.Reflection; | |
namespace NServiceBus.ObjectBuilder.Autofac.Internal | |
{ | |
///<summary> | |
/// Object extension methods | |
///</summary> | |
internal static class ObjectExtensions | |
{ | |
///<summary> | |
/// Set a property value on an instance using reflection | |
///</summary> | |
///<param name="instance"></param> | |
///<param name="propertyName"></param> | |
///<param name="value"></param> | |
public static void SetPropertyValue(this object instance, string propertyName, object value) | |
{ | |
try | |
{ | |
instance.GetType().GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance).SetValue(instance, value, null); | |
} | |
catch (TargetInvocationException ex) | |
{ | |
throw ex.InnerException; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment