-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCsModelStore.cs
More file actions
90 lines (79 loc) · 3.29 KB
/
CsModelStore.cs
File metadata and controls
90 lines (79 loc) · 3.29 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
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
//*****************************************************************************
//* Code Factory SDK
//* Copyright (c) 2020 CodeFactory, LLC
//*****************************************************************************
using System.Collections.Generic;
namespace CodeFactory.DotNet.CSharp
{
/// <summary>
/// Model storage class used to store c# code factory models, to be used to pass data to factories.
/// </summary>
public class CsModelStore
{
#region backing fields for the model data
private ICsModel _model;
private readonly Dictionary<string, IEnumerable<ICsModel>> _models;
#endregion
/// <summary>
/// Sets the single model to be shared with a factory.
/// </summary>
/// <param name="model">Target model to be used in a factory.</param>
public void SetModel(ICsModel model)
{
_model = model;
}
/// <summary>
/// Adds many C# models to the store by category.
/// </summary>
/// <param name="category">Name used to keep track of the models that are stored together.</param>
/// <param name="models">The models stored by the target category.</param>
public void AddModels(string category, IEnumerable<ICsModel> models)
{
if (_models.ContainsKey(category))
{
_models.Remove(category);
}
_models.Add(category, models);
}
/// <summary>
/// Creates a new instance of the <see cref="CsModelStore"/> and initializes the store for data to be added.
/// </summary>
public CsModelStore()
{
_model = null;
_models = new Dictionary<string, IEnumerable<ICsModel>>();
}
/// <summary>
/// Creates a new instance of the <see cref="CsModelStore"/> and sets a single model in the store.
/// </summary>
/// <param name="model">The model to be added to the store.</param>
public CsModelStore(ICsModel model)
{
_model = model;
}
/// <summary>
/// Creates a new instance of the <see cref="CsModelStore"/> and loads all the data for all the categories.
/// </summary>
/// <param name="allModels">All the model data to be added to the store.</param>
public CsModelStore(Dictionary<string, IEnumerable<ICsModel>> allModels)
{
_model = null;
_models = allModels ?? new Dictionary<string, IEnumerable<ICsModel>>();
}
/// <summary>
/// The single <see cref="ICsModel"/> that is provided for the T4 Template.
/// </summary>
public ICsModel Model => _model;
/// <summary>
/// Gets the models from a target category.
/// </summary>
/// <param name="category">Category to get models for.</param>
/// <returns>Returns a enumeration of the models. If no models were found then an empty enumeration is returned.</returns>
public IEnumerable<ICsModel> Models(string category)
{
if (string.IsNullOrEmpty(category)) return new List<ICsModel>();
var getModels = _models.TryGetValue(category, out var models);
return getModels ? models : new List<ICsModel>();
}
}
}