Skip to content

Commit e74fb49

Browse files
committed
Add attributes for initializing DI.
Version up to push changes.
1 parent e59ea03 commit e74fb49

File tree

11 files changed

+30
-14
lines changed

11 files changed

+30
-14
lines changed

ConsoleHero.InjectionExample/Data.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Drawing;
33

44
namespace ConsoleHero.InjectionExample;
5+
[Singleton]
56
public class Data
67
{
78
public string Name { get; set; } = "Person";

ConsoleHero.InjectionExample/Logic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace ConsoleHero.InjectionExample;
22

3+
[Singleton]
34
public class Logic
45
{
56
public void ReviewPlayer(Player player)

ConsoleHero.InjectionExample/Menus.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using static ConsoleHero.MenuBuilder;
33

44
namespace ConsoleHero.InjectionExample;
5+
[Singleton]
56
public class Menus(Paragraphs paragraphs, Requests requests, Tunes tunes, Data data, Logic logic)
67
{
78
private readonly Paragraphs _paragraphs = paragraphs;

ConsoleHero.InjectionExample/Paragraphs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using static ConsoleHero.ParagraphBuilder;
33

44
namespace ConsoleHero.InjectionExample;
5+
[Singleton]
56
public class Paragraphs(Data data)
67
{
78
private readonly Data _data = data;

ConsoleHero.InjectionExample/Program.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
11
using System.Drawing;
2-
using ConsoleHero.Injection;
32

43
namespace ConsoleHero.InjectionExample;
54

65
public static class Program
76
{
87
private static void Main()
98
{
10-
GlobalSettings.Content = Host.
11-
Singleton<Logic>().
12-
Singleton<Data>().
13-
Singleton<Menus>().
14-
Singleton<Paragraphs>().
15-
Singleton<Requests>().
16-
Singleton<Tunes>().
17-
Build();
18-
199
GlobalSettings.Spacing = 2;
2010
GlobalSettings.DefaultTextColor = Color.LightBlue;
2111

ConsoleHero.InjectionExample/Requests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using static ConsoleHero.RequestBuilder;
22

33
namespace ConsoleHero.InjectionExample;
4-
4+
[Singleton]
55
public class Requests(Paragraphs paragraphs, Data data)
66
{
77
private readonly Paragraphs _paragraphs = paragraphs;

ConsoleHero.InjectionExample/Tunes.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using static ConsoleHero.TuneBuilder;
22

33
namespace ConsoleHero.InjectionExample;
4+
[Singleton]
45
public class Tunes
56
{
67
public Tune Mary =>

ConsoleHero/ConsoleHero.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
99
<PackageId>ConsoleHero</PackageId>
10-
<Version>0.3.8</Version>
10+
<Version>0.3.9</Version>
1111
<Authors>Derek Gooding</Authors>
1212
<Description>A library for making quick menus in a console application.</Description>
1313
<PackageTags>Console;FluentAPI;Menu;CLI;ConsoleApp;CSharp;UserInterface;MenuNavigation;Scalable;ConsoleMenu;Fluent;</PackageTags>

ConsoleHero/GlobalSettings.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ConsoleHero.Injection;
22
using ConsoleHero.Interfaces;
33
using ConsoleHero.Services;
4+
using System.Reflection;
45

56
namespace ConsoleHero;
67

@@ -46,7 +47,17 @@ public static IColorService ColorService
4647
private static IConsoleService? _service;
4748
private static IColorService? _colorService;
4849

49-
public static Host Content { get; set; } = new Host();
50+
public static Host Content { get; set; } = InitializeContent();
51+
52+
private static Host InitializeContent()
53+
{
54+
Host host = new Host(AppDomain.CurrentDomain.GetAssemblies()
55+
.SelectMany(x=>x.GetTypes())
56+
.Where(t => t.GetCustomAttributes(typeof(SingletonAttribute), false).Length != 0)
57+
.Select(x => new Singleton(x))
58+
.ToList());
59+
return host.map.Count == 0 ? new Host() : host;
60+
}
5061

5162
public static T Get<T>() where T : class
5263
=> Content.map.ContainsKey(typeof(T)) ? Content.Get<T>()

ConsoleHero/Injection/Host.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
namespace ConsoleHero.Injection;
22
public class Host
33
{
4+
internal Host(List<Singleton> list)
5+
{
6+
singletons = list;
7+
Initialize();
8+
}
9+
410
internal Host() { }
511

612
private readonly List<Singleton> singletons = new();
@@ -35,7 +41,7 @@ public Host Build()
3541

3642
private void Initialize()
3743
{
38-
List<Singleton> toProcess = new(singletons);
44+
List<Singleton> toProcess = singletons.ToList();
3945
for (int i = 0; i < toProcess.Count; i++)
4046
{
4147
Singleton singleton = toProcess[i];

0 commit comments

Comments
 (0)