/// <summary>
        /// Returns a default instance of type <typeparamref name="T"/> based on configuration information
        /// from <paramref name="configurationSource"/>.
        /// </summary>
        /// <typeparam name="T">The type to build.</typeparam>
        /// <param name="locator">The locator to be used for this build operation.</param>
        /// <param name="lifetimeContainer">The lifetime container to be used for this build operation.</param>
        /// <param name="configurationSource">The source for configuration information.</param>
        /// <returns>A new instance of <typeparamref name="T"/> or any of it subtypes, or an existing instance
        /// if type <typeparamref name="T"/> is a singleton that is already present in the <paramref name="locator"/>.
        /// </returns>
        public static T BuildUp <T>(IReadWriteLocator locator,
                                    ILifetimeContainer lifetimeContainer,
                                    IConfigurationSource configurationSource)
        {
            if (configurationSource == null)
            {
                throw new ArgumentNullException("configurationSource");
            }

            try
            {
                return(GetObjectBuilder()
                       .BuildUp <T>(locator,
                                    lifetimeContainer,
                                    GetPolicies(configurationSource),
                                    strategyChain,
                                    NamedTypeBuildKey.Make <T>(),
                                    null));
            }
            catch (BuildFailedException e)
            {
                // look for the wrapped ConfigurationErrorsException, if any, and throw it
                ConfigurationErrorsException cee = GetConfigurationErrorsException(e);
                if (cee != null)
                {
                    throw cee;
                }

                // unknown exception, bubble it up
                throw;
            }
        }
Example #2
0
 /// <summary>
 /// Initializes an instance of the <see cref="ServiceCollection"/> class.
 /// </summary>
 /// <param name="container">The lifetime container the collection will use</param>
 /// <param name="locator">The locator the collection will use</param>
 /// <param name="builder">The builder the collection will use</param>
 /// <param name="parent">The parent collection</param>
 public ServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <BuilderStage> builder, ServiceCollection parent)
 {
     this.builder   = builder;
     this.container = container;
     this.locator   = locator;
     this.parent    = parent;
 }
Example #3
0
        /// <summary>
        /// Initializes an instance of the <see cref="ManagedObjectCollection{TItem}"/> class.
        /// </summary>
        public ManagedObjectCollection(
            ILifetimeContainer container,
            IReadWriteLocator locator,
            IBuilder <WCSFBuilderStage> builder,
            SearchMode searchMode,
            IndexerCreationDelegate indexerCreationDelegate,
            Predicate <TItem> filter,
            ManagedObjectCollection <TItem> parentCollection)
        {
            _lifetimeContainer       = container;
            _locator                 = locator;
            _builder                 = builder;
            _searchMode              = searchMode;
            _indexerCreationDelegate = indexerCreationDelegate;
            _filter           = filter;
            _parentCollection = parentCollection;
            _container        =
                locator.Get <CompositionContainer>(new DependencyResolutionLocatorKey(typeof(CompositionContainer), null));

            if (_container != null)
            {
                _container.ObjectAdded   += new EventHandler <DataEventArgs <object> >(Container_ItemAdded);
                _container.ObjectRemoved += new EventHandler <DataEventArgs <object> >(Container_ItemRemoved);
            }
        }
 private TestableServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <BuilderStage> builder, TestableServiceCollection parent)
     : base(container, locator, builder, parent)
 {
     this.builder   = builder;
     this.container = container;
     this.locator   = locator;
 }
Example #5
0
        private void InitializeFields()
        {
            if (builder == null)
            {
                builder = parent.builder;
            }

            if (locator == null)
            {
                locator = new Locator(parent.locator);
            }

            if (!locator.Contains(typeof(ILifetimeContainer), SearchMode.Local))
            {
                locator.Add(typeof(ILifetimeContainer), lifetime);
            }

            ObjectBuiltNotificationPolicy policy = builder.Policies.Get <ObjectBuiltNotificationPolicy>(null, null);

            if (policy != null)
            {
                policy.AddedDelegates[this]   = new ObjectBuiltNotificationPolicy.ItemNotification(OnObjectAdded);
                policy.RemovedDelegates[this] = new ObjectBuiltNotificationPolicy.ItemNotification(OnObjectRemoved);
            }

            LocateWorkItem(typeof(WorkItem));
            LocateWorkItem(GetType());

            status = WorkItemStatus.Inactive;
        }
 private BuilderContainer(BuilderContainer parent)
 {
     builder  = new BuilderBase <BuilderStage>(parent.configurator);
     lifetime = new LifetimeContainer();
     locator  = new Locator(parent.locator);
     locator.Add(typeof(ILifetimeContainer), lifetime);
 }
Example #7
0
 /// <summary>
 /// Initializes an instance of the <see cref="ServiceCollection"/> class.
 /// </summary>
 /// <param name="container">The lifetime container the collection will use</param>
 /// <param name="locator">The locator the collection will use</param>
 /// <param name="builder">The builder the collection will use</param>
 /// <param name="parent">The parent collection</param>
 public ServiceCollection(ILifetimeContainer container, IReadWriteLocator locator, IBuilder <WCSFBuilderStage> builder,
                          IServiceCollection parent)
 {
     _builder   = builder;
     _container = container;
     _locator   = locator;
     _parent    = parent;
 }
        /// <summary>
        /// Initializes the <see cref="CompositionContainer"/> when it is the root of the containment hierarchy.
        /// </summary>
        /// <param name="builder"></param>
        public void InitializeRootContainer(IBuilder <WCSFBuilderStage> builder)
        {
            _builder = builder;
            _locator = new Locator();

            InitializeFields();
            InitializeCollectionFacades();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="LocatorNameTypeFactoryBase{T}"/> class with a configuration source
        /// and a locator.
        /// </summary>
        /// <param name="configurationSource">The configuration source to use.</param>
        protected LocatorNameTypeFactoryBase(IConfigurationSource configurationSource)
        {
            this.configurationSource = configurationSource;

            locator           = new Locator();
            lifetimeContainer = new LifetimeContainer();
            locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
        }
        /// <summary>
        /// Returns a default instance of type <typeparamref name="T"/> based on configuration information
        /// from <paramref name="configurationSource"/>.
        /// </summary>
        /// <typeparam name="T">The type to build.</typeparam>
        /// <param name="locator">The locator to be used for this build operation.</param>
        /// <param name="configurationSource">The source for configuration information.</param>
        /// <returns>A new instance of <typeparamref name="T"/> or any of it subtypes, or an existing instance
        /// if type <typeparamref name="T"/> is a singleton that is already present in the <paramref name="locator"/>.
        /// </returns>
        public static T BuildUp <T>(IReadWriteLocator locator, IConfigurationSource configurationSource)
        {
            if (configurationSource == null)
            {
                throw new ArgumentNullException("configurationSource");
            }

            return(GetObjectBuilder().BuildUp <T>(locator, null, null, GetPolicies(configurationSource)));
        }
Example #11
0
        private static void CountReturnsNumberOfKeysWithLiveValues_TestHelper(IReadWriteLocator locator)
        {
            object o = new object();

            locator.Add("foo1", o);
            locator.Add("foo2", o);

            Assert.AreEqual(2, locator.Count);
        }
Example #12
0
        static MockBuilderContext BuildContext(IReadWriteLocator locator)
        {
            MockBuilderContext ctx = new MockBuilderContext(locator);

            ctx.Strategies.Add(new SingletonStrategy());
            ctx.Strategies.Add(new SimpleCreationStrategy());

            return(ctx);
        }
        /// <summary>
        /// See <see cref="IBuilder{TStageEnum}.TearDown{T}"/> for more information.
        /// </summary>
        public TItem TearDown <TItem>(IReadWriteLocator locator, TItem item)
        {
            if (typeof(TItem).IsValueType == false && item == null)
            {
                throw new ArgumentNullException("item");
            }

            return(DoTearDown <TItem>(locator, item));
        }
Example #14
0
        /// <summary>
        /// Initializes a root <see cref="WorkItem"/>. Intended to be called by the user who creates the
        /// first WorkItem using new. Subsequent child WorkItem classes are automatically initialized
        /// through dependency injection from their parent.
        /// </summary>
        /// <param name="builder">The <see cref="Builder"/> used to build objects.</param>
        protected internal void InitializeRootWorkItem(Builder builder)
        {
            this.builder = builder;
            this.locator = new Locator();

            InitializeFields();
            InitializeCollectionFacades();
            InitializeState();
        }
Example #15
0
 public TTypeToBuild BuildUp <TTypeToBuild>(IReadWriteLocator locator,
                                            ILifetimeContainer lifetime,
                                            IPolicyList policies,
                                            IStrategyChain strategies,
                                            object buildKey,
                                            object existing)
 {
     return((TTypeToBuild)BuildUp(locator, lifetime, policies, strategies, buildKey, existing));
 }
Example #16
0
 /// <summary>
 /// Initializes an instance of the <see cref="ManagedObjectCollection{TItem}"/> class.
 /// </summary>
 public ManagedObjectCollection(
     ILifetimeContainer container,
     IReadWriteLocator locator,
     IBuilder <WCSFBuilderStage> builder,
     SearchMode searchMode,
     IndexerCreationDelegate indexerCreationDelegate,
     Predicate <TItem> filter)
     : this(container, locator, builder, searchMode, indexerCreationDelegate, filter, null)
 {
 }
Example #17
0
        /// <summary>
        /// Initializes a new instance of the container using the specified configurator.
        /// </summary>
        /// <param name="configurator">The configurator that provides default
        /// strategies for the container.</param>
        public BuilderContainer(IBuilderConfigurator <BuilderStage> configurator)
        {
            Guard.ArgumentNotNull(configurator, "configurator");

            lifetime = new LifetimeContainer();
            locator  = new Locator();
            locator.Add(typeof(ILifetimeContainer), lifetime);
            builder           = new BuilderBase <BuilderStage>(configurator);
            this.configurator = configurator;
        }
Example #18
0
        public MockBuilderContext(IReadWriteLocator locator)
        {
            InnerLocator = locator;
            SetLocator(InnerLocator);
            StrategyChain = InnerChain;
            SetPolicies(InnerPolicies);

            if (!Locator.Contains(typeof(ILifetimeContainer)))
                Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
        }
Example #19
0
 /// <summary>
 /// Create a new <see cref="BuilderContext"/> using the explicitly provided
 /// values.
 /// </summary>
 /// <param name="chain">The <see cref="IStrategyChain"/> to use for this context.</param>
 /// <param name="locator">The <see cref="IReadWriteLocator"/> to use for this context.</param>
 /// <param name="lifetime">The <see cref="ILifetimeContainer"/> to use for this context.</param>
 /// <param name="persistentPolicies">The set of persistent policies to use for this context.</param>
 /// <param name="transientPolicies">The set of transient policies to use for this context. It is
 /// the caller's responsibility to ensure that the transient and persistent policies are properly
 /// combined.</param>
 /// <param name="buildKey">Build key for this context.</param>
 /// <param name="existing">Existing object to build up.</param>
 public BuilderContext(IStrategyChain chain, IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList persistentPolicies, IPolicyList transientPolicies, object buildKey, object existing)
 {
     this.chain              = chain;
     this.lifetime           = lifetime;
     this.locator            = locator;
     this.persistentPolicies = persistentPolicies;
     this.policies           = transientPolicies;
     this.originalBuildKey   = buildKey;
     this.buildKey           = buildKey;
     this.existing           = existing;
 }
Example #20
0
 /// <summary>
 /// Create a new <see cref="BuilderContext"/> using the explicitly provided
 /// values.
 /// </summary>
 /// <param name="chain">The <see cref="IStrategyChain"/> to use for this context.</param>
 /// <param name="locator">The <see cref="IReadWriteLocator"/> to use for this context.</param>
 /// <param name="lifetime">The <see cref="ILifetimeContainer"/> to use for this context.</param>
 /// <param name="persistentPolicies">The set of persistent policies to use for this context.</param>
 /// <param name="transientPolicies">The set of transient policies to use for this context. It is
 /// the caller's responsibility to ensure that the transient and persistent policies are properly
 /// combined.</param>
 /// <param name="buildKey">Build key for this context.</param>
 /// <param name="existing">Existing object to build up.</param>
 public BuilderContext(IStrategyChain chain, IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList persistentPolicies, IPolicyList transientPolicies, object buildKey, object existing)
 {
     this.chain = chain;
     this.lifetime = lifetime;
     this.locator = locator;
     this.persistentPolicies = persistentPolicies;
     this.policies = transientPolicies;
     this.originalBuildKey = buildKey;
     this.buildKey = buildKey;
     this.existing = existing;
 }
Example #21
0
        private IBuilderContext MakeContext(IBuilderStrategyChain chain,
                                            IReadWriteLocator locator, params PolicyList[] transientPolicies)
        {
            PolicyList policies = new PolicyList(this.policies);

            foreach (PolicyList policyList in transientPolicies)
            {
                policies.AddPolicies(policyList);
            }

            return(new BuilderContext(chain, locator, policies));
        }
Example #22
0
 static void FindInLocator(Predicate <KeyValuePair <object, object> > predicate,
                           IReadWriteLocator results,
                           IEnumerable <KeyValuePair <object, object> > currentLocator)
 {
     foreach (KeyValuePair <object, object> kvp in currentLocator)
     {
         if (!results.Contains(kvp.Key) && predicate(kvp))
         {
             results.Add(kvp.Key, kvp.Value);
         }
     }
 }
Example #23
0
        public MockBuilderContext(IReadWriteLocator locator)
        {
            InnerLocator = locator;
            SetLocator(InnerLocator);
            StrategyChain = InnerChain;
            SetPolicies(InnerPolicies);

            if (!Locator.Contains(typeof(ILifetimeContainer)))
            {
                Locator.Add(typeof(ILifetimeContainer), lifetimeContainer);
            }
        }
        private TItem DoTearDown <TItem>(IReadWriteLocator locator, TItem item)
        {
            IBuilderStrategyChain chain = _strategies.MakeReverseStrategyChain();

            ThrowIfNoStrategiesInChain(chain);

            IBuilderContext context = MakeContext(chain, locator);

            TItem result = (TItem)chain.Head.TearDown(context, item);

            return(result);
        }
 public TestableManagedObjectCollection(ILifetimeContainer container, IReadWriteLocator locator,
                                        IBuilder <BuilderStage> builder, SearchMode searchMode, IndexerCreationDelegate indexerCreationDelegate,
                                        Predicate <TItem> predicate, TestableManagedObjectCollection <TItem> parentCollection)
     : base(container, locator, builder, searchMode, indexerCreationDelegate, predicate, parentCollection)
 {
     this.builder    = builder;
     this.container  = container;
     this.locator    = locator;
     this.searchMode = searchMode;
     this.indexerCreationDelegate = indexerCreationDelegate;
     this.predicate = predicate;
 }
Example #26
0
 public BuilderContext(IStrategyChain chain,
                       IReadWriteLocator locator,
                       ILifetimeContainer lifetime,
                       IPolicyList policies,
                       object originalBuildKey)
 {
     this.chain            = chain;
     this.locator          = locator;
     this.lifetime         = lifetime;
     this.originalBuildKey = originalBuildKey;
     this.policies         = new PolicyList(policies);
 }
Example #27
0
        private MockBuilderContext CreateContext(IReadWriteLocator locator)
        {
            MockBuilderContext result = new MockBuilderContext(locator);

            result.InnerChain.Add(new SingletonStrategy());
            result.InnerChain.Add(new PropertyReflectionStrategy());
            result.InnerChain.Add(new CreationStrategy());
            result.InnerChain.Add(new PropertySetterStrategy());
            result.Policies.SetDefault <ISingletonPolicy>(new SingletonPolicy(true));
            result.Policies.SetDefault <ICreationPolicy>(new DefaultCreationPolicy());
            return(result);
        }
        private MockBuilderContext CreateContext(IReadWriteLocator locator, IConfigurationSource configurationSource)
        {
            MockBuilderContext context = new MockBuilderContext(locator);

            context.InnerChain.Add(new SingletonStrategy());
            context.InnerChain.Add(
                new MockFactoryStrategy(
                    new LogWriterCustomFactory(),
                    configurationSource,
                    new ConfigurationReflectionCache()));

            return(context);
        }
        /// <summary>
        /// Returns a default instance of type <typeparamref name="T"/> based on configuration information
        /// from <paramref name="configurationSource"/> for <paramref name="id"/>.
        /// </summary>
        /// <typeparam name="T">The type to build.</typeparam>
        /// <param name="id">The id of the object to build.</param>
        /// <param name="locator">The locator to be used for this build operation.</param>
        /// <param name="configurationSource">The source for configuration information.</param>
        /// <returns>A new instance of <typeparamref name="T"/> or any of it subtypes, or an existing instance
        /// if type <typeparamref name="T"/> is a singleton that is already present in the <paramref name="locator"/>.
        /// </returns>
        public static T BuildUp <T>(IReadWriteLocator locator, string id, IConfigurationSource configurationSource)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException(Resources.ExceptionStringNullOrEmpty, "id");
            }
            if (configurationSource == null)
            {
                throw new ArgumentNullException("configurationSource");
            }

            return(GetObjectBuilder().BuildUp <T>(locator, id, null, GetPolicies(configurationSource)));
        }
        /// <summary>
        /// Utility method to initialize an object instance without adding it to the container.
        /// </summary>
        /// <param name="builder">The <see cref="IBuilder{TStageEnum}"/> to use to build the object.</param>
        /// <param name="locator">The <see cref="IReadWriteLocator"/> to use in building the object.</param>
        /// <param name="item">The object to build.</param>
        /// <returns>The built and initialized object.</returns>
        public static object BuildItem(IBuilder <WCSFBuilderStage> builder, IReadWriteLocator locator, object item)
        {
            Type   itemType    = item.GetType();
            string temporaryId = Guid.NewGuid().ToString();

            PolicyList policies = new PolicyList();

            policies.Set <ISingletonPolicy>(new SingletonPolicy(false), itemType, temporaryId);
            policies.Set <ICreationPolicy>(new DefaultCreationPolicy(), itemType, temporaryId);
            policies.Set <IPropertySetterPolicy>(new PropertySetterPolicy(), itemType, temporaryId);

            return(builder.BuildUp(locator, itemType, temporaryId, item, policies));
        }
        private object DoBuildUp(IReadWriteLocator locator, Type typeToBuild, string idToBuild, object existing,
                                 PolicyList[] transientPolicies)
        {
            IBuilderStrategyChain chain = _strategies.MakeStrategyChain();

            ThrowIfNoStrategiesInChain(chain);

            IBuilderContext context = MakeContext(chain, locator, transientPolicies);

            object result = chain.Head.BuildUp(context, typeToBuild, existing, idToBuild);

            return(result);
        }
