From e1a142e607284417481680d4fe3bb8cd9843339f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <170472707+github-actions[bot]@users.noreply.github.com> Date: Tue, 26 Nov 2024 21:14:50 +0000 Subject: [PATCH] [skip CI] CI/CD: format code --- .../Attributes/InjectAllAttribute.cs | 18 +- .../Attributes/InjectAttribute.cs | 18 +- .../Attributes/InjectableAttribute.cs | 8 +- .../DependencyContainer.cs | 618 +++++++++--------- .../Registrations/DependencyRegistration.cs | 2 +- .../Registrations/SingletonRegistration.cs | 4 +- .../Registrations/TransientRegistration.cs | 6 +- 7 files changed, 337 insertions(+), 337 deletions(-) diff --git a/Fuyu.DependencyInjection/Attributes/InjectAllAttribute.cs b/Fuyu.DependencyInjection/Attributes/InjectAllAttribute.cs index b0f196bc..41b2fafa 100644 --- a/Fuyu.DependencyInjection/Attributes/InjectAllAttribute.cs +++ b/Fuyu.DependencyInjection/Attributes/InjectAllAttribute.cs @@ -2,13 +2,13 @@ namespace Fuyu.DependencyInjection.Attributes { - [AttributeUsage(AttributeTargets.Parameter)] - public class InjectAllAttribute : InjectAttribute - { - // InjectAttribute's id can be null here as it doesn't get used - // in Resolve if the InjectAttribute type is InjectAllAttribute - public InjectAllAttribute() : base(null) - { - } - } + [AttributeUsage(AttributeTargets.Parameter)] + public class InjectAllAttribute : InjectAttribute + { + // InjectAttribute's id can be null here as it doesn't get used + // in Resolve if the InjectAttribute type is InjectAllAttribute + public InjectAllAttribute() : base(null) + { + } + } } diff --git a/Fuyu.DependencyInjection/Attributes/InjectAttribute.cs b/Fuyu.DependencyInjection/Attributes/InjectAttribute.cs index 2c6c8afc..49d361df 100644 --- a/Fuyu.DependencyInjection/Attributes/InjectAttribute.cs +++ b/Fuyu.DependencyInjection/Attributes/InjectAttribute.cs @@ -2,14 +2,14 @@ namespace Fuyu.DependencyInjection.Attributes { - [AttributeUsage(AttributeTargets.Parameter)] - public class InjectAttribute : Attribute - { - public InjectAttribute(string id) - { - Id = id; - } + [AttributeUsage(AttributeTargets.Parameter)] + public class InjectAttribute : Attribute + { + public InjectAttribute(string id) + { + Id = id; + } - public string Id { get; } - } + public string Id { get; } + } } diff --git a/Fuyu.DependencyInjection/Attributes/InjectableAttribute.cs b/Fuyu.DependencyInjection/Attributes/InjectableAttribute.cs index f60e0964..33f11dfe 100644 --- a/Fuyu.DependencyInjection/Attributes/InjectableAttribute.cs +++ b/Fuyu.DependencyInjection/Attributes/InjectableAttribute.cs @@ -2,8 +2,8 @@ namespace Fuyu.DependencyInjection.Attributes { - [AttributeUsage(AttributeTargets.Constructor)] - public class InjectableAttribute : Attribute - { - } + [AttributeUsage(AttributeTargets.Constructor)] + public class InjectableAttribute : Attribute + { + } } diff --git a/Fuyu.DependencyInjection/DependencyContainer.cs b/Fuyu.DependencyInjection/DependencyContainer.cs index 635a34ed..63cc47a0 100644 --- a/Fuyu.DependencyInjection/DependencyContainer.cs +++ b/Fuyu.DependencyInjection/DependencyContainer.cs @@ -8,313 +8,313 @@ namespace Fuyu.DependencyInjection { public class DependencyContainer - { - private Dictionary> _registrations; - - public DependencyContainer() - { - _registrations = new Dictionary>(); - } - - #region Registrations - - private void AddRegistration(Type type, DependencyRegistration registration) - { - if (!_registrations.TryGetValue(type, out var registrations)) - { - _registrations[type] = registrations = new List(); - } - else - { - var index = registrations.FindIndex(d => d.Id == registration.Id); - if (index != -1) - { - Console.WriteLine($"Prevented multi registering of {registration.Id}"); - registrations[index] = registration; - return; - } - } - - registrations.Add(registration); - } - - private List GetRegistrations(Type type) - { - if (_registrations.TryGetValue(type, out var registrations)) - { - return registrations; - } - - return new List(); - } - - #endregion - - #region Resolve - - /// Can be null -- if null will not search for existing registrations but will create a new instance - public object Resolve(string id, Type type) - { - // If we have an id we are willing to take an existing registration - if (id != null) - { - // Get registrations by type - var registrations = GetRegistrations(type); - foreach (var registration in registrations) - { - // Find the one with the id we are looking for - if (registration.Id == id) - { - return registration.GetValue(); - } - } - } - - // If id was not found (or we didn't specify it) try to create one - // We cannot create an instance of an interface or abstract type - if (type.IsInterface || type.IsAbstract) - { - throw new InvalidOperationException($"Cannot create instance of unimplemented type {type.Name}"); - } - - // Find constructor that is injectable - var constructor = type - .GetConstructors() - .FirstOrDefault(c => c.GetCustomAttribute() != null); - - if (constructor == null) - { - throw new InvalidOperationException($"Cannot find injectable constructor on {type.Name}, did you forget the attribute?"); - } - - var parameters = constructor.GetParameters(); - - if (!parameters.All(p => p.GetCustomAttribute() != null)) - { - throw new ArgumentException($"One or more parameters on {type.Name} are not injectable"); - } - - var parameterArguments = new object[parameters.Length]; - - for (var i = 0; i < parameters.Length; i++) - { - var parameter = parameters[i]; - // Assume not null because above check passed - var injectAttribute = parameter.GetCustomAttribute(); - var parameterType = parameter.ParameterType; - - if (injectAttribute is InjectAllAttribute) - { - var parameterTypeGenericArguments = parameterType.GetGenericArguments(); - - if (parameterTypeGenericArguments.Length != 1) - { - throw new Exception($"Using InjectAll but {parameter.Name} does not have 1 generic argument"); - } - - var injectAllType = parameterTypeGenericArguments[0]; - // Using List instead of HashSet to avoid needlessly hashing objects we know SHOULD be unique - var genericListType = typeof(List<>).MakeGenericType(injectAllType); - - if (!parameterType.IsAssignableFrom(genericListType)) - { - throw new Exception($"Using InjectAll but {parameter.Name} uses type {parameterType.Name} when it should be List"); - } - - // Array.Empty to target ResolveAll() instead of ResolveAll(Type) - parameterArguments[i] = - typeof(DependencyContainer) - .GetMethod(nameof(ResolveAll), Array.Empty()) - .MakeGenericMethod(injectAllType) - .Invoke(this, Array.Empty()); - } - else - { - if (injectAttribute.Id == null) - { - if (parameter.ParameterType != typeof(DependencyContainer)) - { - throw new Exception($"Cannot inject '{parameter.Name}' on constructor for {type.Name} because dependency id is null and it is not requesting the container"); - } - - parameterArguments[i] = this; - } - else - { - parameterArguments[i] = Resolve(injectAttribute.Id, parameterType); - } - } - } - - return constructor.Invoke(parameterArguments); - } - - /// - /// Only used to create an instance of an injectable - /// - public object Resolve(Type type) - { - return Resolve(null, type); - } - - /// - /// Only used to create an instance of an injectable - /// - public T Resolve() where T : class - { - return (T)Resolve(typeof(T)); - } - - /// - /// Used to resolve an object of the desired type and id - /// - public T Resolve(string id) where T : class - { - return (T)Resolve(id, typeof(T)); - } - - #endregion - - #region ResolveAll - - public List ResolveAll(Type type) - { - var registrations = GetRegistrations(type); - var instances = new List(registrations.Count); - - for (var i = 0; i < registrations.Count; i++) - { - instances.Add(registrations[i].GetValue()); - } - - return instances; - } - - public List ResolveAll() where T: class - { - var registrations = GetRegistrations(typeof(T)); - var instances = new List(registrations.Count); - - for (var i = 0; i < registrations.Count; i++) - { - instances.Add((T)registrations[i].GetValue()); - } - - return instances; - } - - #endregion - - #region Register - - #region Transient - - public void RegisterTransient(string id, Func factory) where T : class - { - AddRegistration(typeof(TBase), new TransientRegistration(id, factory)); - } - - public void RegisterTransient(string id) where T: class, new() - { - AddRegistration(typeof(TBase), new TransientRegistration(id, () => new T())); - } - - public void RegisterTransientResolved(string id) where T: class - { - AddRegistration(typeof(TBase), new TransientRegistration(id, Resolve)); - } - - public void RegisterTransient(string id, Func factory) where T : class - { - AddRegistration(typeof(T), new TransientRegistration(id, factory)); - } - - public void RegisterTransient(string id) where T : class, new() - { - AddRegistration(typeof(T), new TransientRegistration(id, () => new T())); - } - - public void RegisterTransientResolved(string id) where T : class - { - AddRegistration(typeof(T), new TransientRegistration(id, Resolve)); - } - - public void RegisterTransient(Func factory) where T : class - { - AddRegistration(typeof(TBase), new TransientRegistration(typeof(T).Name, factory)); - } - - public void RegisterTransient() where T: class, new() - { - AddRegistration(typeof(TBase), new TransientRegistration(typeof(T).Name, () => new T())); - } - - public void RegisterTransientResolved() where T: class - { - AddRegistration(typeof(TBase), new TransientRegistration(typeof(T).Name, Resolve)); - } - - public void RegisterTransient(Func factory) where T : class - { - AddRegistration(typeof(T), new TransientRegistration(typeof(T).Name, factory)); - } - - public void RegisterTransient() where T : class, new() - { - AddRegistration(typeof(T), new TransientRegistration(typeof(T).Name, () => new T())); - } - - public void RegisterTransientResolved() where T : class - { - AddRegistration(typeof(T), new TransientRegistration(typeof(T).Name, Resolve)); - } - - #endregion - - #region Singleton - - public void RegisterSingleton(string id, T instance) where T : class - { - AddRegistration(typeof(TBase), new SingletonRegistration(id, instance)); - } - - public void RegisterSingleton(string id) where T : class, new() - { - AddRegistration(typeof(TBase), new SingletonRegistration(id, new T())); - } - - public void RegisterSingleton(T instance) where T : class - { - AddRegistration(typeof(TBase), new SingletonRegistration(typeof(T).Name, instance)); - } - - public void RegisterSingleton() where T: class, new() - { - AddRegistration(typeof(TBase), new SingletonRegistration(typeof(T).Name, new T())); - } - - public void RegisterSingleton(string id, T instance) where T : class - { - AddRegistration(typeof(T), new SingletonRegistration(id, instance)); - } - - public void RegisterSingleton(string id) where T : class, new() - { - AddRegistration(typeof(T), new SingletonRegistration(id, new T())); - } - - public void RegisterSingleton(T instance) where T : class - { - AddRegistration(typeof(T), new SingletonRegistration(typeof(T).Name, instance)); - } - - public void RegisterSingleton() where T : class, new() - { - AddRegistration(typeof(T), new SingletonRegistration(typeof(T).Name, new T())); - } - - #endregion - - #endregion - } + { + private Dictionary> _registrations; + + public DependencyContainer() + { + _registrations = new Dictionary>(); + } + + #region Registrations + + private void AddRegistration(Type type, DependencyRegistration registration) + { + if (!_registrations.TryGetValue(type, out var registrations)) + { + _registrations[type] = registrations = new List(); + } + else + { + var index = registrations.FindIndex(d => d.Id == registration.Id); + if (index != -1) + { + Console.WriteLine($"Prevented multi registering of {registration.Id}"); + registrations[index] = registration; + return; + } + } + + registrations.Add(registration); + } + + private List GetRegistrations(Type type) + { + if (_registrations.TryGetValue(type, out var registrations)) + { + return registrations; + } + + return new List(); + } + + #endregion + + #region Resolve + + /// Can be null -- if null will not search for existing registrations but will create a new instance + public object Resolve(string id, Type type) + { + // If we have an id we are willing to take an existing registration + if (id != null) + { + // Get registrations by type + var registrations = GetRegistrations(type); + foreach (var registration in registrations) + { + // Find the one with the id we are looking for + if (registration.Id == id) + { + return registration.GetValue(); + } + } + } + + // If id was not found (or we didn't specify it) try to create one + // We cannot create an instance of an interface or abstract type + if (type.IsInterface || type.IsAbstract) + { + throw new InvalidOperationException($"Cannot create instance of unimplemented type {type.Name}"); + } + + // Find constructor that is injectable + var constructor = type + .GetConstructors() + .FirstOrDefault(c => c.GetCustomAttribute() != null); + + if (constructor == null) + { + throw new InvalidOperationException($"Cannot find injectable constructor on {type.Name}, did you forget the attribute?"); + } + + var parameters = constructor.GetParameters(); + + if (!parameters.All(p => p.GetCustomAttribute() != null)) + { + throw new ArgumentException($"One or more parameters on {type.Name} are not injectable"); + } + + var parameterArguments = new object[parameters.Length]; + + for (var i = 0; i < parameters.Length; i++) + { + var parameter = parameters[i]; + // Assume not null because above check passed + var injectAttribute = parameter.GetCustomAttribute(); + var parameterType = parameter.ParameterType; + + if (injectAttribute is InjectAllAttribute) + { + var parameterTypeGenericArguments = parameterType.GetGenericArguments(); + + if (parameterTypeGenericArguments.Length != 1) + { + throw new Exception($"Using InjectAll but {parameter.Name} does not have 1 generic argument"); + } + + var injectAllType = parameterTypeGenericArguments[0]; + // Using List instead of HashSet to avoid needlessly hashing objects we know SHOULD be unique + var genericListType = typeof(List<>).MakeGenericType(injectAllType); + + if (!parameterType.IsAssignableFrom(genericListType)) + { + throw new Exception($"Using InjectAll but {parameter.Name} uses type {parameterType.Name} when it should be List"); + } + + // Array.Empty to target ResolveAll() instead of ResolveAll(Type) + parameterArguments[i] = + typeof(DependencyContainer) + .GetMethod(nameof(ResolveAll), Array.Empty()) + .MakeGenericMethod(injectAllType) + .Invoke(this, Array.Empty()); + } + else + { + if (injectAttribute.Id == null) + { + if (parameter.ParameterType != typeof(DependencyContainer)) + { + throw new Exception($"Cannot inject '{parameter.Name}' on constructor for {type.Name} because dependency id is null and it is not requesting the container"); + } + + parameterArguments[i] = this; + } + else + { + parameterArguments[i] = Resolve(injectAttribute.Id, parameterType); + } + } + } + + return constructor.Invoke(parameterArguments); + } + + /// + /// Only used to create an instance of an injectable + /// + public object Resolve(Type type) + { + return Resolve(null, type); + } + + /// + /// Only used to create an instance of an injectable + /// + public T Resolve() where T : class + { + return (T)Resolve(typeof(T)); + } + + /// + /// Used to resolve an object of the desired type and id + /// + public T Resolve(string id) where T : class + { + return (T)Resolve(id, typeof(T)); + } + + #endregion + + #region ResolveAll + + public List ResolveAll(Type type) + { + var registrations = GetRegistrations(type); + var instances = new List(registrations.Count); + + for (var i = 0; i < registrations.Count; i++) + { + instances.Add(registrations[i].GetValue()); + } + + return instances; + } + + public List ResolveAll() where T : class + { + var registrations = GetRegistrations(typeof(T)); + var instances = new List(registrations.Count); + + for (var i = 0; i < registrations.Count; i++) + { + instances.Add((T)registrations[i].GetValue()); + } + + return instances; + } + + #endregion + + #region Register + + #region Transient + + public void RegisterTransient(string id, Func factory) where T : class + { + AddRegistration(typeof(TBase), new TransientRegistration(id, factory)); + } + + public void RegisterTransient(string id) where T : class, new() + { + AddRegistration(typeof(TBase), new TransientRegistration(id, () => new T())); + } + + public void RegisterTransientResolved(string id) where T : class + { + AddRegistration(typeof(TBase), new TransientRegistration(id, Resolve)); + } + + public void RegisterTransient(string id, Func factory) where T : class + { + AddRegistration(typeof(T), new TransientRegistration(id, factory)); + } + + public void RegisterTransient(string id) where T : class, new() + { + AddRegistration(typeof(T), new TransientRegistration(id, () => new T())); + } + + public void RegisterTransientResolved(string id) where T : class + { + AddRegistration(typeof(T), new TransientRegistration(id, Resolve)); + } + + public void RegisterTransient(Func factory) where T : class + { + AddRegistration(typeof(TBase), new TransientRegistration(typeof(T).Name, factory)); + } + + public void RegisterTransient() where T : class, new() + { + AddRegistration(typeof(TBase), new TransientRegistration(typeof(T).Name, () => new T())); + } + + public void RegisterTransientResolved() where T : class + { + AddRegistration(typeof(TBase), new TransientRegistration(typeof(T).Name, Resolve)); + } + + public void RegisterTransient(Func factory) where T : class + { + AddRegistration(typeof(T), new TransientRegistration(typeof(T).Name, factory)); + } + + public void RegisterTransient() where T : class, new() + { + AddRegistration(typeof(T), new TransientRegistration(typeof(T).Name, () => new T())); + } + + public void RegisterTransientResolved() where T : class + { + AddRegistration(typeof(T), new TransientRegistration(typeof(T).Name, Resolve)); + } + + #endregion + + #region Singleton + + public void RegisterSingleton(string id, T instance) where T : class + { + AddRegistration(typeof(TBase), new SingletonRegistration(id, instance)); + } + + public void RegisterSingleton(string id) where T : class, new() + { + AddRegistration(typeof(TBase), new SingletonRegistration(id, new T())); + } + + public void RegisterSingleton(T instance) where T : class + { + AddRegistration(typeof(TBase), new SingletonRegistration(typeof(T).Name, instance)); + } + + public void RegisterSingleton() where T : class, new() + { + AddRegistration(typeof(TBase), new SingletonRegistration(typeof(T).Name, new T())); + } + + public void RegisterSingleton(string id, T instance) where T : class + { + AddRegistration(typeof(T), new SingletonRegistration(id, instance)); + } + + public void RegisterSingleton(string id) where T : class, new() + { + AddRegistration(typeof(T), new SingletonRegistration(id, new T())); + } + + public void RegisterSingleton(T instance) where T : class + { + AddRegistration(typeof(T), new SingletonRegistration(typeof(T).Name, instance)); + } + + public void RegisterSingleton() where T : class, new() + { + AddRegistration(typeof(T), new SingletonRegistration(typeof(T).Name, new T())); + } + + #endregion + + #endregion + } } diff --git a/Fuyu.DependencyInjection/Registrations/DependencyRegistration.cs b/Fuyu.DependencyInjection/Registrations/DependencyRegistration.cs index 49eb3dda..141ce66c 100644 --- a/Fuyu.DependencyInjection/Registrations/DependencyRegistration.cs +++ b/Fuyu.DependencyInjection/Registrations/DependencyRegistration.cs @@ -1,6 +1,6 @@ namespace Fuyu.DependencyInjection.Registrations { - internal abstract class DependencyRegistration + internal abstract class DependencyRegistration { public string Id { get; set; } diff --git a/Fuyu.DependencyInjection/Registrations/SingletonRegistration.cs b/Fuyu.DependencyInjection/Registrations/SingletonRegistration.cs index 32c83107..b5ef6a2a 100644 --- a/Fuyu.DependencyInjection/Registrations/SingletonRegistration.cs +++ b/Fuyu.DependencyInjection/Registrations/SingletonRegistration.cs @@ -1,6 +1,6 @@ namespace Fuyu.DependencyInjection.Registrations { - internal class SingletonRegistration : DependencyRegistration + internal class SingletonRegistration : DependencyRegistration { public T Instance { get; } @@ -10,7 +10,7 @@ internal SingletonRegistration(string id, T instance) Instance = instance; } - public override object GetValue() + public override object GetValue() { return Instance; } diff --git a/Fuyu.DependencyInjection/Registrations/TransientRegistration.cs b/Fuyu.DependencyInjection/Registrations/TransientRegistration.cs index ed875b51..e53aa88b 100644 --- a/Fuyu.DependencyInjection/Registrations/TransientRegistration.cs +++ b/Fuyu.DependencyInjection/Registrations/TransientRegistration.cs @@ -2,17 +2,17 @@ namespace Fuyu.DependencyInjection.Registrations { - internal class TransientRegistration : DependencyRegistration + internal class TransientRegistration : DependencyRegistration { public Func Factory { get; } - internal TransientRegistration(string id, Func factory) + internal TransientRegistration(string id, Func factory) { Id = id; Factory = factory; } - public override object GetValue() + public override object GetValue() { return Factory(); }