forked from scriptcs/scriptcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptServicesRegistration.cs
More file actions
64 lines (52 loc) · 1.75 KB
/
ScriptServicesRegistration.cs
File metadata and controls
64 lines (52 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Collections.Generic;
using Autofac;
using Common.Logging;
namespace ScriptCs
{
public abstract class ScriptServicesRegistration
{
private readonly IDictionary<Type, object> _overrides = null;
protected ILog Logger { get; private set; }
public ScriptServicesRegistration(ILog logger, IDictionary<Type, object> overrides)
{
_overrides = overrides ?? new Dictionary<Type, object>();
Logger = logger;
}
protected void RegisterOverrideOrDefault<T>(ContainerBuilder builder, Action<ContainerBuilder> registrationAction)
{
Guard.AgainstNullArgument("registrationAction", registrationAction);
if (_overrides.ContainsKey(typeof(T)))
{
var reg = _overrides[typeof(T)];
this.Logger.Debug(string.Format("Registering override: {0}", reg));
if (reg.GetType().IsSubclassOf(typeof(Type)))
{
builder.RegisterType((Type)reg).As<T>().SingleInstance();
}
else
{
builder.RegisterInstance(reg).As<T>();
}
}
else
{
this.Logger.Debug(string.Format("Registering default: {0}", typeof(T)));
registrationAction(builder);
}
}
private IContainer _container;
public IContainer Container
{
get
{
if (_container == null)
{
_container = CreateContainer();
}
return _container;
}
}
protected abstract IContainer CreateContainer();
}
}