예제 #1
0
        private static void RegisterTypes(IEnumerable <Type> transientTypes, IocLifeTime lifeTime)
        {
            foreach (var transientType in transientTypes)
            {
                //var implementedInterfaces = transientType.GetTypeInfo().ImplementedInterfaces;

                var implementedInterfaces = transientType.GetTypeInfo().ImplementedInterfaces
                                            .Where(x => typeof(IDependency).IsAssignableFrom(x) && x != typeof(ITransientDependency) &&
                                                   x != typeof(ISingletonDependency) && x != typeof(IScopedDependency) && x != typeof(IDependency));

                foreach (var @interface in implementedInterfaces)
                {
                    switch (lifeTime)
                    {
                    case IocLifeTime.Transient:
                        IocManager.Instance.RegisterTransient(@interface, transientType);
                        break;

                    case IocLifeTime.Scoped:
                        IocManager.Instance.RegisterScoped(@interface, transientType);
                        break;

                    case IocLifeTime.Singleton:
                        IocManager.Instance.RegisterSingleton(@interface, transientType);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(lifeTime), lifeTime, null);
                    }
                }
            }
        }
예제 #2
0
        private static void RegisterTypes <TDependency, TTransient, TSingleton, TScoped>(IEnumerable <Type> transientTypes, IocLifeTime lifeTime, IReadOnlyList <Action> action)
        {
            foreach (var transientType in transientTypes)
            {
                //var implementedInterfaces = transientType.GetTypeInfo().ImplementedInterfaces;

                var implementedInterfaces = transientType.GetTypeInfo().ImplementedInterfaces
                                            .Where(x => typeof(TDependency).IsAssignableFrom(x) && x != typeof(TTransient) &&
                                                   x != typeof(TSingleton) && x != typeof(TScoped) && x != typeof(TDependency));

                foreach (var @interface in implementedInterfaces)
                {
                    TypeInterface      = @interface;
                    TypeImplementation = transientType;

                    switch (lifeTime)
                    {
                    case IocLifeTime.Transient:
                        action[0].Invoke();
                        break;

                    case IocLifeTime.Scoped:
                        action[1].Invoke();
                        break;

                    case IocLifeTime.Singleton:
                        action[2].Invoke();
                        break;

                    default:
                        throw new ArgumentOutOfRangeException(nameof(lifeTime), lifeTime, null);
                    }
                }
            }
        }