Skip to content

Commit

Permalink
Reverted delegate factories to generic - makes more sense
Browse files Browse the repository at this point in the history
  • Loading branch information
grumpydev committed Sep 8, 2011
1 parent b95f61c commit 2feec98
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/TinyIoC/TinyIoC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// An easy to use, hassle free, Inversion of Control Container for small projects
// and beginners alike.
//
// http://hg.grumpydev.com/tinyioc
// https://github.com/grumpydev/TinyIoC
//===============================================================================
// Copyright © Steven Robbins. All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
Expand Down Expand Up @@ -971,8 +971,7 @@ public RegisterOptions Register<RegisterType, RegisterImplementation>(RegisterIm
public RegisterOptions Register<RegisterType>(Func<TinyIoCContainer, NamedParameterOverloads, RegisterType> factory)
where RegisterType : class
{
throw new NotImplementedException();
//return RegisterInternal(typeof(RegisterType), string.Empty, new DelegateFactory(typeof(RegisterType), factory));
return RegisterInternal(typeof(RegisterType), string.Empty, new DelegateFactory<RegisterType>(factory));
}

/// <summary>
Expand All @@ -985,8 +984,7 @@ public RegisterOptions Register<RegisterType>(Func<TinyIoCContainer, NamedParame
public RegisterOptions Register<RegisterType>(Func<TinyIoCContainer, NamedParameterOverloads, RegisterType> factory, string name)
where RegisterType : class
{
throw new NotImplementedException();
//return RegisterInternal(typeof(RegisterType), name, new DelegateFactory<RegisterType>(factory));
return RegisterInternal(typeof(RegisterType), name, new DelegateFactory<RegisterType>(factory));
}

/// <summary>
Expand Down Expand Up @@ -2111,14 +2109,15 @@ public override ObjectFactoryBase MultiInstanceVariant
/// <summary>
/// IObjectFactory that invokes a specified delegate to construct the object
/// </summary>
private class DelegateFactory : ObjectFactoryBase
/// <typeparam name="RegisterType">Registered type to be constructed</typeparam>
private class DelegateFactory<RegisterType> : ObjectFactoryBase
where RegisterType : class
{
private readonly Type registerType;
private Func<TinyIoCContainer, NamedParameterOverloads, object> _factory;
private Func<TinyIoCContainer, NamedParameterOverloads, RegisterType> _factory;

public override bool AssumeConstruction { get { return true; } }

public override Type CreatesType { get { return this.registerType; } }
public override Type CreatesType { get { return typeof(RegisterType); } }

public override object GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)
{
Expand All @@ -2128,24 +2127,23 @@ public override object GetObject(Type requestedType, TinyIoCContainer container,
}
catch (Exception ex)
{
throw new TinyIoCResolutionException(this.registerType, ex);
throw new TinyIoCResolutionException(typeof(RegisterType), ex);
}
}

public DelegateFactory(Type registerType, Func<TinyIoCContainer, NamedParameterOverloads, object> factory)
public DelegateFactory(Func<TinyIoCContainer, NamedParameterOverloads, RegisterType> factory)
{
if (factory == null)
throw new ArgumentNullException("factory");

this.registerType = registerType;
_factory = factory;
}

public override ObjectFactoryBase WeakReferenceVariant
{
get
{
return new WeakDelegateFactory(this.registerType, _factory);
return new WeakDelegateFactory<RegisterType>(_factory);
}
}

Expand All @@ -2165,39 +2163,37 @@ public override void SetConstructor(ConstructorInfo constructor)

/// <summary>
/// IObjectFactory that invokes a specified delegate to construct the object
///
/// Holds the delegate using a weak reference
/// </summary>
/// <typeparam name="RegisterType">Registered type to be constructed</typeparam>
private class WeakDelegateFactory : ObjectFactoryBase
private class WeakDelegateFactory<RegisterType> : ObjectFactoryBase
where RegisterType : class
{
private readonly Type registerType;
private WeakReference _factory;

public override bool AssumeConstruction { get { return true; } }

public override Type CreatesType { get { return this.registerType; } }
public override Type CreatesType { get { return typeof(RegisterType); } }

public override object GetObject(Type requestedType, TinyIoCContainer container, NamedParameterOverloads parameters, ResolveOptions options)
{
var factory = _factory.Target as Func<TinyIoCContainer, NamedParameterOverloads, object>;
var factory = _factory.Target as Func<TinyIoCContainer, NamedParameterOverloads, RegisterType>;

if (factory == null)
throw new TinyIoCWeakReferenceException(this.registerType);
throw new TinyIoCWeakReferenceException(typeof(RegisterType));

try
{
return factory.Invoke(container, parameters);
}
catch (Exception ex)
{
throw new TinyIoCResolutionException(this.registerType, ex);
throw new TinyIoCResolutionException(typeof(RegisterType), ex);
}
}

public WeakDelegateFactory(Type registerType, Func<TinyIoCContainer, NamedParameterOverloads, object> factory)
public WeakDelegateFactory(Func<TinyIoCContainer, NamedParameterOverloads, RegisterType> factory)
{
this.registerType = registerType;
if (factory == null)
throw new ArgumentNullException("factory");

Expand All @@ -2208,12 +2204,12 @@ public override ObjectFactoryBase StrongReferenceVariant
{
get
{
var factory = _factory.Target as Func<TinyIoCContainer, NamedParameterOverloads, object>;
var factory = _factory.Target as Func<TinyIoCContainer, NamedParameterOverloads, RegisterType>;

if (factory == null)
throw new TinyIoCWeakReferenceException(this.registerType);
throw new TinyIoCWeakReferenceException(typeof(RegisterType));

return new DelegateFactory(this.registerType, factory);
return new DelegateFactory<RegisterType>(factory);
}
}

Expand Down

0 comments on commit 2feec98

Please sign in to comment.