示例#1
0
        /// <summary>
        ///     Add Default Mode
        /// </summary>
        /// <param name="configuarations"></param>
        /// <param name="mode">Mode instance</param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddMode(this InyectorConfiguration configuarations,
                                                    Mode mode)
        {
            configuarations.Modes.Add(mode);

            return(configuarations);
        }
示例#2
0
 /// <summary>
 ///     Add Default Mode for a ServiceLifeTime
 /// </summary>
 /// <param name="configuarations"></param>
 /// <param name="services"></param>
 /// <param name="lifetime"></param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration DefaultMode(this InyectorConfiguration configuarations,
                                                 IServiceCollection services,
                                                 ServiceLifetime lifetime)
 {
     configuarations.DefaultMode(AspNetCoreModeFactory.Create(lifetime, services).InyectorMethod);
     return(configuarations);
 }
示例#3
0
        /// <summary>
        ///     Apply a Naming convention rule with an Assembly
        ///     example : ICarRepository match with CarRepository
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="assembly">Assembly to apply rule</param>
        /// <param name="lifetime">AspNet Core mode to inyect </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddRuleForNamingConvention(this InyectorConfiguration configuration,
                                                                       Assembly assembly,
                                                                       ServiceLifetime lifetime)
        {
            configuration.AddRule(assembly, (t1, t2) => $"I{t1.Name}" == t2.Name, lifetime.ToString());

            return(configuration);
        }
示例#4
0
        /// <summary>
        ///     Add Rule from LifeTime using the default models and a target Assembly
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="assembly">Assembly to apply the rule</param>
        /// <param name="criteria">Criteria to find the types</param>
        /// <param name="lifetime">lifetime</param>
        /// <returns></returns>
        public static InyectorConfiguration AddRule(this InyectorConfiguration configuration,
                                                    Assembly assembly,
                                                    Func <Type, Type, bool> criteria,
                                                    ServiceLifetime lifetime)
        {
            configuration.AddRule(assembly, criteria, lifetime.ToString());

            return(configuration);
        }
示例#5
0
        /// <summary>
        ///     Apply a Naming convention rule with an assembly
        ///     with ends with names example only inyect classes that finish with [Repository, Helper, Services, Factory, and so
        ///     on...]
        ///     example : ICarRepository match with CarRepository
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="assembly">Assembly to apply rule</param>
        /// <param name="names">names to find is the type name finish with</param>
        /// <param name="lifetime">AspNet Core mode to inyect </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddRuleForEndsWithNamingConvention(this InyectorConfiguration configuration,
                                                                               Assembly assembly,
                                                                               IEnumerable <string> names,
                                                                               ServiceLifetime lifetime)
        {
            configuration.AddRule(assembly, (t1, t2) => names.Any(n => t1.Name.ToLower().EndsWith(n.ToLower())) &&
                                  $"I{t1.Name}" == t2.Name,
                                  lifetime.ToString());

            return(configuration);
        }
示例#6
0
        /// <summary>
        ///     Add Default Mode
        /// </summary>
        /// <param name="configuarations"></param>
        /// <param name="inyectorMethod">
        ///     is the action to apply the inyector method, the first param is the implementation and the
        ///     second is the interface
        /// </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration DefaultMode(this InyectorConfiguration configuarations,
                                                        Action <Type, Type> inyectorMethod)
        {
            configuarations.Modes.Add(new Mode
            {
                Name           = Mode.DefaultMode,
                InyectorMethod = inyectorMethod
            });

            return(configuarations);
        }
示例#7
0
        /// <summary>
        ///     Add Default Mode
        /// </summary>
        /// <param name="configuarations"></param>
        /// <param name="name">Mode name</param>
        /// <param name="inyectorMethod">
        ///     is the action to apply the inyector method, the first param is the implementation and the
        ///     second is the interface
        /// </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddMode(this InyectorConfiguration configuarations,
                                                    string name,
                                                    Action <Type, Type> inyectorMethod)
        {
            AddMode(configuarations, new Mode
            {
                Name           = name,
                InyectorMethod = inyectorMethod
            });

            return(configuarations);
        }
示例#8
0
        /// <summary>
        ///     Apply a Naming convention rule with an Assembly
        ///     example : ICarRepository match with CarRepository
        ///     and a pre configurated Inyector Mode
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="assembly">Assembly to apply rule</param>
        /// <param name="mode">
        ///     Mode to use to execute the inyection
        /// </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddRuleForNamingConvention(this InyectorConfiguration configuration,
                                                                       Assembly assembly,
                                                                       string mode)
        {
            configuration.Rules.Add(new Rule
            {
                Assembly     = assembly,
                Criteria     = (t1, t2) => $"I{t1.Name}" == t2.Name,
                InyectorMode = mode
            });

            return(configuration);
        }
