RSCG – depso
RSCG – depso
name | depso |
nuget | https://www.nuget.org/packages/depso/ |
link | https://github.com/notanaverageman/Depso |
author | Yusuf Tarık Günaydın |
generating DI code
This is how you can use depso .
The code that you start with is
<project sdk="Microsoft.NET.Sdk"> <propertygroup> <outputtype>Exe</outputtype> <targetframework>net8.0</targetframework> <implicitusings>enable</implicitusings> <nullable>enable</nullable> </propertygroup> <itemgroup> <packagereference version="1.0.1" include="Depso"> </packagereference> <propertygroup> <emitcompilergeneratedfiles>true</emitcompilergeneratedfiles> <compilergeneratedfilesoutputpath>$(BaseIntermediateOutputPath)\GX</compilergeneratedfilesoutputpath> </propertygroup> </itemgroup>
The code that you will use is
using InjectDemo; MyServiceProvider sc = new(); var con = sc.GetService(typeof(Database)) as IDatabase; ArgumentNullException.ThrowIfNull(con); con.Open(); [Depso.ServiceProvider] public partial class MyServiceProvider { private void RegisterServices() { AddTransient<database database="" ,="">(); AddTransient<idatabase databasecon="" ,="">(); } }
namespace InjectDemo; partial class Database : IDatabase { private readonly IDatabase con; public Database(IDatabase con) { this.con = con; } public void Open() { Console.WriteLine($"open from database"); con.Open(); } }
namespace InjectDemo; public partial class DatabaseCon:IDatabase { public string? Connection { get; set; } public void Open() { Console.WriteLine("open from database con" ); } }
The code that is generated is
// <auto-generated> #nullable enable namespace Depso { [global::System.AttributeUsage(global::System.AttributeTargets.Class)] internal sealed class ServiceProviderAttribute : global::System.Attribute { } }
// <auto-generated> #nullable enable namespace Depso { [global::System.AttributeUsage(global::System.AttributeTargets.Class)] internal sealed class ServiceProviderModuleAttribute : global::System.Attribute { } }
// <auto-generated> #nullable enable public partial class MyServiceProvider : global::System.IDisposable, global::System.IAsyncDisposable, global::System.IServiceProvider { private readonly object _sync = new object(); private global::MyServiceProvider.Scope? _rootScope; private global::MyServiceProvider.Scope RootScope => _rootScope ??= CreateScope(_sync); private bool _isDisposed; public object? GetService(global::System.Type serviceType) { if (serviceType == typeof(global::InjectDemo.Database)) return CreateDatabase_0(); if (serviceType == typeof(global::InjectDemo.IDatabase)) return CreateDatabaseCon_0(); if (serviceType == typeof(global::System.IServiceProvider)) return this; return null; } private T GetService<t>() { return (T)GetService(typeof(T))!; } private global::InjectDemo.Database CreateDatabase_0() { return new global::InjectDemo.Database(GetService<global::injectdemo.idatabase>()); } private global::InjectDemo.DatabaseCon CreateDatabaseCon_0() { return new global::InjectDemo.DatabaseCon(); } private global::MyServiceProvider.Scope CreateScope(object? sync) { ThrowIfDisposed(); return new global::MyServiceProvider.Scope(this, sync); } public void Dispose() { lock (_sync) { if (_isDisposed) { return; } _isDisposed = true; } if (_rootScope != null) _rootScope.Dispose(); } public async global::System.Threading.Tasks.ValueTask DisposeAsync() { lock (_sync) { if (_isDisposed) { return; } _isDisposed = true; } if (_rootScope != null) await _rootScope.DisposeAsync(); } private void ThrowIfDisposed() { if (_isDisposed) { throw new global::System.ObjectDisposedException("MyServiceProvider"); } } }
// <auto-generated> #nullable enable public partial class MyServiceProvider { private class RegistrationModifier { public static readonly global::MyServiceProvider.RegistrationModifier Instance; static RegistrationModifier() { Instance = new global::MyServiceProvider.RegistrationModifier(); } private RegistrationModifier() { } public global::MyServiceProvider.RegistrationModifier AlsoAsSelf() { return this; } public global::MyServiceProvider.RegistrationModifier AlsoAs(global::System.Type type) { return this; } public global::MyServiceProvider.RegistrationModifier AlsoAs<t>() { return this; } } private global::MyServiceProvider.RegistrationModifier ImportModule<t>() { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier ImportModule(global::System.Type moduleType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddSingleton(global::System.Type serviceType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddSingleton(global::System.Type serviceType, global::System.Type implementationType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddSingleton<tservice>() { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddSingleton<tservice timplementation="" ,="">() where TImplementation : TService { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddSingleton<tservice>(global::System.Func<global::system.iserviceprovider tservice="" ,=""> factory) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddScoped(global::System.Type serviceType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddScoped(global::System.Type serviceType, global::System.Type implementationType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddScoped<tservice>() { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddScoped<tservice timplementation="" ,="">() where TImplementation : TService { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddScoped<tservice>(global::System.Func<global::system.iserviceprovider tservice="" ,=""> factory) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddTransient(global::System.Type serviceType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddTransient(global::System.Type serviceType, global::System.Type implementationType) { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddTransient<tservice>() { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddTransient<tservice timplementation="" ,="">() where TImplementation : TService { return global::MyServiceProvider.RegistrationModifier.Instance; } private global::MyServiceProvider.RegistrationModifier AddTransient<tservice>(global::System.Func<global::system.iserviceprovider tservice="" ,=""> factory) { return global::MyServiceProvider.RegistrationModifier.Instance; } }
// <auto-generated> #nullable enable public partial class MyServiceProvider { public class Scope : global::System.IDisposable, global::System.IAsyncDisposable, global::System.IServiceProvider { private readonly object _sync = new object(); private readonly global::MyServiceProvider _root; private bool _isDisposed; public Scope(global::MyServiceProvider root, object? sync) { _root = root; if (sync != null) { _sync = sync; } } public object? GetService(global::System.Type serviceType) { if (serviceType == typeof(global::InjectDemo.Database)) return _root.CreateDatabase_0(); if (serviceType == typeof(global::InjectDemo.IDatabase)) return _root.CreateDatabaseCon_0(); if (serviceType == typeof(global::System.IServiceProvider)) return this; return null; } private T GetService<t>() { return (T)GetService(typeof(T))!; } public void Dispose() { lock (_sync) { if (_isDisposed) { return; } _isDisposed = true; } } public global::System.Threading.Tasks.ValueTask DisposeAsync() { lock (_sync) { if (_isDisposed) { return default; } _isDisposed = true; } return default; } private void ThrowIfDisposed() { if (_isDisposed) { throw new global::System.ObjectDisposedException("MyServiceProvider.Scope"); } } } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/depso
Leave a Reply