-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutofacExtension.cs
48 lines (42 loc) · 1.68 KB
/
AutofacExtension.cs
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
using System;
using Autofac;
namespace boss.client.win
{
public static class AutofacExtension
{
public static Page ResolvePage(this IComponentContext container, string code, object parameter)
{
var result = container.ResolveOptionalNamed<Page>(GetPageFullName(code), new NamedParameter("parameter", parameter));
if (result == null) throw new ApplicationError("message.unimplemented-page-resolved", code);
return result;
}
public static void RegisterAllTypes(this ContainerBuilder builder)
{
RegisterAllServices(builder);
RegisterAllPages(builder);
}
private static void RegisterAllServices(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(x => x.GetCustomAttributes(typeof(ServiceAttribute), false).Length > 0)
.AsImplementedInterfaces()
.AsSelf()
.InstancePerDependency();
}
private static void RegisterAllPages(ContainerBuilder builder)
{
builder.RegisterAssemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
.Where(x => x.GetCustomAttributes(typeof(PageAttribute), false).Length > 0)
.Named<Page>(x => GetPageFullName(((PageAttribute)x.GetCustomAttributes(typeof(PageAttribute), false)[0]).Code))
.InstancePerDependency();
}
private static string GetPagePrefix()
{
return "PAGE.";
}
private static string GetPageFullName(string code)
{
return GetPagePrefix() + code;
}
}
}