Exemple #1
0
        public void Attribute_Constructor_Correctly()
        {
            InjectableAttribute injectable = new InjectableAttribute(typeof(Class1), Enums.DependencyInjectionTypes.Scoped);

            Assert.NotNull(injectable);
            Assert.NotNull(injectable.InstaceOf);
        }
Exemple #2
0
        public void TestInjectableAttributeSingletonLifestyle()
        {
            //Given that the following instance of the InjectableAttribute class is initialized
            var injectableAttribute = new InjectableAttribute(InjectableLifestyle.Singleton);

            //When we get the value returned by the Lifestyle propery
            var lifestyle = injectableAttribute.Lifestyle;

            //Then the value returned should match the Singleton lifestyle
            lifestyle.Should().Be(InjectableLifestyle.Singleton);
        }
        public override void Register(IEnumerable <Type> types)
        {
            foreach (Type t in types)
            {
                if (Attribute.IsDefined(t, typeof(InjectableAttribute)))
                {
                    InjectableAttribute typeInjectableAttribute = Attribute.GetCustomAttributes(t, typeof(InjectableAttribute))
                                                                  .FirstOrDefault() as InjectableAttribute;

                    this.Container.Register(t.GetDependencyId(), t, typeInjectableAttribute.InstanceType);
                }
            }

            this.RegisterNext(types);
        }
    /// <summary>
    /// Add services tagged with a <see cref="InjectableAttribute"/> to the <see cref="IServiceCollection"/>.
    /// </summary>
    /// <param name="services">Service collection.</param>
    /// <param name="assemblies">Assemblies to look for injectable services.</param>
    public static void AddInjectableServices(this IServiceCollection services, params Assembly[] assemblies)
    {
        var injectableServicesTypes = ReflectionHelper.GetClassesWithCustomAttribute<InjectableAttribute>(assemblies);

        foreach (var serviceType in injectableServicesTypes)
        {
            InjectableAttribute serviceAttribute = GetTypeAttribute<InjectableAttribute>(serviceType);
            Type[] serviceInterfacesTypes = serviceType.GetInterfaces();
            ServiceLifetime serviceLifeTime = serviceAttribute != null ? serviceAttribute.LifeTime : ServiceLifetime.Transient;

            if (serviceInterfacesTypes.Any())
            {
                foreach (var serviceInterface in serviceInterfacesTypes)
                {
                    services.Add(new ServiceDescriptor(serviceInterface, serviceType, serviceLifeTime));
                }
            }
            else
            {
                services.Add(new ServiceDescriptor(serviceType, serviceLifeTime));
            }
        }
    }
Exemple #5
0
    public void Inject(object target)
    {
        FieldInfo[] fields = target.GetType().GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Static);
        foreach (FieldInfo field in fields)
        {
            foreach (object attribute in field.GetCustomAttributes(true))
            {
                InjectableAttribute injectableAttribute = attribute as InjectableAttribute;
                if (injectableAttribute == null)
                {
                    continue;
                }

                Type   targetType  = field.FieldType;
                object targetValue = GetManager(targetType);
                if (targetValue == null)
                {
                    continue;
                }

                field.SetValue(target, targetValue);
            }
        }
    }
Exemple #6
0
 public void ConfigureServices(IServiceCollection services)
 {
     InjectableAttribute.RegisterInjectables(services);
 }
Exemple #7
0
 public void ConfigureServices(IServiceCollection services)
 {
     InjectableAttribute.RegisterInjectables(services);
     InjectableAttribute.RegisterInjectables(services);
     services.AddSingleton <AppState>();
 }
 public CompletedBuilder(Object instance, String name = null) : base(instance.GetType(), new HashSet <ParameterInfo>())
 {
     Instance   = instance;
     Injectable = new InjectableAttribute(name);
 }