- linfupart4_samples.zip
- Simple.IoC.Examples
- bin
- Simple.IoC.dll
- Simple.IoC.Loaders.dll
- ConstructorInjection
- DependencyInjectionSample
- Extensibility
- PropertyInjectionSample
- Simple.IoC.Examples.sln
- linfu_src.zip
- LinFu.Common
- LinFu.Delegates
- LinFu.DynamicProxy
- LinFu.License.txt
- LinFu.Reflection
- NMock2.dll
- nunit.core.dll
- nunit.framework.dll
- Simple.IoC
- Simple.IoC.Loaders
- Simple.IoC.Tests
- Simple.IoC
- linfu_bin.zip
- LinFu.Common.dll
- LinFu.Delegates.dll
- LinFu.DynamicProxy.dll
- LinFu.License.txt
- LinFu.Reflection.dll
- NMock2.dll
- nunit.core.dll
- nunit.framework.dll
- Simple.IoC.dll
- Simple.IoC.Loaders.dll
|
using System;
using System.Collections.Generic;
using System.Text;
using Simple.IoC.Factories;
namespace Simple.IoC.Loaders
{
public class AutoFactoryLoader : BaseFactoryLoader
{
private static readonly Dictionary<LifecycleType, Type>
_typeMap = new Dictionary<LifecycleType, Type>();
static AutoFactoryLoader()
{
_typeMap[LifecycleType.OncePerRequest] = typeof (OncePerRequestFactory<,>);
_typeMap[LifecycleType.OncePerThread] = typeof (OncePerThreadFactory<,>);
_typeMap[LifecycleType.Singleton] = typeof (SingletonFactory<,>);
}
protected override object CreateFactory(Type factoryInterfaceType, Type implementingType, Type serviceType)
{
object factoryInstance = null;
object[] attributes = implementingType.GetCustomAttributes(typeof (ImplementsAttribute), true);
foreach(object attribute in attributes)
{
ImplementsAttribute currentAttribute = attribute as ImplementsAttribute;
if (currentAttribute == null)
continue;
// Match the factory with the service type and instantiate it
if (currentAttribute.ServiceType != serviceType)
continue;
Type factoryGenericType = _typeMap[currentAttribute.LifeCycleType];
Type factoryType = factoryGenericType.MakeGenericType(serviceType, implementingType);
factoryInstance = Activator.CreateInstance(factoryType);
break;
}
return factoryInstance;
}
public override void LoadFactory(IContainer container, Type loadedType)
{
// Load any custom factories associated with the assembly
CustomFactoryLoader factoryLoader = new CustomFactoryLoader();
factoryLoader.LoadFactory(container, loadedType);
base.LoadFactory(container, loadedType);
}
protected override bool CanLoad(Type loadedType)
{
if (loadedType.IsAbstract || loadedType.IsInterface || !loadedType.IsClass)
return false;
if (!loadedType.IsDefined(typeof(ImplementsAttribute), true))
return false;
return true;
}
protected override IEnumerable<Type> GetItemTypes(Type currentType)
{
List<Type> results = new List<Type>();
foreach (object currentAttribute in currentType.GetCustomAttributes(true))
{
if (!(currentAttribute is ImplementsAttribute))
continue;
ImplementsAttribute attribute = currentAttribute as ImplementsAttribute;
results.Add(attribute.ServiceType);
}
return results;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.