public RegisteredObject(Type registeredType, Type concreteType, LifecycleType lifecycleType, RegisteredObjectLookup registry) { RegisteredType = registeredType; ConcreteType = concreteType; LifecycleType = lifecycleType; Registry = registry; }
public RegisteredObject(Type interfaceType, Type concreteType, LifecycleType lifecycleType, object instance = null) { _interfaceType = interfaceType; _concreteType = concreteType; _lifecycleType = lifecycleType; _instance = instance; }
public void Register <TImplementation>(LifecycleType lifecycle = LifecycleType.Transient) where TImplementation : class { _contracts.AddContract( new Contract(typeof(TImplementation), new CreateByType(typeof(TImplementation)), lifecycle)); }
public void Register <TContract>(Func <IResolve, TContract> func, LifecycleType lifecycle = LifecycleType.Transient) where TContract : class { _contracts.AddContract( new Contract(typeof(TContract), new CreateByFactory(func), lifecycle)); }
public void Register <I, C>(LifecycleType lifecycleType) { var interfaceType = typeof(I); var concreteType = typeof(C); ValidateRegisteredObject(interfaceType, concreteType); _container.Add(_registerObjectFactory.Build(interfaceType, concreteType, lifecycleType)); }
public ParticleInfo(ParticleSystem particleSystem, bool isSubemitter) { ParticleSystem = particleSystem; StartRotation = particleSystem.main.startRotationMultiplier; Renderer = particleSystem.GetComponent <ParticleSystemRenderer>(); SortingOrder = particleSystem.GetComponent <ParticlePlayerSortingOrder>(); DefaultSpeed = particleSystem.main.simulationSpeed; IsSubemitter = isSubemitter; LifeCycle = LifecycleType.None; }
public ScriptUnitAttribute(string name, LifecycleType lifecycleType = LifecycleType.Transient, AccessType accessType = AccessType.Local, Type clientInterface = null) { Name = name ?? string.Empty; LifecycleType = lifecycleType; AccessType = accessType; ClientInterface = clientInterface; }
public void ReturnsCorrectLifecycleManager(LifecycleType lifecycleType, Type typeOfLifecycleManager) { // arrange Func <A> objectFactory = () => new A(); // act var lifecycleManager = LifecycleManagerFactory.CreateLifecycleManager(lifecycleType, objectFactory); // assert Assert.IsType(typeOfLifecycleManager, lifecycleManager); }
public RegisteredObject Build(Type interfaceType, Type concreteType, LifecycleType lifecycleType) { if (lifecycleType == LifecycleType.Singleton) { return(new RegisteredObject(interfaceType, concreteType, lifecycleType, _objectResolver.BuildObject(concreteType))); } else { return(new RegisteredObject(interfaceType, concreteType, lifecycleType)); } }
/// <summary> /// Registers the type "Service" for the type "ServiceInterface". After this registration, when /// a Resolve call is made for the "ServiceInterface" type, an instance of the "Service" type will /// be returned. The "lifecycle" of the object (returned by the Resolve method) will be controlled /// by the "lifecycle" parameter passed to this method. /// </summary> /// <typeparam name="ServiceInterface">The type under which to register the "Service" type.</typeparam> /// <typeparam name="Service">The type to associate with the "ServiceInterface" type.</typeparam> /// <param name="lifecycle"> /// A value controlling the construction of instances of the "Service" type when /// resolved. /// </param> public void Register <ServiceInterface, Service>(LifecycleType lifecycle = LifecycleType.Transient) { var serviceInterfaceType = typeof(ServiceInterface); var serviceType = typeof(Service); if (!serviceInterfaceType.IsAssignableFrom(serviceType)) { throw new InvalidOperationException($"Cannot register the service type \"{typeof(Service).Name}\" because it does not implement the service interface \"{typeof(ServiceInterface).Name}\"."); } registeredTypes[serviceInterfaceType] = BaseTypeContext.CreateTypeContext(serviceType, lifecycle); }
/// <summary> /// Add or Update the container's object registry /// </summary> /// <typeparam name="TInterface">Objects's contract</typeparam> /// <typeparam name="TImplementation">Contracts implementation</typeparam> /// <param name="lifecycleType"><see cref="LifecycleType"/> of the object</param> public void AddOrUpdate <TInterface, TImplementation>(LifecycleType lifecycleType) where TImplementation : class, TInterface { var interfaceType = typeof(TInterface); var existingRegistration = _objectRegistry.FirstOrDefault(registration => registration.Interface == interfaceType); if (existingRegistration == null) { _objectRegistry.Add(new RegisteredObject(typeof(TInterface), typeof(TImplementation), lifecycleType)); } else { existingRegistration.Update(typeof(TImplementation), lifecycleType); } }
public static BaseTypeContext CreateTypeContext(Type registeredType, LifecycleType lifecycle) { switch (lifecycle) { case LifecycleType.Transient: return(new TransientTypeContext(registeredType)); case LifecycleType.Singleton: return(new SingletonTypeContext(registeredType)); default: throw new InvalidOperationException("Unexpected lifecycle type: " + lifecycle.ToString()); } }
/// <summary> /// Add the container's object registry /// </summary> /// <typeparam name="TInterface">Objects's contract</typeparam> /// <typeparam name="TConcrete">Contracts implementation</typeparam> /// <param name="lifecycle"><see cref="LifecycleType"/> of the object</param> /// <exception cref="ArgumentException"> if type of TInterface already exists in the object registry</exception> public void Add <TInterface, TConcrete>(LifecycleType lifecycle) where TConcrete : class, TInterface { var interfaceType = typeof(TInterface); var existingRegistration = _objectRegistry.FirstOrDefault(registration => registration.InterfaceType == interfaceType); if (existingRegistration == null) { _objectRegistry.Add(new RegisteredObject(typeof(TInterface), typeof(TConcrete), lifecycle)); } else { throw new ArgumentException($"The type {interfaceType.FullName} has been registered already!"); } }
/// <summary> /// Creates a factory instance that can create instaces of the given /// <paramref name="serviceType"/> using the <paramref name="implementingType"/> /// as the implementation. /// </summary> /// <param name="serviceType">The service being implemented.</param> /// <param name="implementingType">The actual type that will implement the service.</param> /// <param name="lifecycle">The <see cref="LifecycleType"/> that determines the lifetime of each instance being created.</param> /// <returns>A valid <see cref="IFactory"/> instance.</returns> public IFactory CreateFactory(Type serviceType, Type implementingType, LifecycleType lifecycle) { // Determine the factory type Type factoryTypeDefinition = _factoryTypes[lifecycle]; Type actualType = GetActualType(serviceType, implementingType); if (!serviceType.ContainsGenericParameters && !actualType.ContainsGenericParameters) { Type factoryType = factoryTypeDefinition.MakeGenericType(serviceType); return(CreateFactory(serviceType, actualType, factoryType)); } Func <IFactoryRequest, object> factoryMethod = request => { string serviceName = request.ServiceName; Type type = request.ServiceType; IServiceContainer currentContainer = request.Container; object[] arguments = request.Arguments; // Determine the implementing type Type concreteType = GetActualType(type, implementingType); // The concrete type cannot be null if (concreteType == null) { return(null); } // Generate the concrete factory instance // at runtime Type factoryType = factoryTypeDefinition.MakeGenericType(type); IFactory factory = CreateFactory(type, concreteType, factoryType); var factoryRequest = new FactoryRequest { ServiceType = serviceType, ServiceName = serviceName, Arguments = arguments, Container = currentContainer }; return(factory.CreateInstance(factoryRequest)); }; return(new FunctorFactory(factoryMethod)); }
public void Register <TResolvable, TImplementation>(LifecycleType lifecycleType = LifecycleType.Transient) where TResolvable : class where TImplementation : TResolvable { var resolvableType = typeof(TResolvable); var implementationType = typeof(TImplementation); Func <TResolvable> objectFactory = () => (TResolvable)this.Resolve(resolvableType); // This static dependency is most useful in that I can now test the creation of the lifecycle managers separately. // An argument could be made that this is both more and less SOLID: it helps adhere to SRP, but // it violates dependency inversion (which is a bit ironic given what I'm writing). ILifecycleManager lifecycleManager = LifecycleManagerFactory.CreateLifecycleManager(lifecycleType, objectFactory); RegisterTypes(resolvableType, implementationType, lifecycleManager); }
/// <summary> /// Convert the LifecycleType to a <see cref="ILifecycle"/> /// </summary> /// <param name="lifecycleType">The type of the lifecycle to get</param> /// <returns>The <see cref="ILifecycle"/> instance </returns> public static ILifecycle GetByType(LifecycleType lifecycleType) { switch (lifecycleType) { case LifecycleType.Singleton: return(Lifecycle.Singleton); case LifecycleType.Transient: return(Lifecycle.Transient); default: case LifecycleType.Default: return(null); } }
public void AddObject(Type resolveType, Type concreteType, LifecycleType lifecycleType) { if (!resolveType.IsAssignableFrom(concreteType)) { throw new InvalidCastException($"Type {resolveType.Name} is not assignable from {concreteType.Name}"); } if (lifecycleType == LifecycleType.Transient) { RegisteredObjects.Add(resolveType, new TransientRegisteredObject(resolveType, concreteType, lifecycleType, this)); } else if (lifecycleType == LifecycleType.Singleton) { RegisteredObjects.Add(resolveType, new SingletonRegisteredObject(resolveType, concreteType, lifecycleType, this)); } }
/// <summary> /// Converts a given <see cref="System.Type"/> into /// a set of <see cref="Action{IServiceContainer}"/> instances so that /// the <see cref="IContainer"/> instance can be loaded /// with the given factories. /// </summary> /// <param name="sourceType">The input type from which one or more factories will be created.</param> /// <returns>A set of <see cref="Action{IServiceContainer}"/> instances. This cannot be null.</returns> /// public IEnumerable <Action <IServiceContainer> > Load(Type sourceType) { // Extract the Implements attribute from the source type ICustomAttributeProvider provider = sourceType; object[] attributes = provider.GetCustomAttributes(typeof(ImplementsAttribute), false); List <ImplementsAttribute> attributeList = attributes.Cast <ImplementsAttribute>().ToList(); var results = new List <Action <IServiceContainer> >(); IFactory singletonFactory = null; foreach (ImplementsAttribute attribute in attributeList) { string serviceName = attribute.ServiceName; Type serviceType = attribute.ServiceType; LifecycleType lifeCycle = attribute.LifecycleType; IFactory currentFactory = CreateFactory(serviceType, sourceType, lifeCycle); if (currentFactory == null) { continue; } // If this type is implemented as a factory singleton, // it only needs to be implemented once if (lifeCycle == LifecycleType.Singleton) { if (singletonFactory == null) { // Initialize the singleton instance only once singletonFactory = currentFactory; } else { // Make sure that the same singleton factory instance // is assigned to every single point // where it is marked as a singleton currentFactory = singletonFactory; } } results.Add(container => container.AddFactory(serviceName, serviceType, currentFactory)); } return(results); }
public void RegisterImplementation <TInterface, TImplementation>(LifecycleType objectLifecycleType = LifecycleType.Transient) where TImplementation : TInterface { var interfaceType = typeof(TInterface); var existingRegistration = registrations.FirstOrDefault(registration => registration.Interface == interfaceType); if (existingRegistration != null) { existingRegistration.Implementation = typeof(TImplementation); existingRegistration.ObjectLifecycle = objectLifecycleType; } else { registrations.Add(new RegistrationInfoModel { Implementation = typeof(TImplementation), Interface = typeof(TInterface), ObjectLifecycle = objectLifecycleType }); } }
public static ILifecycleManager <T> CreateLifecycleManager <T>(LifecycleType lifecycleType, Func <T> objectFactory) where T : class { ILifecycleManager <T> lifecycleManager = null; switch (lifecycleType) { case LifecycleType.Transient: lifecycleManager = new TransientLifecycleManager <T>(objectFactory); break; case LifecycleType.Singleton: lifecycleManager = new SingletonLifecycleManager <T>(objectFactory); break; default: throw new NotImplementedException($"Lifecycle type {lifecycleType} has not been implemented."); } return(lifecycleManager); }
/// <summary> /// Allows users to add services to a container using a /// given <paramref name="serviceType">service type</paramref> and /// <paramref name="lifeCycleType">lifecycle type</paramref>. /// </summary> /// <param name="serviceType">The <see cref="System.Type" /> of service to implement.</param> /// <param name="lifeCycleType">The instancing behavior to use with this implementation.</param> public ImplementsAttribute(Type serviceType, LifecycleType lifeCycleType) { ServiceType = serviceType; LifecycleType = lifeCycleType; }
/// <summary> /// Allows users to add services to a container using a /// given <paramref name="serviceType">service type</paramref> and /// <paramref name="lifeCycleType">lifecycle type</paramref>. /// </summary> /// <param name="serviceType">The <see cref="System.Type"/> of service to implement.</param> /// <param name="lifeCycleType">The instancing behavior to use with this implementation.</param> public ImplementsAttribute(Type serviceType, LifecycleType lifeCycleType) { _serviceType = serviceType; _lifeCycleType = lifeCycleType; }
/// <summary> /// Registers a service. /// </summary> /// <typeparam name="TService">The type, base type or interface of the service.</typeparam> /// <typeparam name="TImplementation">The concrete implementation of the <typeparamref name="TService"/>.</typeparam> /// <param name="registrator">The registrator.</param> /// <param name="lifecycle">The lifecycle used in the creating/locating the service.</param> /// <param name="behavior">The behavior to take when there is a duplicate registration.</param> public static void Register <TService, TImplementation>(this IRegistrator registrator, LifecycleType lifecycleType, RegistrationConflictBehavior behavior) { registrator.Register(typeof(TService), typeof(TImplementation), Lifecycle.GetByType(lifecycleType), behavior); }
/// <summary> /// Registers a service. /// </summary> /// <typeparam name="TService">The type, base type or interface of the service.</typeparam> /// <typeparam name="TImplementation">The concrete implementation of the <typeparamref name="TService"/>.</typeparam> /// <param name="registrator">The registrator.</param> /// <param name="lifecycle">The lifecycle used in the creating/locating the service.</param> public static void Register <TService, TImplementation>(this IRegistrator registrator, LifecycleType lifecycleType) { registrator.Register <TService, TImplementation>(lifecycleType, RegistrationConflictBehavior.Default); }
/// <summary> /// Implementation to register an object with the <see cref="InfuseContainer"/> /// </summary> /// <typeparam name="TInterface">Contract to TConcrete</typeparam> /// <typeparam name="TConcrete">Class confirming to the TInterface contract</typeparam> /// <param name="lifecycle"><see cref="LifecycleType"/> type for the object</param> public void Register <TInterface, TConcrete>(LifecycleType lifecycle) where TConcrete : class, TInterface { _registrar.Add <TInterface, TConcrete>(lifecycle); }
public RegisteredObjectInfo(Type registeredType, Type concreteType, LifecycleType lifecycleType) { RegisteredType = registeredType; ConcreteType = concreteType; LifecycleType = lifecycleType; }
public Contract(Type contractType, IContractActivator executer, LifecycleType lifecycle) { ContractType = contractType; Lifecycle = lifecycle; Executer = executer; }
public void Register(Type selfType, LifecycleType lifecycle = LifecycleType.Transient) { _contracts.AddContract( new Contract(selfType, new CreateByType(selfType), lifecycle)); }
private void AddService(Type serviceType, Type implementationType, LifecycleType lifecycle) { this.container.AddService(serviceType, implementationType, lifecycle); }
public void Register <I, C>(LifecycleType lifecycleType) { _containerRegistry.Register <I, C>(lifecycleType); }
/// <summary> /// Implementation to register an object with the <see cref="InfuseContainer"/> /// </summary> /// <typeparam name="TInterface">Contract to TImplementation</typeparam> /// <typeparam name="TImplementation">Class confirming to the TInterface contract</typeparam> /// <param name="lifecycleType"><see cref="LifecycleType"/> type for the object</param> public void Register <TInterface, TImplementation>(LifecycleType lifecycleType = LifecycleType.Transient) where TImplementation : class, TInterface { _registrar.AddOrUpdate <TInterface, TImplementation>(lifecycleType); }
private void AddService(Type serviceType, Type implementationType, LifecycleType lifecycle) { this.container.AddService(serviceType, implementationType, lifecycle); }
/// <summary> /// Creates a factory instance that can create instaces of the given /// <paramref name="serviceType"/> using the <paramref name="implementingType"/> /// as the implementation. /// </summary> /// <param name="serviceType">The service being implemented.</param> /// <param name="implementingType">The actual type that will implement the service.</param> /// <param name="lifecycle">The <see cref="LifecycleType"/> that determines the lifetime of each instance being created.</param> /// <returns>A valid <see cref="IFactory"/> instance.</returns> private static IFactory CreateFactory(Type serviceType, Type implementingType, LifecycleType lifecycle) { // HACK: Use a lazy factory since the actualy IFactoryBuilder instance won't // be available until runtime Func<IFactoryRequest, object> factoryMethod = request => { IServiceContainer currentContainer = request.Container; object[] arguments = request.Arguments; var builder = currentContainer.GetService<IFactoryBuilder>(); // HACK: If the service type is a type definition and // the implementing type is a type definition, // assume that the service type has the same number of // generic arguments as the implementing type Type actualServiceType = serviceType; Type actualImplementingType = implementingType; if (serviceType.IsGenericTypeDefinition && implementingType.IsGenericTypeDefinition && serviceType.GetGenericArguments().Count() == implementingType.GetGenericArguments().Count()) { Type[] typeArguments = request.ServiceType.GetGenericArguments(); actualServiceType = serviceType.MakeGenericType(typeArguments); actualImplementingType = implementingType.MakeGenericType(typeArguments); } IFactory actualFactory = builder.CreateFactory(actualServiceType, actualImplementingType, lifecycle); var factoryRequest = new FactoryRequest { ServiceType = serviceType, ServiceName = request.ServiceName, Arguments = arguments, Container = currentContainer }; return actualFactory.CreateInstance(factoryRequest); }; return new FunctorFactory(factoryMethod); }