##// END OF EJS Templates
Added Trace<T>.Debug(...) method for debug messages...
Added Trace<T>.Debug(...) method for debug messages Added ContainerBuilde.LoadConfig(Uri) method

File last commit:

r279:8714471e8d78 v3
r280:f07be402ab02 v3
Show More
InjectionValueBuilder.cs
136 lines | 4.5 KiB | text/x-csharp | CSharpLexer
cin
Unity xml configuration, alpha2
r274 using System;
cin
Container configuration cleanup, RC2
r279 using System.Collections;
cin
Refactoring...
r277 using System.Collections.Generic;
cin
Unity xml configuration, alpha2
r274 using System.ComponentModel;
cin
Refactoring...
r277 using System.Linq;
cin
Unity xml configuration, alpha2
r274 using System.Xml.Serialization;
using Unity.Injection;
namespace Implab.ServiceHost.Unity {
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 public class InjectionParameterBuilder {
cin
Unity xml configuration, alpha2
r274
readonly TypeResolver m_resolver;
public Type DefaultType { get; private set; }
cin
Refactoring...
r277 public Type ValueType { get; private set; }
cin
Unity xml configuration, alpha2
r274
cin
Container configuration cleanup, RC2
r279 object m_value;
cin
Unity xml configuration, alpha2
r274
cin
Container configuration cleanup, RC2
r279 public object Value {
cin
Unity xml configuration, alpha2
r274 get {
cin
Container configuration cleanup, RC2
r279 if (!ValueSpecified)
throw new InvalidOperationException("The regular value must be set (dependency or array are not situable in this context)");
return m_value;
}
}
public bool ValueSpecified { get; private set; }
InjectionParameterValue m_injection;
cin
Unity xml configuration, alpha2
r274
cin
Container configuration cleanup, RC2
r279 public InjectionParameterValue Injection {
get {
if (m_injection == null)
throw new InvalidOperationException("The injection parameter is not specified");
return m_injection;
cin
Unity xml configuration, alpha2
r274 }
}
cin
Container configuration cleanup, RC2
r279 public bool InjectionSpecified {
get { return m_injection != null; }
}
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 internal InjectionParameterBuilder(TypeResolver resolver, Type defaultType) {
cin
Unity xml configuration, alpha2
r274 m_resolver = resolver;
cin
Refactoring...
r277 DefaultType = defaultType;
cin
Unity xml configuration, alpha2
r274 }
cin
Refactoring...
r277 public Type ResolveInjectedValueType(string typeSpec) {
cin
Unity xml configuration, alpha2
r274 if (string.IsNullOrEmpty(typeSpec)) {
if (DefaultType == null)
throw new Exception("The type must be specified");
return DefaultType;
}
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 return m_resolver.Resolve(typeSpec, true);
cin
Unity xml configuration, alpha2
r274 }
cin
Refactoring...
r277 public Type ResolveType(string typeSpec) {
cin
Container configuration cleanup, RC2
r279 return string.IsNullOrEmpty(typeSpec) ? null : m_resolver.Resolve(typeSpec, true);
cin
Refactoring...
r277 }
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 public void SetValue(Type type, object value) {
cin
Container configuration cleanup, RC2
r279 Safe.ArgumentNotNull(type, nameof(type));
cin
Refactoring...
r277 ValueType = type;
cin
Container configuration cleanup, RC2
r279 m_value = value;
ValueSpecified = true;
m_injection = new InjectionParameter(type, value);
cin
Refactoring...
r277 }
cin
Container configuration cleanup, RC2
r279 public void SetDependency(Type type, string name, bool optional) {
Safe.ArgumentNotNull(type, nameof(type));
cin
Refactoring...
r277
ValueType = type;
cin
Container configuration cleanup, RC2
r279 ValueSpecified = false;
m_value = null;
m_injection = optional ? (InjectionParameterValue)new OptionalParameter(type, name) : new ResolvedParameter(type, name);
cin
Unity xml configuration, alpha2
r274 }
cin
Refactoring...
r277 internal void Visit(ArrayParameterElement arrayParameter) {
Type itemsType = null;
var arrayType = string.IsNullOrEmpty(arrayParameter.TypeName) ? null : ResolveType(arrayParameter.TypeName);
cin
Unity xml configuration, alpha2
r274
cin
Container configuration cleanup, RC2
r279 if (arrayType == null)
arrayType = DefaultType;
cin
Refactoring...
r277 if (!string.IsNullOrEmpty(arrayParameter.ItemsType)) {
itemsType = ResolveType(arrayParameter.ItemsType);
cin
Container configuration cleanup, RC2
r279 arrayType = itemsType.MakeArrayType();
cin
Refactoring...
r277 } else {
cin
Container configuration cleanup, RC2
r279 itemsType = GetItemsType(arrayType);
cin
Refactoring...
r277 }
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 if (itemsType == null)
throw new Exception("Failed to determine array elements type");
cin
Unity xml configuration, alpha2
r274
cin
Refactoring...
r277 InjectionParameterValue[] injections = (arrayParameter.Items ?? new InjectionParameterElement[0])
.Select(x => {
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 var builder = new InjectionParameterBuilder(m_resolver, itemsType);
cin
Refactoring...
r277 x.Visit(builder);
return builder.Injection;
})
.ToArray();
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 var array = itemsType.IsGenericParameter ?
cin
Container configuration cleanup, RC2
r279 (InjectionParameterValue)new GenericResolvedArrayParameter(itemsType.Name, injections) :
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 new ResolvedArrayParameter(itemsType, injections);
cin
Refactoring...
r277 ValueType = arrayType;
cin
Container configuration cleanup, RC2
r279 m_value = null;
ValueSpecified = false;
m_injection = array;
}
Type GetItemsType(Type collectionType) {
if (collectionType == null)
return null;
Type itemsType = null;
if (collectionType.GetGenericTypeDefinition() == typeof(IEnumerable<>)) {
itemsType = collectionType.GetGenericArguments()[0];
} else if (collectionType == typeof(IEnumerable)) {
itemsType = typeof(object);
} else {
itemsType = collectionType.GetInterface(typeof(IEnumerable<>).FullName)?.GetGenericArguments()[0];
}
return itemsType;
cin
Unity xml configuration, alpha2
r274 }
}
}