diff --git a/DigitalData.Core.Abstractions/Factory.cs b/DigitalData.Core.Abstractions/Factory.cs new file mode 100644 index 0000000..0848b0d --- /dev/null +++ b/DigitalData.Core.Abstractions/Factory.cs @@ -0,0 +1,97 @@ +using Microsoft.Extensions.DependencyInjection; +using System.Collections; +#if NETFRAMEWORK +using System; +using System.Collections.Generic; +#endif + +namespace DigitalData.Core.Abstractions +{ + public class Factory : IServiceProvider, IServiceCollection + { + public static readonly Factory Instance = new Factory(); + + private readonly IServiceCollection _serviceCollection = new ServiceCollection(); + + private readonly Lazy _lazyServiceProvider; + + public Factory() + { + _serviceCollection = new ServiceCollection(); + _lazyServiceProvider = new Lazy(() => _serviceCollection.BuildServiceProvider()); + } + + #region Service Provider + public object +#if NET + ? +#endif + GetService(Type serviceType) + { + return _lazyServiceProvider.Value; + } + #endregion + + #region Service Collection + public int Count => _serviceCollection.Count; + + public bool IsReadOnly => _serviceCollection.IsReadOnly; + + public ServiceDescriptor this[int index] + { + get => _serviceCollection[index]; + set => _serviceCollection[index] = value; + } + + public int IndexOf(ServiceDescriptor item) + { + return _serviceCollection.IndexOf(item); + } + + public void Insert(int index, ServiceDescriptor item) + { + _serviceCollection.Insert(index, item); + } + + public void RemoveAt(int index) + { + _serviceCollection.RemoveAt(index); + } + + public void Add(ServiceDescriptor item) + { + _serviceCollection.Add(item); + } + + public void Clear() + { + _serviceCollection.Clear(); + } + + public bool Contains(ServiceDescriptor item) + { + return _serviceCollection.Contains(item); + } + + public void CopyTo(ServiceDescriptor[] array, int arrayIndex) + { + _serviceCollection.CopyTo(array, arrayIndex); + } + + public bool Remove(ServiceDescriptor item) + { + return _serviceCollection.Remove(item); + } + + public IEnumerator GetEnumerator() + { + return _serviceCollection.GetEnumerator(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return (_serviceCollection as IEnumerable).GetEnumerator(); + } + #endregion + } +} \ No newline at end of file