using System;
using UnityEngine;
namespace Framework.Services {
/// Configures the service graph at runtime
public class RuntimeServiceModel {
#if false
#region interface IScopingContext
/// Allows a service's scope to be specified
public interface IScopingContext {
/// Keeps the service alive until the game terminates
void InGlobalScope();
/// Destroys the service when the player quits the gameplay session
void InSessionScope();
/// Service will only keep alive until another scene is loaded
void InSceneScope();
/// Each consumer will get a different copy of the service
void InTransientScope();
}
#endregion // interface IScopingContext
#region class BindingScopingContext
/// Scoping context that modifies a service binding
private class BindingScopingContext : IScopingContext {
/// Initializes a new scoping context for service bindings
///
public BindingScopingContext(ServiceInjector.IBinding binding) {
this.binding = binding;
}
/// Keeps the service alive until the game terminates
public void InGlobalScope() {
this.binding.ChangeScope(Scope.Global);
}
/// Destroys the service when the player quits the gameplay session
public void InSessionScope() {
this.binding.ChangeScope(Scope.Session);
}
/// Service will only keep alive until another scene is loaded
public void InSceneScope() {
this.binding.ChangeScope(Scope.Scene);
}
/// Each consumer will get a different copy of the service
public void InTransientScope() {
throw new NotImplementedException();
}
}
#endregion // class BindingScopingContext
#region interface IBindingContext
/// Allows the implementation of a service to be specified
/// Interface to which an implementation will be bound
public interface IBindingContext where TInterface : class {
/// Binds the interface to the specified implementation class
///
/// Implementation class to which the interface will be bound
///
/// A context by which the service's scope can be modified
IScopingContext To() where TImplementation : MonoBehaviour, TInterface;
// Problem: We don't know how and where the existing instance was created
// and whether its service lifetime can be modified.
// Solution? Always assume scene lifetime?
#if false
/// Binds the interface to the specified factory method
/// Method that will be called to create the component
/// A context by which the service's scope can be modified
IScopingContext ToMethod(Func factoryMethod);
/// Binds the interface to the specified existing instance
/// Instance to which the interface will be bound
/// A context by which the service's scope can be modified
IScopingContext ToConstant(TInterface instance);
#endif
}
#endregion // interface IBindingContext
#region class ServiceInjectorBindingContext
/// Sets up bindings in a service injector
///
private class ServiceInjectorBindingContext : IBindingContext
where TInterface : class {
/// Binds the interface to the specified implementation class
///
/// Implementation class to which the interface will be bound
///
/// A context by which the service's scope can be modified
public IScopingContext To()
where TImplementation : MonoBehaviour, TInterface {
Type interfaceType = typeof(TInterface);
Scope scope = ServiceInjector.GetScope(interfaceType).GetValueOrDefault(Scope.Scene);
ServiceInjector.IBinding = ServiceInjector.AddBinding(
typeof(TInterface), typeof(TImplementation)
);
throw new NotImplementedException();
}
}
#endregion // class ServiceInjectorBindingContext
/// Lets you set up a binding for the specified interface
/// Interface for which a binding will be set up
///
/// A context through which the binding for the specified interface can be set up
///
public static IBindingContext Bind()
where TInterface : class {
return new ServiceInjectorBindingContext();
}
#endif
}
} // namespace Framework.Services