Skip to content

Instantly share code, notes, and snippets.

@ewilde
Created January 28, 2013 14:00
Show Gist options
  • Save ewilde/4655700 to your computer and use it in GitHub Desktop.
Save ewilde/4655700 to your computer and use it in GitHub Desktop.
#structuremap instance factory pattern
public class Example
{
public IInstanceFactory InstanceFactory { get; set; }
public void GetInstanceWithArgument()
{
var foo = this.InstanceFactory<Employee, string>("ArgumentValue1")
}
}
using System;
/// <summary>
/// The InstanceFactory interface.
/// </summary>
public interface IInstanceFactory
{
/// <summary>
/// The get instance.
/// </summary>
/// <typeparam name="T">
/// Return type
/// </typeparam>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
T GetInstance<T>();
/// <summary>
/// The get instance.
/// </summary>
/// <param name="argumentTypes">
/// The argument types.
/// </param>
/// <param name="values">
/// The values.
/// </param>
/// <typeparam name="T">
/// Return type
/// </typeparam>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
T GetInstance<T>(Type[] argumentTypes, object[] values) where T : class;
/// <summary>
/// The get instance.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <typeparam name="TObject">
/// Return type
/// </typeparam>
/// <typeparam name="TArgument">
/// Argument type
/// </typeparam>
/// <returns>
/// The <see cref="TObject"/>.
/// </returns>
TObject GetInstance<TObject, TArgument>(TArgument value) where TObject : class;
}
using System;
using StructureMap;
/// <summary>
/// The instance factory.
/// </summary>
public class InstanceFactory : IInstanceFactory
{
/// <summary>
/// The get instance.
/// </summary>
/// <typeparam name="T">
/// Return type
/// </typeparam>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
public T GetInstance<T>()
{
return ObjectFactory.GetInstance<T>();
}
/// <summary>
/// The get instance.
/// </summary>
/// <param name="argumentTypes">
/// The argument types.
/// </param>
/// <param name="values">
/// The values.
/// </param>
/// <typeparam name="T">
/// Return type
/// </typeparam>
/// <returns>
/// The <see cref="T"/>.
/// </returns>
public T GetInstance<T>(Type[] argumentTypes, object[] values) where T : class
{
if (argumentTypes == null || argumentTypes.Length == 0)
{
return null;
}
var instance = ObjectFactory.With(argumentTypes[0], values[0]);
for (int i = 1; i < argumentTypes.Length; i++)
{
instance = instance.With(argumentTypes[i], values[i]);
}
return instance.GetInstance<T>();
}
/// <summary>
/// The get instance.
/// </summary>
/// <param name="value">
/// The value.
/// </param>
/// <typeparam name="TObject">
/// Return type
/// </typeparam>
/// <typeparam name="TArgument">
/// Argument type
/// </typeparam>
/// <returns>
/// The <see cref="TObject"/>.
/// </returns>
public TObject GetInstance<TObject, TArgument>(TArgument value) where TObject : class
{
return ObjectFactory.With<TArgument>(value).GetInstance<TObject>();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment