public void ReplaceType_WithValidArgs_DoesNotThrow() { var container = new ServiceContainer(); container.Add(typeof(string), typeof(string), Lifetime.Singleton); Assert.DoesNotThrow(() => container.Add(typeof(string), _factory => string.Empty, Lifetime.Singleton)); }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { engineComponents.Add(new DisplayUtilities(this)); EngineComponents.Add(renderer2d = new Renderer2d(this)); EngineComponents.Add(InputManager = new InputManager(this)); engineComponents.Add(new AudioManager(this)); // [FOREACH PERFORMANCE] Should not allocate garbage EngineComponents.AllServices.ForEach(a => a.Initialize()); loadingScene = new LoadingScene(this); loadingScene.Initialize(); base.Initialize(); }
public void AddNewFactory_WithNullFactory_ThrowsArgumentNullException() { var container = new ServiceContainer(); Func <IServiceFactory, object> nullFactory = null; Assert.Throws <ArgumentNullException>(() => container.Add(typeof(string), nullFactory, Lifetime.Singleton)); }
public void AddNewType_WithNullInstanceType_ThrowsArgumentNullException() { var container = new ServiceContainer(); Type nullType = null; Assert.Throws <ArgumentNullException>(() => container.Add(typeof(string), nullType, Lifetime.Singleton)); }
protected void AddService <TModel, TService, TRepository>() where TModel : class where TService : class, IService <TModel>, new() where TRepository : class, IRepository <TModel>, new() { Type key = typeof(TService); if (!ServiceContainer.ContainsKey(key)) { Lazy <IService <TModel> > obj; obj = new Lazy <IService <TModel> >(valueFactory: () => { IService <TModel> x = new TService(); x.Root = this; if (x.RequiresOwnDbContext) { x.Context = this.NewDbContext(); } else { x.Context = this.Context; // shared } x.Repository = new TRepository(); x.Repository.Context = x.Context; return(x); }); ServiceContainer.Add(key: key, value: obj); } }
/// <summary> /// Gets the service of the specified type from this scene, or creates one /// if one does not already exist. /// </summary> /// <param name="serviceType">Type of the service.</param> /// <returns></returns> public IService GetService(Type serviceType) { IService service = null; if (services.TryGet(serviceType, out service)) { return(service); } if (!typeof(IService).IsAssignableFrom(serviceType)) { throw new ArgumentException("serviceType is not an IService."); } service = Kernel.Get(serviceType) as IService; services.Add(service); service.Initialise(this); return(service); }
public void AddNewFactory_WithNullServiceType_ThrowsArgumentNullException() { var container = new ServiceContainer(); Assert.Throws <ArgumentNullException>(() => container.Add(null, _factory => null, Lifetime.Singleton)); }
public void AddNewType_WithValidArgs_DoesNotThrow() { var container = new ServiceContainer(); Assert.DoesNotThrow(() => container.Add(typeof(string), typeof(string), Lifetime.Singleton)); }
public void AddNewType_WithInvalidInheritance_ThrowsArgumentException() { var container = new ServiceContainer(); Assert.Throws <ArgumentException>(() => container.Add(typeof(string), typeof(int), Lifetime.Singleton)); }
/// <summary> /// Adds a component to this entity /// </summary> /// <param name="component"></param> public T AddComponent <T>(T component) where T : Component { component.SetEntity(this); components.Add(component); return(component); }