Last active
October 26, 2024 10:25
-
-
Save dmitryzenevich/1a0e97566dd764d6267d89981372f97a to your computer and use it in GitHub Desktop.
VContainer Extensions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using VContainer; | |
| using VContainer.Unity; | |
| namespace Extensions | |
| { | |
| public static class VContainerExtensions | |
| { | |
| public static IObjectResolver CreateContainer(Action<IContainerBuilder> build) | |
| { | |
| var builder = new ContainerBuilder(); | |
| build?.Invoke(builder); | |
| return builder.Build(); | |
| } | |
| public static IObjectResolver CreateContainer(this IInstaller installer) | |
| { | |
| if (installer is null) | |
| throw new ArgumentNullException(nameof(installer)); | |
| var builder = new ContainerBuilder(); | |
| installer.Install(builder); | |
| return builder.Build(); | |
| } | |
| public static IObjectResolver CreateContainer(this IEnumerable<IInstaller> installers) | |
| { | |
| if (installers is null) | |
| throw new ArgumentNullException(nameof(installers)); | |
| var builder = new ContainerBuilder(); | |
| foreach (var installer in installers) | |
| { | |
| installer.Install(builder); | |
| } | |
| return builder.Build(); | |
| } | |
| public static T Instantiate<T>(this IObjectResolver resolver) where T : class | |
| { | |
| var instanceType = typeof(T); | |
| var constructorInfo = instanceType.GetConstructors()[0]; | |
| var constructorParamsInfo = constructorInfo.GetParameters(); | |
| var constructorParams = new object[constructorParamsInfo.Length]; | |
| for (var i = 0; i < constructorParamsInfo.Length; i++) | |
| { | |
| var parameterInfo = constructorParamsInfo[i]; | |
| var type = parameterInfo.ParameterType; | |
| constructorParams[i] = resolver.Resolve(type); | |
| } | |
| var instance = Activator.CreateInstance(instanceType, constructorParams); | |
| return (T)instance; | |
| } | |
| public static T Instantiate<T>(this IObjectResolver resolver, params object[] args) where T : class | |
| { | |
| var instanceType = typeof(T); | |
| var constructorInfo = instanceType.GetConstructors()[0]; | |
| var constructorParamsInfo = constructorInfo.GetParameters(); | |
| var constructorParams = new object[constructorParamsInfo.Length]; | |
| for (var i = 0; i < constructorParamsInfo.Length; i++) | |
| { | |
| var parameterInfo = constructorParamsInfo[i]; | |
| var type = parameterInfo.ParameterType; | |
| var parameter = args.FirstOrDefault(arg => arg.GetType() == type) ?? resolver.Resolve(type); | |
| constructorParams[i] = parameter; | |
| } | |
| var instance = Activator.CreateInstance(instanceType, constructorParams); | |
| return (T)instance; | |
| } | |
| // public static RegistrationBuilder RegisterFactory<T>(this IContainerBuilder builder, Lifetime lifetime = Lifetime.Singleton) where T : class | |
| // { | |
| // return builder.RegisterFactory<T>(resolver => resolver.Instantiate<T>, lifetime); | |
| // } | |
| public static RegistrationBuilder Register<T>(this IContainerBuilder builder, Lifetime lifetime = Lifetime.Singleton, params object[] args) where T : class | |
| { | |
| return builder.Register(resolver => resolver.Instantiate<T>(args), lifetime); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment