##// END OF EJS Templates
Container configuration cleanup, RC2
Container configuration cleanup, RC2

File last commit:

r279:8714471e8d78 v3
r279:8714471e8d78 v3
Show More
ContainerBuilder.cs
110 lines | 3.5 KiB | text/x-csharp | CSharpLexer
cin
Unity xml configuration, alpha2
r274 using System;
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 using System.Reflection;
using Unity;
cin
Unity xml configuration, alpha2
r274
namespace Implab.ServiceHost.Unity {
public class ContainerBuilder {
readonly TypeResolver m_resolver;
cin
Container configuration cleanup, RC2
r279 readonly IUnityContainer m_container;
cin
Unity xml configuration, alpha2
r274
readonly ContainerConfigurationSchema m_schema;
cin
Container configuration cleanup, RC2
r279 public IUnityContainer Container {
cin
Unity xml configuration, alpha2
r274 get {
return m_container;
}
}
public ContainerBuilder() : this(null, null) {
}
cin
Container configuration cleanup, RC2
r279 public ContainerBuilder(IUnityContainer container, ContainerConfigurationSchema schema) {
cin
Unity xml configuration, alpha2
r274 m_container = container ?? new UnityContainer();
m_resolver = new TypeResolver();
m_schema = schema ?? ContainerConfigurationSchema.Default;
}
public Type ResolveType(string typeReference) {
cin
Container configuration cleanup, RC2
r279 return string.IsNullOrEmpty(typeReference) ? null : m_resolver.Resolve(typeReference, true);
cin
Unity xml configuration, alpha2
r274 }
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 public void Visit(ITypeRegistration registration) {
Safe.ArgumentNotNull(registration, nameof(registration));
var registrationType = registration.GetRegistrationType(this);
var implementationType = registration.GetImplementationType(this) ?? registrationType;
if (registrationType == null)
throw new Exception($"A type must be specified for the registration {registration.Name}");
var builder = new TypeRegistrationBuilder(
m_resolver,
registrationType,
implementationType
);
builder.Lifetime = registration.GetLifetime(this);
if (registration.MemberInjections != null) {
foreach(var member in registration.MemberInjections)
member.Visit(builder);
}
m_container.RegisterType(
builder.RegistrationType,
builder.ImplementationType,
registration.Name,
builder.Lifetime,
builder.Injections
);
cin
Unity xml configuration, alpha2
r274 }
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 public void Visit(IInstanceRegistration registration) {
Safe.ArgumentNotNull(registration, nameof(registration));
var registrationType = registration.GetRegistrationType(this);
var builder = new InstanceRegistrationBuilder (
m_resolver,
registrationType
);
builder.Lifetime = registration.GetLifetime(this);
if (registration.MemberInjections != null) {
foreach(var member in registration.MemberInjections)
member.Visit(builder.ValueBuilder);
}
if (builder.RegistrationType == null && builder.ValueBuilder.ValueType == null)
throw new Exception($"A type must be specified for the registration {registration.Name}");
m_container.RegisterInstance(
builder.RegistrationType ?? builder.ValueBuilder.ValueType,
registration.Name,
cin
Container configuration cleanup, RC2
r279 builder.ValueBuilder.Value,
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 builder.Lifetime
);
cin
Unity xml configuration, alpha2
r274 }
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278 public void AddNamespace(string ns) {
m_resolver.AddNamespace(ns);
}
public void AddAssembly(string assembly) {
cin
Unity xml configuration, alpha2
r274 }
public void Include(string file) {
var includeContext = new ContainerBuilder(m_container, m_schema);
includeContext.LoadConfig(file);
}
public void LoadConfig(string file) {
var config = m_schema.LoadFile(file);
cin
Refactoring...
r277 config.Visit(this);
cin
Unity xml configuration, alpha2
r274 }
cin
Implab: added XmlDefaultSeializer (SerializersPool is now obsolete)...
r278
cin
Unity xml configuration, alpha2
r274 }
}