Example #1
0
        private void Remove(Type t)
        {
            lock (m_syncRoot)
            {
                ComponentDescriptor descriptor = GetDescriptor(t);

                if (descriptor == null)
                {
                    return;
                }

                // be nice and dispose of disposable items
                if (descriptor.Instance != null)
                {
                    IDisposable d = descriptor.Instance as IDisposable;
                    if (d != null)
                    {
                        d.Dispose();
                    }
                }

                m_services.Remove(descriptor);

                if (Removed == null)
                {
                    return;
                }

                Removed(this, new DataEventArgs <object>(descriptor.Instance));
            }
        }
Example #2
0
        private void UpdateDescriptor(ComponentDescriptor descriptor)
        {
            lock (m_syncRoot)
            {
                int index = m_services.IndexOf(descriptor);

                if (index < 0)
                {
                    throw new Exception("Cannot update descriptor");
                }

                m_services[index] = descriptor;
            }
        }
Example #3
0
        internal ServiceCollection(WorkItem root)
        {
            m_root = root;

            ComponentDescriptor descriptor = new ComponentDescriptor
            {
                ClassType        = m_root.GetType(),
                Instance         = m_root,
                Name             = "WorkItem",
                RegistrationType = m_root.GetType()
            };

            m_services.Add(descriptor);
        }
Example #4
0
        private void Add(object instance, string name, Type classType, Type registrationType)
        {
            if (classType == null)
            {
                throw new ArgumentNullException("classType");
            }

            if (registrationType == null)
            {
                registrationType = classType;
            }

            lock (m_syncRoot)
            {
                ComponentDescriptor descriptor = GetDescriptor(registrationType);

                if (descriptor != null)
                {
                    throw new RegistrationTypeInUseException(string.Format("Service already registered with type '{0}'", registrationType.Name));
                }

                if (name == null)
                {
                    name = ObjectFactory.GenerateServiceName(registrationType);
                }

                descriptor = new ComponentDescriptor
                {
                    Name             = name,
                    ClassType        = classType,
                    Instance         = instance,
                    RegistrationType = registrationType
                };

                m_services.Add(descriptor);
#if !PCL
                if (instance != null)
                {
                    ObjectFactory.DoInjections(instance, m_root);
                }
#endif
                if (Added == null)
                {
                    return;
                }

                Added(this, new DataEventArgs <object>(descriptor.Instance));
            }
        }
Example #5
0
        public object Get(Type serviceType, bool ensureExists)
        {
            lock (m_syncRoot)
            {
                ComponentDescriptor desc = GetDescriptor(serviceType);

                if (desc == null)
                {
                    if (ensureExists)
                    {
                        throw new ServiceMissingException(string.Format("Cannot get service of type '{0}'", serviceType.Name));
                    }
                    return(null);
                }

                if (desc.Instance == null)
                {
                    desc.Instance = ObjectFactory.CreateObject(desc.ClassType, m_root);
                }
                return(desc.Instance);
            }
        }