示例#9
0
        /// <summary>
        ///     Apply a Naming convention rule with an Assembly
        ///     example : ICarRepository match with CarRepository
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="assembly">Assembly to apply rule</param>
        /// <param name="inyectorMethod">
        ///     is the action to apply the inyector method, the first param is the implementation and the
        ///     second is the interface
        /// </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddRuleForNamingConvention(this InyectorConfiguration configuration,
                                                                       Assembly assembly,
                                                                       Action <Type, Type> inyectorMethod)
        {
            configuration.Rules.Add(new Rule
            {
                Assembly       = assembly,
                Criteria       = (t1, t2) => $"I{t1.Name}" == t2.Name,
                InyectorMethod = inyectorMethod
            });

            return(configuration);
        }
示例#10
0
        /// <summary>
        ///     Init Inyector
        /// </summary>
        /// <param name="action">the method to configure the inyector </param>
        public static void Init(Action <InyectorConfiguration> action)
        {
            var configuration = new InyectorConfiguration();

            action.Invoke(configuration);

            // init the context
            InyectorContext.Assemblies = configuration.Assemblies;
            foreach (var mode in configuration.Modes)
            {
                InyectorContext.Modes[mode.Name] = mode;
            }

            InyectorEngine.Proccess(configuration);
        }
示例#11
0
        /// <summary>
        ///     Apply a Naming convention rule with an assembly
        ///     with ends with names example only inyect classes that finish with [Repository, Helper, Services, Factory, and so
        ///     on...]
        ///     example : ICarRepository match with CarRepository
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="assembly">Assembly to apply rule</param>
        /// <param name="names">names to find is the type name finish with</param>
        /// <param name="inyectorMethod">
        ///     is the action to apply the inyector method, the first param is the implementation and the
        ///     second is the interface
        /// </param>
        /// <returns>a instance of IInyectorConfiguration</returns>
        public static InyectorConfiguration AddRuleForEndsWithNamingConvention(this InyectorConfiguration configuration,
                                                                               Assembly assembly,
                                                                               IEnumerable <string> names,
                                                                               Action <Type, Type> inyectorMethod)
        {
            configuration.Rules.Add(new Rule
            {
                Assembly = assembly,
                Criteria = (t1, t2) => names.Any(n => t1.Name.ToLower().EndsWith(n.ToLower())) &&
                           $"I{t1.Name}" == t2.Name,
                InyectorMethod = inyectorMethod
            });

            return(configuration);
        }
示例#12
0
        /// <summary>
        ///     Proccess Rules with configurations and scaned types from the assembly cache
        /// </summary>
        /// <param name="configuration">inyector common configurations</param>
        private static void ProccessRules(InyectorConfiguration configuration)
        {
            foreach (var rule in configuration.Rules.AsParallel())
            {
                // the target types to find
                var target = new List <Type>();

                // add the assembly types
                if (rule.Assembly != null)
                {
                    target.AddRange(rule.Assembly.GetTypes());
                }

                // add the scanned assemblies
                target.AddRange(InyectorContext.ScanedTypes);

                // get interfaces to try match
                var interfaces = target.Where(t => t.IsInterface &&
                                              !t.GetCustomAttributes(typeof(AvoidInyectorAttribute), true).Any());

                // get the candidates types to check the criteria logic
                var candidates = target.Where(t => !t.IsInterface &&
                                              !t.IsAbstract &&
                                              !t.GetCustomAttributes(typeof(AvoidInyectorAttribute), true).Any())
                                 .ToList();

                foreach (var @interface in interfaces)
                {
                    var candidate = candidates.FirstOrDefault(c => rule.Criteria(c, @interface));

                    if (candidate == null)
                    {
                        continue;
                    }

                    // find method or mode to execute the inyection
                    var method = rule.InyectorMethod;

                    if (method == null)
                    {
                        InyectorContext.Modes.TryGetValue(rule.InyectorMode, out Mode mode);
                        method = mode?.InyectorMethod;
                    }

                    method?.Invoke(candidate, @interface);
                }
            }
        }
示例#13
0
        /// <summary>
        ///     Proccess auto inyected object with attributes
        /// </summary>
        /// <param name="configuration">inyector common configurations</param>
        private static void ProccessAutoInyectedObjects(InyectorConfiguration configuration)
        {
            // first check if exist any declareted mode
            if (!InyectorContext.Modes.Any())
            {
                return;
            }

            var autoInyectedObjects = InyectorContext.ScanedTypes.Where(t => !t.IsInterface &&
                                                                        !t.IsAbstract &&
                                                                        t.GetCustomAttributes(
                                                                            typeof(InyectAttribute),
                                                                            true)
                                                                        .Any());

            foreach (var type in autoInyectedObjects)
            {
                // get the attribute to get the meta data to inyect the object
                var attribute = type.GetCustomAttributes(typeof(InyectAttribute), true).FirstOrDefault();

                if (!(attribute is InyectAttribute))
                {
                    continue;
                }

                var inyectAttribute = (InyectAttribute)attribute;

                var mode = inyectAttribute.Mode ?? Mode.DefaultMode;

                // get the target mode
                var targetMode = InyectorContext.Modes[mode];

                // configure the target or the type itself
                targetMode?.InyectorMethod?.Invoke(type,
                                                   inyectAttribute.AbstractType ?? type);
            }
        }
