Created
January 28, 2013 14:00
-
-
Save ewilde/4655700 to your computer and use it in GitHub Desktop.
#structuremap instance factory pattern
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
public class Example | |
{ | |
public IInstanceFactory InstanceFactory { get; set; } | |
public void GetInstanceWithArgument() | |
{ | |
var foo = this.InstanceFactory<Employee, string>("ArgumentValue1") | |
} | |
} |
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; | |
/// <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; | |
} |
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 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