Exemplo n.º 1
0
        public IContainer Setup(Action <IContainerSetup> setup)
        {
            var setupObject = new ContainerSetup(m_records, m_plugins, m_generatorListeners);

            try
            {
                m_compiledLocatorLock.EnterWriteLock();

                setup(setupObject);

                m_locatorFactory = null;
            }
            finally
            {
                m_compiledLocatorLock.ExitWriteLock();
            }

            return(this);
        }
Exemplo n.º 2
0
        private void CompileLocator()
        {
            try
            {
                m_compiledLocatorLock.EnterWriteLock();

                SetApplicablePlugins();
                ResolveConstructors();

                m_factories = new Dictionary <string, Func <IServiceLocator, object> >();

                foreach (var record in m_records)
                {
                    if (record.Factory != null)
                    {
                        m_factories[record.Factory.Name] = record.Factory.Factory;
                    }

                    if (record.ConstructorParameters != null)
                    {
                        foreach (var param in record.ConstructorParameters.Where(p => p.ValueProvider != null))
                        {
                            m_factories[param.ValueProvider.Name] = param.ValueProvider.Factory;
                        }
                    }
                }

                var cplr = new Compiler();
                var bdr  = new LocatorBuilder();
                bdr.BuildLocatorCode(m_records, cplr);

                var code = cplr.ToString();

                try
                {
                    var assembly = cplr.Compile();

                    var factoryType = assembly.GetTypes().FirstOrDefault(i => typeof(ILocatorFactory).IsAssignableFrom(i));
                    if (factoryType == null)
                    {
                        throw new InvalidOperationException("No Servicelocator compiled");
                    }

                    var ctor = factoryType.GetConstructors().FirstOrDefault();
                    if (ctor == null)
                    {
                        throw new InvalidOperationException("Compiled locator doesn't have expected constructor");
                    }

                    m_locatorFactory = (ILocatorFactory)ctor.Invoke(new object[] { m_parent?.GetLocator(), m_factories });

                    foreach (var listener in m_generatorListeners)
                    {
                        listener.OnContainerGenerated(code, false, null);
                    }
                }
                catch (Exception ex)
                {
                    foreach (var listener in m_generatorListeners)
                    {
                        listener.OnContainerGenerated(code, true, ex);
                    }

                    throw;
                }
            }
            finally
            {
                m_compiledLocatorLock.ExitWriteLock();
            }
        }