示例#14
0
 /// <summary>
 ///     Apply a Naming convention rule
 ///     with ends with names example only inyect classes that finish with [Repository, Helper, Services, Factory, and so
 ///     on...]
 ///     example : ICarRepository match with CarRepository
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="names">names to find is the type name finish with</param>
 /// <param name="lifetime">AspNet Core mode to inyect </param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration AddRuleForEndsWithNamingConvention(this InyectorConfiguration configuration,
                                                                        IEnumerable <string> names,
                                                                        ServiceLifetime lifetime)
 {
     return(AddRuleForEndsWithNamingConvention(configuration, null, names, lifetime));
 }
示例#15
0
 /// <summary>
 ///     Add Rule from LifeTime using the default models
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="criteria">Criteria to find the types</param>
 /// <param name="lifetime">lifetime</param>
 /// <returns></returns>
 public static InyectorConfiguration AddRule(this InyectorConfiguration configuration,
                                             Func <Type, Type, bool> criteria,
                                             ServiceLifetime lifetime)
 {
     return(AddRule(configuration, null, criteria, lifetime));
 }
示例#16
0
 /// <summary>
 ///     Apply a Naming convention rule
 ///     example : ICarRepository match with CarRepository
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="lifetime">AspNet Core mode to inyect </param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration AddRuleForNamingConvention(this InyectorConfiguration configuration,
                                                                ServiceLifetime lifetime)
 {
     return(AddRuleForNamingConvention(configuration, null, lifetime));
 }
示例#17
0
        /// <summary>
        ///     Proccess the inyector Engine
        /// </summary>
        /// <param name="configuration">inyector common configurations</param>
        internal static void Proccess(InyectorConfiguration configuration)
        {
            ProccessRules(configuration);

            ProccessAutoInyectedObjects(configuration);
        }
示例#18
0
 /// <summary>
 ///     Apply a Naming convention rule
 ///     example : ICarRepository match with CarRepository
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="inyectorMethod">
 ///     is the action to apply the inyector method, the first param is the implementation and the
 ///     second is the interface
 /// </param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration AddRuleForNamingConvention(this InyectorConfiguration configuration,
                                                                Action <Type, Type> inyectorMethod)
 {
     return(AddRuleForNamingConvention(configuration, null, inyectorMethod));
 }
示例#19
0
 /// <summary>
 ///     Apply a Naming convention rule
 ///     with ends with names example only inyect classes that finish with [Repository, Helper, Services, Factory, and so
 ///     on...]
 ///     example : ICarRepository match with CarRepository
 ///     and a pre configurated mode
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="names">names to find is the type name finish with</param>
 /// <param name="mode"> Mode to use to execute the inyection </param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration AddRuleForEndsWithNamingConvention(this InyectorConfiguration configuration,
                                                                        IEnumerable <string> names,
                                                                        string mode)
 {
     return(AddRuleForEndsWithNamingConvention(configuration, null, names, mode));
 }
示例#20
0
 /// <summary>
 ///     Apply a Naming convention rule
 ///     example : ICarRepository match with CarRepository
 ///     and a pre configurated Inyector Mode
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="mode"> Mode to use to execute the inyection </param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration AddRuleForNamingConvention(this InyectorConfiguration configuration,
                                                                string mode)
 {
     return(AddRuleForNamingConvention(configuration, null, mode));
 }
示例#21
0
 /// <summary>
 ///     Apply a Naming convention rule
 ///     with ends with names example only inyect classes that finish with [Repository, Helper, Services, Factory, and so
 ///     on...]
 ///     example : ICarRepository match with CarRepository
 /// </summary>
 /// <param name="configuration"></param>
 /// <param name="names">names to find is the type name finish with</param>
 /// <param name="inyectorMethod">
 ///     is the action to apply the inyector method, the first param is the implementation and the
 ///     second is the interface
 /// </param>
 /// <returns>a instance of IInyectorConfiguration</returns>
 public static InyectorConfiguration AddRuleForEndsWithNamingConvention(this InyectorConfiguration configuration,
                                                                        IEnumerable <string> names,
                                                                        Action <Type, Type> inyectorMethod)
 {
     return(AddRuleForEndsWithNamingConvention(configuration, null, names, inyectorMethod));
 }