-
Notifications
You must be signed in to change notification settings - Fork 164
/
DotNetifyConfiguration.cs
102 lines (85 loc) · 3.74 KB
/
DotNetifyConfiguration.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Copyright 2017 Dicky Suryadi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
using System;
using System.ComponentModel;
using System.Reflection;
using Newtonsoft.Json;
namespace DotNetify
{
/// <summary>
/// Provides startup configuration options.
/// </summary>
public class DotNetifyConfiguration : IDotNetifyConfiguration
{
/// <summary>
/// How long to keep a view model controller in memory after it hasn't been accessed for a while. Default to never expire.
/// </summary>
public TimeSpan? VMControllerCacheExpiration { get; set; }
/// <summary>
/// Provides a factory method to create view model instances.
/// The method accepts a class type and constructor arguments, and returns an instance of that type.
/// </summary>
public void SetFactoryMethod(Func<Type, object[], object> factoryMethod) => VMController.CreateInstance = (type, args) => factoryMethod(type, args);
/// <summary>
/// Provides custom global json serializer settings.
/// </summary>
public void UseJsonSerializerSettings(JsonSerializerSettings settings) => VMSerializer.SerializerSettings = settings;
/// <summary>
/// Register view model classes in an assembly that are subtypes of BaseVM.
/// </summary>
public void RegisterAssembly(Assembly assembly) => VMController.RegisterAssembly(assembly);
/// <summary>
/// Register view model classes in an assembly that are subtypes of BaseVM.
/// </summary>
public void RegisterAssembly(string assemblyName) => VMController.RegisterAssembly(Assembly.Load(new AssemblyName(assemblyName)));
/// <summary>
/// Register view model classes in an assembly that are subtypes of a certain type.
/// </summary>
public void RegisterAssembly<T>(Assembly assembly) where T : INotifyPropertyChanged => VMController.RegisterAssembly<T>(assembly);
/// <summary>
/// Register view model classes in an assembly that are subtypes of a certain type.
/// </summary>
public void RegisterAssembly<T>(string assemblyName) where T : INotifyPropertyChanged => VMController.RegisterAssembly<T>(Assembly.Load(new AssemblyName(assemblyName)));
/// <summary>
/// Register a specific view model class type.
/// </summary>
public IDotNetifyConfiguration Register<T>() where T : INotifyPropertyChanged
{
VMController.Register<T>();
return this;
}
/// <summary>
/// Register a specific runtime view model class type.
/// </summary>
public IDotNetifyConfiguration Register(string typeName, Func<object[], INotifyPropertyChanged> factory)
{
VMController.Register(typeName, factory);
return this;
}
#region Methods for internal use
/// <summary>
/// Registers view model classes in the entry assembly.
/// </summary>
public void RegisterEntryAssembly()
{
var entryAssembly = Assembly.GetEntryAssembly();
if (entryAssembly != null)
VMController.RegisterAssembly(entryAssembly);
}
/// <summary>
/// Whether anything has been registered.
/// </summary>
public bool HasAssembly => VMController._registeredAssemblies.Count > 0;
#endregion
}
}