Esempio n. 1
0
 public InstantiateModel(MethodInfo methodInfo, string name)
 {
     Name              = NamingHelper.ConvertToCamelCase(name);
     Type              = methodInfo.ReturnType;
     Method            = methodInfo;
     Weight            = Method.GetParameters().Length;
     IsConstructorType = false;
 }
Esempio n. 2
0
 public InstantiateModel(Type type, string name)
 {
     Name              = NamingHelper.ConvertToCamelCase(name);
     Type              = type;
     Method            = type.GetConstructors()[0];
     Weight            = Method.GetParameters().Length;
     IsConstructorType = true;
 }
Esempio n. 3
0
        public IContext LoadContext()
        {
            var propertyDictionary = PropertyLoader.Load(ContextType);
            // inject property into context
            var contextProperties = from PropertyInfo propertyInfo
                                    in ContextType.GetProperties()
                                    where propertyInfo.PropertyType == typeof(string) &&
                                    AttributeHelper.IsPropertyMarked <PropertyValue>(propertyInfo)
                                    select(propertyInfo, AttributeHelper.getAttribute <PropertyValue>(propertyInfo));

            foreach (var property in contextProperties)
            {
                property.Item1.SetValue(Context, propertyDictionary[property.Item2.Name]);
            }

            var contextBuilder = new InjectorContext.InjectorContextBuilder();
            // initialize models
            var priorityQueue    = new SimplePriorityQueue <int, InstantiateModel>();
            var methodsInContext = from MethodInfo methodInfo
                                   in ContextType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                                   where AttributeHelper.IsMethodMarked <Instantiate>(methodInfo)
                                   select methodInfo;

            foreach (var methodInfo in methodsInContext)
            {
                var attr  = methodInfo.GetCustomAttribute <Instantiate>();
                var model = string.IsNullOrEmpty(attr.Name) ? AddModel(methodInfo) : AddModel(methodInfo, attr.Name);

                if (model != null)
                {
                    priorityQueue.Enqueue(model.Weight, model);
                }
            }

            var namespaceToScan = AttributeHelper.GetNamespaceToScan(ContextType);
            var assembly        = Assembly.GetCallingAssembly();

            var scanTypes = from Type type
                            in assembly.GetTypes()
                            where AttributeHelper.IsTypeMarked <Instantiate>(type) && namespaceToScan.Contains(type.Namespace)
                            select type;

            // create models to instantiate
            foreach (var scanType in scanTypes)
            {
                var attr  = scanType.GetCustomAttribute <Instantiate>();
                var model = string.IsNullOrEmpty(attr.Name) ? AddModel(scanType) : AddModel(scanType, attr.Name);

                if (model != null)
                {
                    priorityQueue.Enqueue(model.Weight, model);
                }
            }

            // Create Objects
            var previousCount = priorityQueue.Count;

            while (!priorityQueue.IsEmpty)
            {
                var model = priorityQueue.Dequeue();
                if (CanInstantiate(model))
                {
                    var instance = Instantiate(model);
                    // property injection
                    if (model.IsConstructorType)
                    {
                        var properties = from PropertyInfo propertyInfo in model.Type.GetProperties()
                                         where AttributeHelper.IsPropertyMarked <PropertyValue>(propertyInfo)
                                         select(propertyInfo, AttributeHelper.getAttribute <PropertyValue>(propertyInfo));

                        foreach (var property in properties)
                        {
                            var name = property.Item2.Name;
                            property.Item1.SetValue(instance, propertyDictionary[name]);
                        }
                    }
                    ObjectStorage[model.Name] = instance;
                }
                else
                {
                    UpdateWeight(model);
                    priorityQueue.Enqueue(model.Weight, model);
                }

                if (previousCount <= priorityQueue.Count)
                {
                    throw new DependencyNotFoundException(model);
                }

                previousCount = priorityQueue.Count;
            }

            // Property Injection
            foreach (var instance in ObjectStorage.Values)
            {
                var instanceType = instance.GetType();
                var properties   = from PropertyInfo propertyInfo
                                   in instanceType.GetProperties()
                                   where AttributeHelper.IsPropertyMarked <Autowired>(propertyInfo)
                                   select propertyInfo;
                foreach (var property in properties)
                {
                    var attr = property.GetCustomAttribute <Autowired>();
                    property.SetValue(instance,
                                      !string.IsNullOrEmpty(attr?.Name)
                            ? ObjectStorage[attr.Name]
                            : ObjectStorage[NamingHelper.ConvertToCamelCase(property.Name)]);
                }
            }

            // invoke post constructor
            foreach (var name in ObjectStorage.Keys)
            {
                var model = ModelStorage[name];
                if (!model.IsConstructorType)
                {
                    continue;
                }
                var type             = model.Type;
                var instance         = ObjectStorage[name];
                var postConstructors =
                    from MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance)
                    where AttributeHelper.IsMethodMarked <PostConsturctor>(method)
                    select method;
                foreach (var postConstructor in postConstructors)
                {
                    postConstructor.Invoke(instance, null);
                }
            }
            // Build
            foreach (var pair in ObjectStorage)
            {
                contextBuilder.Add(pair.Key, pair.Value);
            }

            return(contextBuilder.Build());
        }
Esempio n. 4
0
 public InstantiateModel(MethodInfo methodInfo)
     : this(methodInfo, NamingHelper.ConvertToCamelCase(methodInfo.Name))
 {
 }