public ServiceWithTypedFactory(IDependencyFactory factory)
        {
            _factory = factory;

            Dependency1 = factory.Create("DependencyImpl1");
            Dependency2 = factory.Create("DependencyImpl2");
            Dependency3 = factory.Create("DependencyImpl3");
        }
 public A(IDependency1 obj1, IDependencyFactory <IDependency2> dep2Factory)
 {
     _things = new List <IDependency2>();
     foreach (var x in someCollection)
     {
         _things.Add(dep2Factory.Create());
     }
 }
 /// <summary>
 /// Creates dependency <see cref="Expression"/> object. if no dependency factory is set and shared factory exists then
 /// creates dependency from the shared factory; otherwise creates from the dependency factory.
 /// </summary>
 /// <param name="interfaceType">The type of the contract.</param>
 /// <param name="register">The service register object.</param>
 /// <param name="state">The service state.</param>
 /// <param name="serviceName"></param>
 /// <param name="attributes"></param>
 /// <returns>Dependency expression</returns>
 protected virtual Expression CreateDependency(Type interfaceType, ServiceRegister register, ServiceState state, string serviceName, TypeContextAttributes attributes)
 {
     //let's subcontract take responsibility to create dependency objects
     if (DependencyFactory == null && SharedFactory != null)
     {
         return(SharedFactory.Create(interfaceType, register, state, serviceName, attributes) ?? Expression.Default(interfaceType));
     }
     return(DependencyFactory?.Create(interfaceType, register, state, serviceName, attributes) ?? Expression.Default(interfaceType));
 }
Beispiel #4
0
    void DoSomething()
    {
        // Do not call the container, let it call you.
        // So no container usage here.
        //var container = ...

        Console.Write("Hey, user. How are you feeling?");
        var userInput = Console.ReadLine();

        var dependency = _factory.Create(userInput);
        // There you go!
    }
Beispiel #5
0
        public void BeforeAdd_WhenThereIsMatchingDependencyWithAliasApplied_ShouldUpdateCaptionForCurrentDependency()
        {
            // Both top level
            // Same provider type
            // Duplicate caption, though with parenthesized text after one instance
            //   -> Changes caption of non-parenthesized

            const string providerType = "myprovider";
            const string caption      = "MyCaption";

            var dependencyReplacement = IDependencyFactory.Create();

            var dependency = IDependencyFactory.Implement(
                providerType: providerType,
                id: "mydependency1",
                caption: caption,
                alias: "mydependency1 (mydependency1ItemSpec)",
                setPropertiesCaption: "mydependency1 (mydependency1ItemSpec)", // should set caption to its alias
                setPropertiesReturn: dependencyReplacement,
                topLevel: true);

            var otherDependency = IDependencyFactory.Implement(
                originalItemSpec: "mydependency2ItemSpec",
                providerType: providerType,
                id: "mydependency2",
                caption: $"{caption} (mydependency2ItemSpec)",     // caption already includes alias
                topLevel: true);

            var worldBuilder = new[] { dependency.Object, otherDependency.Object }.ToImmutableDictionary(d => d.Id).ToBuilder();

            var filter = new DuplicatedDependenciesSnapshotFilter();

            var resultDependency = filter.BeforeAdd(
                null,
                null,
                dependency.Object,
                worldBuilder,
                null,
                null,
                out bool filterAnyChanges);

            dependency.VerifyAll();
            otherDependency.VerifyAll();

            Assert.True(filterAnyChanges);
            Assert.Same(dependencyReplacement, resultDependency);
            Assert.Same(dependency.Object, worldBuilder[dependency.Object.Id]); // doesn't update worldBuilder
            Assert.Same(otherDependency.Object, worldBuilder[otherDependency.Object.Id]);
        }