private Session CreateSession( Action <ScopedObjectRegistry> sessionObjectsConfigurator = null, params Type[] commandTypes) { var parser = new Parser(); var nameValidator = new NameValidator(); var descriptorGenerator = new CommandAttributeInspector(); var registry = new CommandRegistry(nameValidator, descriptorGenerator); foreach (var commandType in commandTypes) { registry.Register(commandType); } var services = new CommandServices(); var scopedObjects = new ScopedObjectRegistry(); scopedObjects.Register <VariableCollection>(); sessionObjectsConfigurator?.Invoke(scopedObjects); var factory = new CommandFactory(registry, services, scopedObjects); var variables = scopedObjects.Resolve(typeof(VariableCollection)) as VariableCollection; var replacer = new VariableReplacer(); var binder = new CommandParameterBinder(registry, replacer, variables); return(new Session(parser, factory, binder)); }
private CommandFactory CreateCommandFactory( Action <CommandRegistry> commandRegistryConfigurator = null, Action <CommandServices> commandServicesConfigurator = null, Action <ScopedObjectRegistry> scopedObjectRegistryConfigurator = null) { var nameValidator = new NameValidator(); var descriptorGenerator = new CommandAttributeInspector(); var registry = new CommandRegistry(nameValidator, descriptorGenerator); if (commandRegistryConfigurator != null) { commandRegistryConfigurator.Invoke(registry); } var services = new CommandServices(); if (commandServicesConfigurator != null) { commandServicesConfigurator.Invoke(services); } var sessionObjects = new ScopedObjectRegistry(); sessionObjects.Register <VariableCollection>(); if (scopedObjectRegistryConfigurator != null) { scopedObjectRegistryConfigurator.Invoke(sessionObjects); } return(new CommandFactory(registry, services, sessionObjects)); }
public void Register_TypeMissingParameterlessConstructor_ThrowsException() { // arrange var sut = new ScopedObjectRegistry(); Action sutAction = () => sut.Register <NoParameterlessConstructorObject>(); // act, assert var ex = Assert.Throws <InvalidOperationException>(sutAction); Assert.Contains("Type Chel.UnitTests.SampleObjects.NoParameterlessConstructorObject requires a parameterless constructor", ex.Message); }
public void Resolve_RegisteredType_ReturnsInstance() { // arrange var sut = new ScopedObjectRegistry(); sut.Register <GoodObject>(); // act var result = sut.Resolve(typeof(GoodObject)); // assert Assert.IsType <GoodObject>(result); }
public void Resolve_RegisteredType_ReturnsSameInstance() { // arrange var sut = new ScopedObjectRegistry(); sut.Register <GoodObject>(); // act var result1 = sut.Resolve(typeof(GoodObject)); var result2 = sut.Resolve(typeof(GoodObject)); // assert Assert.Same(result1, result2); }
public void CreateScope_WhenCalled_IncludesRegisteredTypes() { // arrange var sut = new ScopedObjectRegistry(); sut.Register <GoodObject>(); // act var scope = sut.CreateScope(); // assert var result = scope.Resolve(typeof(GoodObject)); Assert.IsType <GoodObject>(result); }
public void CreateScope_WhenCalled_ExcludesInstances() { // arrange var sut = new ScopedObjectRegistry(); sut.Register <GoodObject>(); var result1 = sut.Resolve(typeof(GoodObject)); // act var scope = sut.CreateScope(); // assert var result2 = scope.Resolve(typeof(GoodObject)); Assert.NotSame(result1, result2); }