Esempio n. 1
0
        /// <summary>
        /// Adds an instance of a dependency to the dependency collection.
        /// </summary>
        /// <typeparam name="TDep">Type of dependency to register.</typeparam>
        /// <typeparam name="TImpl">Type of implementation to register.</typeparam>
        /// <param name="instance">Instance to register.</param>
        /// <returns>This <see cref="DependencyCollectionBuilder"/>.</returns>
        public DependencyCollectionBuilder AddInstance <TDep, TImpl>(TImpl instance)
            where TDep : class
            where TImpl : class, TDep
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance), "Dependency cannot be null.");
            }

            var tdep  = typeof(TDep);
            var timpl = typeof(TImpl);

            this.RegisteredTypes.Add(tdep);
            if (tdep != timpl)
            {
                this.RegisteredTypes.Add(timpl);
            }

            var depinf = new DependencyInfo
            {
                DependencyType     = tdep,
                ImplementationType = timpl,
                Instance           = instance
            };

            this.RegisteredDependencies.Add(depinf);

            return(this);
        }
Esempio n. 2
0
        /// <summary>
        /// Constructs and adds a dependency by type.
        /// </summary>
        /// <typeparam name="TDep">Type of dependency.</typeparam>
        /// <typeparam name="TImpl">Type of implementation.</typeparam>
        /// <returns>This <see cref="DependencyCollectionBuilder"/>.</returns>
        public DependencyCollectionBuilder Add <TDep, TImpl>()
            where TDep : class
            where TImpl : class, TDep
        {
            var tdep  = typeof(TDep);
            var timpl = typeof(TImpl);

            var ti = timpl.GetTypeInfo();
            var cs = ti.DeclaredConstructors
                     .Where(xci => xci.IsPublic)
                     .ToArray();

            if (cs.Length != 1)
            {
                throw new ArgumentException("Specified type has more than 1 constructor.", nameof(TImpl));
            }

            var cx = cs[0];
            var ts = cx.GetParameters()
                     .Select(xpi => xpi.ParameterType)
                     .ToArray();
            var deps  = this.RegisteredDependencies.ToDictionary(xdi => xdi.ImplementationType, xdi => xdi.Instance);
            var cdeps = new object[ts.Length];

            for (var i = 0; i < ts.Length; i++)
            {
                cdeps[i] = this.GetDependency(ts[i]);
            }

            var instance = Activator.CreateInstance(timpl, cdeps);

            this.RegisteredTypes.Add(tdep);
            if (tdep != timpl)
            {
                this.RegisteredTypes.Add(timpl);
            }

            var depinf = new DependencyInfo
            {
                DependencyType     = tdep,
                ImplementationType = timpl,
                Instance           = instance
            };

            this.RegisteredDependencies.Add(depinf);

            return(this);
        }