Example #1
0
 public object?CreateInstance(IServiceProvider serviceProvider, Type instanceType, object[]?parameters)
 {
     if (parameters != null && parameters.Length > 0)
     {
         throw new NotSupportedException("SimpleCoconaInstanceActivator doesn't support extra arguments.");
     }
     return(SimpleActivator.CreateInstance(serviceProvider, instanceType));
 }
 public static void AddSingleton <TService, TImplementation>(this ICoconaLiteServiceCollection services)
     where TImplementation : TService
 {
     services.AddDescriptor <TService>((provider, disposables) =>
     {
         var instance = (TService)SimpleActivator.CreateInstance(provider, typeof(TImplementation))
                        ?? throw new InvalidOperationException($"The service factory of '{typeof(TService)}' must be non-null value.");
         if (instance is IDisposable disposable)
         {
             disposables.Add(disposable);
         }
         return(instance);
     }, singleton: true);
 }
        public static void AddTransient <TService, TImplementation>(this ICoconaLiteServiceCollection services)
            where TImplementation : TService
        {
            services.AddDescriptor <TService>((provider, disposables) =>
            {
                var instance = SimpleActivator.CreateInstance(provider, typeof(TImplementation));
                if (instance is IDisposable disposable)
                {
                    disposables.Add(disposable);
                }

                return(instance);
            }, singleton: false);
        }
Example #4
0
 public void AddSingleton <TService, TImplementation>()
     where TImplementation : TService
 {
     _factories[typeof(TService)] = provider =>
     {
         var instance = (TService)SimpleActivator.CreateInstance(this, typeof(TImplementation));
         if (instance is IDisposable disposable)
         {
             _disposables.Add(disposable);
         }
         _factories[typeof(TService)] = _ => instance !;
         return(instance !);
     };
 }