Exemplo n.º 1
0
        internal static void RegisterType(Type type, string lifeCycle)
        {
            var exportAttributes = type.GetCustomAttributes(typeof(EventExportAttribute), false);

            if (exportAttributes == null || exportAttributes.Length == 0)
            {
                return;
            }

            EventExportAttribute exportAttribute = exportAttributes[0] as EventExportAttribute;

            if (exportAttribute.EventType == null)
            {
                return;
            }

            var eventType = exportAttribute.EventType;

            switch (GetObjectLifeCycle(lifeCycle))
            {
            case ObjectLifeCycle.Singleton:
                ObjectContainer.RegisterInstance(exportAttribute.InterfaceType, Activator.CreateInstance(type));

                if (eventHandlerContainer.ContainsKey(eventType))
                {
                    eventHandlerContainer[eventType].Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                }
                else
                {
                    IList <EventHandlerKeyValuePair> tempTypes = new List <EventHandlerKeyValuePair>();
                    tempTypes.Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                    eventHandlerContainer.Add(eventType, tempTypes);
                }
                break;

            case ObjectLifeCycle.New:
                if (eventHandlerContainer.ContainsKey(eventType))
                {
                    if (eventHandlerContainer[eventType].Where(pre => pre.ImplementType == type).Count() > 0)
                    {
                        return;
                    }

                    eventHandlerContainer[eventType].Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                }
                else
                {
                    IList <EventHandlerKeyValuePair> tempTypes = new List <EventHandlerKeyValuePair>();
                    tempTypes.Add(new EventHandlerKeyValuePair()
                    {
                        InterfaceType = exportAttribute.InterfaceType,
                        ImplementType = type
                    });
                    eventHandlerContainer.Add(eventType, tempTypes);
                }

                ObjectContainer.RegisterType(exportAttribute.InterfaceType, type);

                ObjectContainer.RegisterType(type, type);

                break;
            }
        }