public WhenRunningInTheDebuggerFactoryRegistrationExtraShould()
        {
            _implementationType = GetType();
            var sut = new FactoryRegistration <int>(_implementationType, c => null);

            GetDebuggerDisplay(sut);
        }
Пример #2
0
        public void GuyWire_Delegates_To_FactoryInstaller()
        {
            var installer = new FactoryRegistration();

            _guyWire.Expect(g => g.ComponentsInfo).Return(new[] { installer });

            Assert.DoesNotThrow(() => _guyWire.Wire());
        }
        public static RegistrationBuilder RegisterFactory <TParam1, TParam2, T>(
            this IContainerBuilder builder,
            Func <IObjectResolver, Func <TParam1, TParam2, T> > factoryFactory,
            Lifetime lifetime)
        {
            var registrationBuilder = new FactoryRegistration <TParam1, TParam2, T>(factoryFactory, lifetime);

            builder.Register(registrationBuilder);
            return(registrationBuilder);
        }
Пример #4
0
 public static void Register(this Container container, AppSettings appSettings, LoggerSettings loggerSettings)
 {
     MediatorRegistration.Register(container);
     AutoMapperRegistration.Register(container);
     FactoryRegistration.Register(container);
     ServiceRegistration.Register(container);
     ValidationRegistration.Register(container);
     SettingsRegistration.Register(container, appSettings, loggerSettings);
     LoggingRegistration.Register(container);
     GCPRegistrations.Register(container);
 }
        /// <summary>
        /// Accepts the <see cref="FactoryRegistration{TExtra}"/> to visit.
        /// </summary>
        /// <param name="registration">The <see cref="FactoryRegistration{TExtra}"/> to visit.</param>
        public void Accept(FactoryRegistration <TExtra> registration)
        {
            if (registration == null)
            {
                throw new ArgumentNullException(nameof(registration));
            }

            Type type        = registration.ImplementationType;
            var  composition = new FactoryComposition(type, registration.Factory, typeof(ConstructionContext <TExtra>));

            _container.AddComposition(composition);
        }
Пример #6
0
        private FactoryRegistrationOptions ProcessRegisterFactory(Type type, string name, Func <object> factory)
        {
            this.CheckRegistered(type, name);

            var registration        = new FactoryRegistration(type, name, factory);
            var registrationOptions = new FactoryRegistrationOptions(registration);

            this.AddRegistration(type, name, registration);

            this.RaiseRegistered(registration);

            return(registrationOptions);
        }
 public void Handle(FactoryRegistration serviceRegistration)
 => _container.Configure(x =>
 {
     x.For(serviceRegistration.InterfaceType).Use("Factory registration", context =>
     {
         var factoryContext = new FactoryContext
         {
             ParentType    = context.ParentType,
             RequestedName = context.RequestedName
         };
         var factory = context.GetInstance(serviceRegistration.FactoryType);
         return(serviceRegistration.FactoryMethod(factory, factoryContext));
     });
 });
Пример #8
0
        IAsBuilderWithCacheability RegisterTypedFactory <TService>(Delegate factory) where TService : class
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            var registration = new FactoryRegistration <TService>(factory)
            {
                ServiceType = typeof(TService)
            };

            registrations.Add(registration);
            return(new RegistrationBuilder(registration));
        }
        public void Set_OnResolved()
        {
            var registration = new FactoryRegistration(typeof(Item), "item", () => new Item());

            var service = Activator.CreateInstance(typeof(FactoryRegistrationOptions),
                                                   BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, new object[] { registration }, null) as FactoryRegistrationOptions;


            Assert.AreEqual(null, registration.OnResolved);

            var action = new Action <ContainerRegistration, object>((c, p) => { });

            service.OnResolved(action);

            Assert.AreEqual(action, registration.OnResolved);
        }
Пример #10
0
        /// <inheritdoc />
        public IUnityContainer RegisterFactory(Type type, string?name, Func <IUnityContainer, Type, string?, object?> factory, IFactoryLifetimeManager?lifetimeManager)
        {
            // Validate input
            if (null == type)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (null == factory)
            {
                throw new ArgumentNullException(nameof(factory));
            }

            // Lifetime Manager
            var manager = lifetimeManager as LifetimeManager ??
                          Context.FactoryLifetimeManager.CreateLifetimePolicy();

            // Target Container
            var container = manager is SingletonLifetimeManager ? _root : this;

            Debug.Assert(null != container);

            // Create registration
            var registration = new FactoryRegistration(container, type, name, factory, manager);

            // Register
            var previous = container.Register(type, name, registration);

            // Allow reference adjustment and disposal
            if (null != previous && 0 == previous.Release() &&
                previous.LifetimeManager is IDisposable disposable)
            {
                // Dispose replaced lifetime manager
                container.LifetimeContainer.Remove(disposable);
                disposable.Dispose();
            }

            // Check what strategies to run
            registration.Processors = Context.FactoryPipelineCache;

            // Raise event
            container.Registering?.Invoke(this, new RegisterEventArgs(type, type, name, manager));

            // Return Container
            return(this);
        }
Пример #11
0
        /// <summary>
        /// Registers a component via a factory function.  The provided delegate is expected to produce an instance of the
        /// specified component type when it is invoked.
        /// </summary>
        /// <returns>A builder instance.</returns>
        /// <param name="factory">A delegate which shall create the component instance.</param>
        /// <param name="serviceType">The type for which to register the service.  This must be assignable from the return value of the delegate.</param>
        public IRegistrationOptionsBuilderWithCacheability RegisterFactory(Delegate factory, Type serviceType)
        {
            if (factory == null)
            {
                throw new ArgumentNullException(nameof(factory));
            }
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            var registration = new FactoryRegistration(factory)
            {
                ServiceType = serviceType
            };

            registrations.Add(registration);
            return(new RegistrationBuilder(registration));
        }
Пример #12
0
        public IStrategyRegistration RegisterFactoryAs(Delegate factoryDelegate, Type interfaceType, string name = null)
        {
            if (factoryDelegate == null)
            {
                throw new ArgumentNullException("factoryDelegate");
            }
            if (interfaceType == null)
            {
                throw new ArgumentNullException("interfaceType");
            }

            var registrationKey = new RegistrationKey(interfaceType, name);

            AssertNotResolved(registrationKey);

            ClearRegistrations(registrationKey);
            var factoryRegistration = new FactoryRegistration(factoryDelegate);

            AddRegistration(registrationKey, factoryRegistration);

            return(factoryRegistration);
        }
Пример #13
0
 private SingletonVariableDeclaration CreateFactoryVariableDeclaration(FactoryRegistration factoryRegistration)
 => new SingletonVariableDeclaration(factoryRegistration.Service.FullyQualifiedTypeName(), factoryRegistration.Service.Name);
Пример #14
0
 private FactoryClassDeclaration CreateFactoryClassDeclaration(FactoryRegistration factoryRegistration, DependencyGraph dependencyGraph)
 => new FactoryClassDeclaration(
     factoryRegistration.ImplimentationName,
     factoryRegistration.Service.FullyQualifiedTypeName(),
     factoryRegistration.ServiceMethods.Select(method => CreateServiceConstructorWithName(method.Name, method.ReturnType, dependencyGraph)).ToArray());
Пример #15
0
 string FormatRegistration(FactoryRegistration registration)
 {
     return($"Factory delegate");
 }
 internal FactoryRegistrationOptions(FactoryRegistration registration)
 {
     this.registration = registration;
 }