Skip to content

Commit bd46c81

Browse files
committed
Dependency injection is actually good now
1 parent 039a55f commit bd46c81

File tree

9 files changed

+226
-215
lines changed

9 files changed

+226
-215
lines changed

WriterSharp.PluginApi/DependencyInjection/IDependencyInjector.cs

Lines changed: 0 additions & 48 deletions
This file was deleted.

WriterSharp.PluginApi/IWriterSharpPlugin.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,35 @@
1-
namespace WriterSharp.PluginApi
1+
using WriterSharp.PluginApi.Sharing;
2+
3+
4+
namespace WriterSharp.PluginApi
25
{
36

47
/// <summary>
58
/// Represents a plugin to extend WriterSharp's functionalities.
69
/// </summary>
7-
public interface IWriterSharpPlugin
10+
public interface IWriterSharpPlugin : IService
811
{
912

1013
/// <summary>
11-
/// A unique ID for the plugin.
12-
/// One may use a GUID if their ID would be otherwise too common.
14+
/// The author of the plugin.
1315
/// </summary>
14-
string Id { get; init; }
16+
string Author { get; init; }
1517

1618
/// <summary>
17-
/// The name of the plugin.
19+
/// A brief description of the plugin.
1820
/// </summary>
19-
string Name { get; init; }
21+
string Description { get; init; }
2022

2123
/// <summary>
22-
/// A brief description of the plugin.
24+
/// A unique ID for the plugin.
25+
/// One may use a GUID if their ID would be otherwise too common.
2326
/// </summary>
24-
string Description { get; init; }
27+
string Id { get; init; }
2528

2629
/// <summary>
27-
/// The author of the plugin.
30+
/// The name of the plugin.
2831
/// </summary>
29-
string Author { get; init; }
32+
string Name { get; init; }
3033

3134
/// <summary>
3235
/// The current version of the plugin.

WriterSharp.PluginApi/Settings/ISettingsManager.cs

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ namespace WriterSharp.PluginApi.Settings
1414
public interface ISettingsManager
1515
{
1616

17-
/// <summary>
18-
/// Checks if the settings file is in use by another plugin.
19-
/// </summary>
20-
/// <returns><c>true</c> if in use</returns>
21-
bool InUse();
22-
2317
/// <summary>
2418
/// Gets the plugin's section.
2519
/// </summary>
@@ -28,10 +22,12 @@ public interface ISettingsManager
2822
Task<Dictionary<string, string>> GetSectionAsync();
2923

3024
/// <summary>
31-
/// Update's the plugin's section.
25+
/// Gets the plugin's section.
3226
/// </summary>
33-
/// <param name="section">The new version of the section</param>
34-
void UpdateSectionAsync(Dictionary<string, string> section);
27+
/// <param name="cancellationToken">A cancellation token to stop WriterSharp from getting the section</param>
28+
/// <returns>The section, as a dictionary</returns>
29+
/// <remarks>Useful if you're going to be editing the settings a lot.</remarks>
30+
Task<Dictionary<string, string>> GetSectionAsync(CancellationToken cancellationToken);
3531

3632
// ReSharper disable once GrammarMistakeInComment
3733
/// <summary>
@@ -41,6 +37,20 @@ public interface ISettingsManager
4137
/// <returns>A string or <c>null</c> if the value was <c>nil</c>.</returns>
4238
Task<string?> GetValueAsync(string key);
4339

40+
/// <summary>
41+
/// Retrieves a value from the plugin's section given a key. If the key is missing, an exception is thrown.
42+
/// </summary>
43+
/// <param name="key">The key that matches the value</param>
44+
/// <param name="cancellationToken">A cancellation token</param>
45+
/// <returns>A string or <c>null</c> if the value was <c>nil</c>.</returns>
46+
Task<string?> GetValueAsync(string key, CancellationToken cancellationToken);
47+
48+
/// <summary>
49+
/// Checks if the settings file is in use by another plugin.
50+
/// </summary>
51+
/// <returns><c>true</c> if in use</returns>
52+
bool InUse();
53+
4454
/// <summary>
4555
/// Sets an existing key to match a value or adds a new one if necessary.
4656
/// </summary>
@@ -87,29 +97,6 @@ public interface ISettingsManager
8797
/// <param name="pair">A key-value pair containing a key string and a value boolean</param>
8898
Task SetValueAsync(KeyValuePair<string, bool> pair);
8999

90-
/// <summary>
91-
/// Gets the plugin's section.
92-
/// </summary>
93-
/// <param name="cancellationToken">A cancellation token to stop WriterSharp from getting the section</param>
94-
/// <returns>The section, as a dictionary</returns>
95-
/// <remarks>Useful if you're going to be editing the settings a lot.</remarks>
96-
Task<Dictionary<string, string>> GetSectionAsync(CancellationToken cancellationToken);
97-
98-
/// <summary>
99-
/// Update's the plugin's section.
100-
/// </summary>
101-
/// <param name="section">The new version of the section</param>
102-
/// <param name="cancellationToken">A cancellation token</param>
103-
void UpdateSectionAsync(Dictionary<string, string> section, CancellationToken cancellationToken);
104-
105-
/// <summary>
106-
/// Retrieves a value from the plugin's section given a key. If the key is missing, an exception is thrown.
107-
/// </summary>
108-
/// <param name="key">The key that matches the value</param>
109-
/// <param name="cancellationToken">A cancellation token</param>
110-
/// <returns>A string or <c>null</c> if the value was <c>nil</c>.</returns>
111-
Task<string?> GetValueAsync(string key, CancellationToken cancellationToken);
112-
113100
/// <summary>
114101
/// Sets an existing key to match a value or adds a new one if necessary.
115102
/// </summary>
@@ -163,6 +150,19 @@ public interface ISettingsManager
163150
/// <param name="cancellationToken">A cancellation token</param>
164151
Task SetValueAsync(KeyValuePair<string, bool> pair, CancellationToken cancellationToken);
165152

153+
/// <summary>
154+
/// Update's the plugin's section.
155+
/// </summary>
156+
/// <param name="section">The new version of the section</param>
157+
void UpdateSectionAsync(Dictionary<string, string> section);
158+
159+
/// <summary>
160+
/// Update's the plugin's section.
161+
/// </summary>
162+
/// <param name="section">The new version of the section</param>
163+
/// <param name="cancellationToken">A cancellation token</param>
164+
void UpdateSectionAsync(Dictionary<string, string> section, CancellationToken cancellationToken);
165+
166166
}
167167

168168
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
namespace WriterSharp.PluginApi.Sharing
2+
{
3+
4+
/// <summary>
5+
/// Dependency container for WriterSharp plugins.
6+
/// </summary>
7+
public interface IDependencyContainer
8+
{
9+
10+
/// <summary>
11+
/// Gets a service, if such is both registered and available.
12+
/// </summary>
13+
/// <typeparam name="TService"></typeparam>
14+
/// <returns></returns>
15+
TService Get<TService>();
16+
17+
/// <summary>
18+
/// Injects a service.
19+
/// </summary>
20+
/// <typeparam name="TService">The type of the service</typeparam>
21+
void Inject<TService>();
22+
23+
/// <summary>
24+
/// Checks if a service is available.
25+
/// </summary>
26+
/// <typeparam name="TService">The type of the service</typeparam>
27+
/// <returns><c>true</c> if available</returns>
28+
bool IsAvailable<TService>();
29+
30+
/// <summary>
31+
/// Checks if a service is registered.
32+
/// </summary>
33+
/// <typeparam name="TService">The type of the service</typeparam>
34+
/// <returns><c>true</c> if registered</returns>
35+
bool IsRegistered<TService>();
36+
37+
}
38+
39+
}

WriterSharp.PluginApi/Resources/IReadOnlyResource.cs renamed to WriterSharp.PluginApi/Sharing/IReadOnlyResource.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
namespace WriterSharp.PluginApi.Resources
1+
namespace WriterSharp.PluginApi.Sharing
22
{
33

44
/// <summary>
55
/// A read-only resource.
66
/// </summary>
7-
public interface IReadOnlyResource<TObject>
7+
public interface IReadOnlyResource<out TObject>
88
{
99

1010
/// <summary>
@@ -14,15 +14,15 @@ public interface IReadOnlyResource<TObject>
1414
TObject? AsReadOnly();
1515

1616
/// <summary>
17-
/// Gets a mutable copy of the object that's entirely detached.
17+
/// Frees the memory used by this object.
1818
/// </summary>
19-
/// <returns>The copy of the object</returns>
20-
TObject? GetDetachedCopy();
19+
void Free();
2120

2221
/// <summary>
23-
/// Frees the memory used by this object.
22+
/// Gets a mutable copy of the object that's entirely detached.
2423
/// </summary>
25-
void Free();
24+
/// <returns>The copy of the object</returns>
25+
TObject? GetDetachedCopy();
2626

2727
}
2828

WriterSharp.PluginApi/Resources/IResourceManager.cs renamed to WriterSharp.PluginApi/Sharing/IResourceManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System.Threading.Tasks;
22

33

4-
namespace WriterSharp.PluginApi.Resources
4+
namespace WriterSharp.PluginApi.Sharing
55
{
66

77
/// <summary>
8-
/// A resource manager for sharability purposes.
8+
/// A resource manager for shareability purposes.
99
/// </summary>
1010
public interface IResourceManager
1111
{
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
namespace WriterSharp.PluginApi.Sharing
2+
{
3+
4+
/// <summary>
5+
/// A WriterSharp service.
6+
/// </summary>
7+
public interface IService
8+
{
9+
10+
/// <summary>
11+
/// Whether the service is available for public usage.
12+
/// </summary>
13+
bool IsAvailable { get; }
14+
15+
}
16+
17+
}

0 commit comments

Comments
 (0)