Example #32
0
        public object BuildUp(IReadWriteLocator locator,
                              ILifetimeContainer lifetime,
                              IPolicyList policies,
                              IStrategyChain strategies,
                              object buildKey,
                              object existing)
        {
            Guard.ArgumentNotNull(strategies, "strategies");

            BuilderContext context = new BuilderContext(strategies, locator, lifetime, policies, buildKey, existing);

            return(strategies.ExecuteBuildUp(context));
        }
        private MockBuilderContext CreateContext(IReadWriteLocator locator)
        {
            IBuilderStrategy[] strategies = {
                                                new SingletonStrategy(),
                                                new PropertyReflectionStrategy(),
                                                new CreationStrategy(),
                                                new PropertySetterStrategy()
                                            };
            MockBuilderContext result = new MockBuilderContext(strategies);
            result.Locator = locator;

            LifetimeContainer lifetimeContainer = new LifetimeContainer();

            if (!result.Locator.Contains(typeof (ILifetimeContainer)))
                result.Locator.Add(typeof (ILifetimeContainer), lifetimeContainer);

            result.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
            result.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
            return result;
        }
 private MockBuilderContext CreateContext(IReadWriteLocator locator)
 {
     MockBuilderContext result = new MockBuilderContext(locator);
     result.InnerChain.Add(new SingletonStrategy());
     result.InnerChain.Add(new MethodReflectionStrategy());
     result.InnerChain.Add(new CreationStrategy());
     result.InnerChain.Add(new MethodExecutionStrategy());
     result.Policies.SetDefault<ICreationPolicy>(new DefaultCreationPolicy());
     result.Policies.SetDefault<ISingletonPolicy>(new SingletonPolicy(true));
     return result;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="BuilderContext"/> class using
 /// the provided chain, locator, and policies.
 /// </summary>
 /// <param name="chain">The strategy chain.</param>
 /// <param name="locator">The locator.</param>
 /// <param name="policies">The permanent policies from the builder.</param>
 public BuilderContext(IBuilderStrategyChain chain, IReadWriteLocator locator, PolicyList policies)
 {
     this.chain = chain;
     this.locator = locator;
     this.policies = new PolicyList(policies);
 }
            public TestableServiceCollection(ILifetimeContainer container, IReadWriteLocator locator,
			                                 IBuilder<WCSFBuilderStage> builder)
                : this(container, locator, builder, null)
            {
            }
 /// <summary>
 /// Sets the locator.
 /// </summary>
 protected void SetLocator(IReadWriteLocator locator)
 {
     this.locator = locator;
 }
		private MockBuilderContext CreateContext(IReadWriteLocator locator, IConfigurationSource configurationSource)
		{
			MockBuilderContext context = new MockBuilderContext(locator);
			context.InnerChain.Add(new SingletonStrategy());
			context.InnerChain.Add(
				new MockFactoryStrategy(
					new LogWriterCustomFactory(),
					configurationSource,
					new ConfigurationReflectionCache()));

			return context;
		}
            private TestableServiceCollection(ILifetimeContainer container, IReadWriteLocator locator,
			                                  IBuilder<WCSFBuilderStage> builder, TestableServiceCollection parent)
                : base(container, locator, builder, parent)
            {
                this.builder = builder;
                this.container = container;
                this.locator = locator;
            }