From 6352cbebeff0f686098176eb477f8509da3cf246 Mon Sep 17 00:00:00 2001 From: Christopher F Date: Sat, 11 Feb 2017 13:53:14 -0500 Subject: [PATCH] Add TryAdd to DependencyMaps --- .../Dependencies/DependencyMap.cs | 18 +++++++++++++ .../Dependencies/IDependencyMap.cs | 27 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs index 0761f5fff..f5adf1a8c 100644 --- a/src/Discord.Net.Commands/Dependencies/DependencyMap.cs +++ b/src/Discord.Net.Commands/Dependencies/DependencyMap.cs @@ -18,12 +18,21 @@ namespace Discord.Commands public void Add(T obj) where T : class => AddFactory(() => obj); /// + public bool TryAdd(T obj) where T : class + => TryAddFactory(() => obj); + /// public void AddTransient() where T : class, new() => AddFactory(() => new T()); /// + public bool TryAddTransient() where T : class, new() + => TryAddFactory(() => new T()); + /// public void AddTransient() where TKey : class where TImpl : class, TKey, new() => AddFactory(() => new TImpl()); + public bool TryAddTransient() where TKey : class + where TImpl : class, TKey, new() + => TryAddFactory(() => new TImpl()); /// public void AddFactory(Func factory) where T : class @@ -33,6 +42,15 @@ namespace Discord.Commands throw new InvalidOperationException($"The dependency map already contains \"{t.FullName}\""); map.Add(t, factory); } + /// + public bool TryAddFactory(Func factory) where T : class + { + var t = typeof(T); + if (map.ContainsKey(t)) + return false; + map.Add(t, factory); + return true; + } /// public T Get() diff --git a/src/Discord.Net.Commands/Dependencies/IDependencyMap.cs b/src/Discord.Net.Commands/Dependencies/IDependencyMap.cs index fb042c304..a55a9e4c5 100644 --- a/src/Discord.Net.Commands/Dependencies/IDependencyMap.cs +++ b/src/Discord.Net.Commands/Dependencies/IDependencyMap.cs @@ -11,11 +11,24 @@ namespace Discord.Commands /// The instance of a service. void Add(T obj) where T : class; /// + /// Tries to add an instance of a service to be injected. + /// + /// The type of service. + /// The instance of a service. + /// A bool, indicating if the service was successfully added to the DependencyMap. + bool TryAdd(T obj) where T : class; + /// /// Add a service that will be injected by a new instance every time. /// /// The type of instance to inject. void AddTransient() where T : class, new(); /// + /// Tries to add a service that will be injected by a new instance every time. + /// + /// The type of instance to inject. + /// A bool, indicating if the service was successfully added to the DependencyMap. + bool TryAddTransient() where T : class, new(); + /// /// Add a service that will be injected by a new instance every time. /// /// The type to look for when injecting. @@ -25,11 +38,25 @@ namespace Discord.Commands /// void AddTransient() where TKey: class where TImpl : class, TKey, new(); /// + /// Tries to add a service that will be injected by a new instance every time. + /// + /// The type to look for when injecting. + /// The type to inject when injecting. + /// A bool, indicating if the service was successfully added to the DependencyMap. + bool TryAddTransient() where TKey : class where TImpl : class, TKey, new(); + /// /// Add a service that will be injected by a factory. /// /// The type to look for when injecting. /// The factory that returns a type of this service. void AddFactory(Func factory) where T : class; + /// + /// Tries to add a service that will be injected by a factory. + /// + /// The type to look for when injecting. + /// The factory that returns a type of this service. + /// A bool, indicating if the service was successfully added to the DependencyMap. + bool TryAddFactory(Func factory) where T : class; /// /// Pull an object from the map.