public void ReturnMediatorFromNestedContainer() { var nestedContainer = MockRepository.GenerateStrictMock<IContainer>(); nestedContainer.Expect(c => c.Configure(Arg<Action<ConfigurationExpression>>.Is.NotNull)) .Repeat.Once(); var expected = MockRepository.GenerateStrictMock<IMediator>(); nestedContainer.Expect(c => c.GetInstance<IMediator>()) .Return(expected) .Repeat.Once(); nestedContainer.Expect(c => c.Dispose()) .Repeat.Once(); var container = MockRepository.GenerateStrictMock<IContainer>(); container.Expect(c => c.GetNestedContainer()) .Return(nestedContainer) .Repeat.Once(); IMediator actual; using (var context = new ContainerContext(container)) actual = context.Mediator; nestedContainer.VerifyAllExpectations(); actual.Should().Be.SameInstanceAs(expected); }
/// <summary> /// Execute extension's job in given context /// </summary> /// <param name="context"> </param> public void Process(ContainerContext context) { var appbound = context.Object as IApplicationBound; if (null == appbound) { return; } if (ContainerOperation.AfterCreate == context.Operation) { appbound.SetApplication(Application); } }
public void CreateANestedContainerWhenConstructed() { var nestedContainer = MockRepository.GenerateStrictMock<IContainer>(); nestedContainer.Expect(c => c.Configure(Arg<Action<ConfigurationExpression>>.Is.NotNull)) .Repeat.Once(); var container = MockRepository.GenerateStrictMock<IContainer>(); container.Expect(c => c.GetNestedContainer()) .Return(nestedContainer) .Repeat.Once(); var context = new ContainerContext(container); container.VerifyAllExpectations(); }
private void PopContext() { _stack.RemoveAt(_stack.Count - 1); if (_stack.Count == 0) _currentContext = null; else _currentContext = _stack[_stack.Count - 1]; }
private bool ReadNormal() { switch (CurrentState) { case State.Start: { JsonToken token = (!_readRootValueAsArray) ? JsonToken.StartObject : JsonToken.StartArray; BsonType type = (!_readRootValueAsArray) ? BsonType.Object : BsonType.Array; SetToken(token); ContainerContext newContext = new ContainerContext(type); PushContext(newContext); newContext.Length = ReadInt32(); return true; } case State.Complete: case State.Closed: return false; case State.Property: { ReadType(_currentElementType); return true; } case State.ObjectStart: case State.ArrayStart: case State.PostValue: ContainerContext context = _currentContext; if (context == null) return false; int lengthMinusEnd = context.Length - 1; if (context.Position < lengthMinusEnd) { if (context.Type == BsonType.Array) { ReadElement(); ReadType(_currentElementType); return true; } else { SetToken(JsonToken.PropertyName, ReadElement()); return true; } } else if (context.Position == lengthMinusEnd) { if (ReadByte() != 0) throw JsonReaderException.Create(this, "Unexpected end of object byte value."); PopContext(); if (_currentContext != null) MovePosition(context.Length); JsonToken endToken = (context.Type == BsonType.Object) ? JsonToken.EndObject : JsonToken.EndArray; SetToken(endToken); return true; } else { throw JsonReaderException.Create(this, "Read past end of current container context."); } case State.ConstructorStart: break; case State.Constructor: break; case State.Error: break; case State.Finished: break; default: throw new ArgumentOutOfRangeException(); } return false; }
private bool ReadCodeWScope() { switch (_bsonReaderState) { case BsonReaderState.CodeWScopeStart: SetToken(JsonToken.PropertyName, "$code"); _bsonReaderState = BsonReaderState.CodeWScopeCode; return true; case BsonReaderState.CodeWScopeCode: // total CodeWScope size - not used ReadInt32(); SetToken(JsonToken.String, ReadLengthString()); _bsonReaderState = BsonReaderState.CodeWScopeScope; return true; case BsonReaderState.CodeWScopeScope: if (CurrentState == State.PostValue) { SetToken(JsonToken.PropertyName, "$scope"); return true; } else { SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeScopeObject; ContainerContext newContext = new ContainerContext(BsonType.Object); PushContext(newContext); newContext.Length = ReadInt32(); return true; } case BsonReaderState.CodeWScopeScopeObject: bool result = ReadNormal(); if (result && TokenType == JsonToken.EndObject) _bsonReaderState = BsonReaderState.CodeWScopeScopeEnd; return result; case BsonReaderState.CodeWScopeScopeEnd: SetToken(JsonToken.EndObject); _bsonReaderState = BsonReaderState.Normal; return true; default: throw new ArgumentOutOfRangeException(); } }
private void PopContext() { _stack.RemoveAt(_stack.Count - 1); _currentContext = _stack.Count == 0 ? null : _stack[_stack.Count - 1]; }
public void Process(ContainerContext context) { if (context.Component.ServiceType == typeof (ILogger)) { if (context.Component.Lifestyle == Lifestyle.Extension && null != context.Component.Implementation) { _manager.Loggers.Add((ILogger) context.Component.Implementation); } else if (!string.IsNullOrEmpty(context.Component.Name)) { _manager.Loggers.Add(Container.Get<ILogger>(context.Component.Name)); } } }
/// <summary> /// Create a default <see cref="UnityContainer"/>. /// </summary> public UnityContainer(ModeFlags mode = ModeFlags.Optimized) { if (!mode.IsValid()) { throw new ArgumentException("'Activated' and 'Diagnostic' flags are mutually exclusive."); } ///////////////////////////////////////////////////////////// // Initialize Root _root = this; ExecutionMode = mode; LifetimeContainer = new LifetimeContainer(this); Register = AddOrReplace; //Built-In Registrations // Defaults Defaults = new DefaultPolicies(this); // IUnityContainer, IUnityContainerAsync var container = new ExplicitRegistration(this, null, typeof(UnityContainer), new ContainerLifetimeManager()) { Pipeline = (ref BuilderContext c) => c.Container }; Debug.Assert(null != container.LifetimeManager); // Create Registries _metadata = new Metadata(); _registry = new Registry(Defaults); _registry.Set(typeof(IUnityContainer), null, container); // TODO: Remove redundancy _registry.Set(typeof(IUnityContainerAsync), null, container); _registry.Set(typeof(IUnityContainer), null, container.LifetimeManager.Pipeline); _registry.Set(typeof(IUnityContainerAsync), null, container.LifetimeManager.Pipeline); ///////////////////////////////////////////////////////////// // Built-In Features var func = new PolicySet(this); _registry.Set(typeof(Func <>), func); func.Set(typeof(LifetimeManager), new PerResolveLifetimeManager()); func.Set(typeof(TypeFactoryDelegate), FuncResolver.Factory); // Func<> Factory _registry.Set(typeof(Lazy <>), new PolicySet(this, typeof(TypeFactoryDelegate), LazyResolver.Factory)); // Lazy<> _registry.Set(typeof(IEnumerable <>), new PolicySet(this, typeof(TypeFactoryDelegate), EnumerableResolver.Factory)); // Enumerable _registry.Set(typeof(IRegex <>), new PolicySet(this, typeof(TypeFactoryDelegate), RegExResolver.Factory)); // Regular Expression Enumerable ///////////////////////////////////////////////////////////// // Pipelines var factory = new FactoryPipeline(); var lifetime = new LifetimePipeline(); // Mode of operation if (ExecutionMode.IsOptimized()) { ///////////////////////////////////////////////////////////// // Setup Optimized mode // Create Context Context = new ContainerContext(this, new StagedStrategyChain <Pipeline, Stage> // Type Build Pipeline { { lifetime, Stage.Lifetime }, { factory, Stage.Factory }, { new MappingPipeline(), Stage.TypeMapping }, { new ConstructorPipeline(this), Stage.Creation }, { new FieldPipeline(this), Stage.Fields }, { new PropertyPipeline(this), Stage.Properties }, { new MethodPipeline(this), Stage.Methods } }, new StagedStrategyChain <Pipeline, Stage> // Factory Resolve Pipeline { { lifetime, Stage.Lifetime }, { factory, Stage.Factory } }, new StagedStrategyChain <Pipeline, Stage> // Instance Resolve Pipeline { { factory, Stage.Factory } }); } else { ///////////////////////////////////////////////////////////// // Setup Diagnostic mode var diagnostic = new DiagnosticPipeline(); // Create Context Context = new ContainerContext(this, new StagedStrategyChain <Pipeline, Stage> // Type Build Pipeline { { diagnostic, Stage.Diagnostic }, { factory, Stage.Factory }, { new MappingDiagnostic(), Stage.TypeMapping }, { new ConstructorDiagnostic(this), Stage.Creation }, { new FieldDiagnostic(this), Stage.Fields }, { new PropertyDiagnostic(this), Stage.Properties }, { new MethodDiagnostic(this), Stage.Methods } }, new StagedStrategyChain <Pipeline, Stage> // Factory Resolve Pipeline { { diagnostic, Stage.Diagnostic }, { factory, Stage.Factory } }, new StagedStrategyChain <Pipeline, Stage> // Instance Resolve Pipeline { { diagnostic, Stage.Diagnostic }, { factory, Stage.Factory } }); // Build process DependencyResolvePipeline = ValidatingDependencyResolvePipeline; // Validators ValidateType = DiagnosticValidateType; ValidateTypes = DiagnosticValidateTypes; CreateErrorMessage = CreateDiagnosticMessage; } }
private void PushContext(ContainerContext newContext) { _stack.Add(newContext); _currentContext = newContext; }
private bool ReadNormal() { switch (CurrentState) { case State.Start: { JsonToken token = (!_readRootValueAsArray) ? JsonToken.StartObject : JsonToken.StartArray; BsonType type = (!_readRootValueAsArray) ? BsonType.Object : BsonType.Array; SetToken(token); ContainerContext newContext = new ContainerContext(type); PushContext(newContext); newContext.Length = ReadInt32(); return(true); } case State.Complete: case State.Closed: return(false); case State.Property: { ReadType(_currentElementType); return(true); } case State.ObjectStart: case State.ArrayStart: case State.PostValue: ContainerContext context = _currentContext; if (context == null) { return(false); } int lengthMinusEnd = context.Length - 1; if (context.Position < lengthMinusEnd) { if (context.Type == BsonType.Array) { ReadElement(); ReadType(_currentElementType); return(true); } else { SetToken(JsonToken.PropertyName, ReadElement()); return(true); } } else if (context.Position == lengthMinusEnd) { if (ReadByte() != 0) { throw JsonReaderException.Create(this, "Unexpected end of object byte value."); } PopContext(); if (_currentContext != null) { MovePosition(context.Length); } JsonToken endToken = (context.Type == BsonType.Object) ? JsonToken.EndObject : JsonToken.EndArray; SetToken(endToken); return(true); } else { throw JsonReaderException.Create(this, "Read past end of current container context."); } case State.ConstructorStart: break; case State.Constructor: break; case State.Error: break; case State.Finished: break; default: throw new ArgumentOutOfRangeException(); } return(false); }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: double d = ReadDouble(); if (_floatParseHandling == FloatParseHandling.Decimal) { SetToken(JsonToken.Float, Convert.ToDecimal(d, CultureInfo.InvariantCulture)); } else { SetToken(JsonToken.Float, d); } break; case BsonType.String: case BsonType.Symbol: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.Object: { SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(BsonType.Object); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Array: { SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(BsonType.Array); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Binary: BsonBinaryType binaryType; byte[] data = ReadBinary(out binaryType); object value = (binaryType != BsonBinaryType.Uuid) ? data : (object)new Guid(data); SetToken(JsonToken.Bytes, value); break; case BsonType.Undefined: SetToken(JsonToken.Undefined); break; case BsonType.Oid: byte[] oid = ReadBytes(12); SetToken(JsonToken.Bytes, oid); break; case BsonType.Boolean: bool b = Convert.ToBoolean(ReadByte()); SetToken(JsonToken.Boolean, b); break; case BsonType.Date: long ticks = ReadInt64(); DateTime utcDateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); DateTime dateTime; switch (DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime = utcDateTime.ToLocalTime(); break; default: dateTime = utcDateTime; break; } SetToken(JsonToken.Date, dateTime); break; case BsonType.Null: SetToken(JsonToken.Null); break; case BsonType.Regex: string expression = ReadString(); string modifiers = ReadString(); string regex = @"/" + expression + @"/" + modifiers; SetToken(JsonToken.String, regex); break; case BsonType.Reference: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.ReferenceStart; break; case BsonType.Code: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.CodeWScope: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: SetToken(JsonToken.Integer, (long)ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: SetToken(JsonToken.Integer, ReadInt64()); break; default: throw new ArgumentOutOfRangeException(nameof(type), "Unexpected BsonType value: " + type); } }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: SetToken(JsonToken.Float, ReadDouble()); break; case BsonType.String: case BsonType.Symbol: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.Object: { SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(BsonType.Object); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Array: { SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(BsonType.Array); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Binary: SetToken(JsonToken.Bytes, ReadBinary()); break; case BsonType.Undefined: SetToken(JsonToken.Undefined); break; case BsonType.Oid: byte[] oid = ReadBytes(12); SetToken(JsonToken.Bytes, oid); break; case BsonType.Boolean: bool b = Convert.ToBoolean(ReadByte()); SetToken(JsonToken.Boolean, b); break; case BsonType.Date: long ticks = ReadInt64(); DateTime utcDateTime = JsonConvert.ConvertJavaScriptTicksToDateTime(ticks); DateTime dateTime; switch (DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(utcDateTime.ToLocalTime(), DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime = utcDateTime.ToLocalTime(); break; default: dateTime = utcDateTime; break; } SetToken(JsonToken.Date, dateTime); break; case BsonType.Null: SetToken(JsonToken.Null); break; case BsonType.Regex: string expression = ReadString(); string modifiers = ReadString(); string regex = @"/" + expression + @"/" + modifiers; SetToken(JsonToken.String, regex); break; case BsonType.Reference: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.ReferenceStart; break; case BsonType.Code: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.CodeWScope: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: SetToken(JsonToken.Integer, (long)ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: SetToken(JsonToken.Integer, ReadInt64()); break; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: SetToken(JsonToken.Float, ReadDouble()); break; case BsonType.String: case BsonType.Symbol: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.Object: { SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(JTokenType.Object); _stack.Add(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Array: { SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(JTokenType.Array); _stack.Add(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Binary: SetToken(JsonToken.Bytes, ReadBinary()); break; case BsonType.Undefined: SetToken(JsonToken.Undefined); break; case BsonType.Oid: byte[] oid = ReadBytes(12); SetToken(JsonToken.Bytes, oid); break; case BsonType.Boolean: bool b = Convert.ToBoolean(ReadByte()); SetToken(JsonToken.Boolean, b); break; case BsonType.Date: long ticks = ReadInt64(); DateTime dateTime = JsonConvert.ConvertJavaScriptTicksToDateTime(ticks); SetToken(JsonToken.Date, dateTime); break; case BsonType.Null: SetToken(JsonToken.Null); break; case BsonType.Regex: string expression = ReadString(); string modifiers = ReadString(); string regex = @"/" + expression + @"/" + modifiers; SetToken(JsonToken.String, regex); break; case BsonType.Reference: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.ReferenceStart; break; case BsonType.Code: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.CodeWScope: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: SetToken(JsonToken.Integer, (long)ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: SetToken(JsonToken.Integer, ReadInt64()); break; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } }
private void ReadType(BsonType type) { switch (type) { case BsonType.Number: double d = ReadDouble(); if (_floatParseHandling == FloatParseHandling.Decimal) SetToken(JsonToken.Float, Convert.ToDecimal(d, CultureInfo.InvariantCulture)); else SetToken(JsonToken.Float, d); break; case BsonType.String: case BsonType.Symbol: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.Object: { SetToken(JsonToken.StartObject); ContainerContext newContext = new ContainerContext(BsonType.Object); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Array: { SetToken(JsonToken.StartArray); ContainerContext newContext = new ContainerContext(BsonType.Array); PushContext(newContext); newContext.Length = ReadInt32(); break; } case BsonType.Binary: SetToken(JsonToken.Bytes, ReadBinary()); break; case BsonType.Undefined: SetToken(JsonToken.Undefined); break; case BsonType.Oid: byte[] oid = ReadBytes(12); SetToken(JsonToken.Bytes, oid); break; case BsonType.Boolean: bool b = Convert.ToBoolean(ReadByte()); SetToken(JsonToken.Boolean, b); break; case BsonType.Date: long ticks = ReadInt64(); DateTime utcDateTime = DateTimeUtils.ConvertJavaScriptTicksToDateTime(ticks); DateTime dateTime; switch (DateTimeKindHandling) { case DateTimeKind.Unspecified: dateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Unspecified); break; case DateTimeKind.Local: dateTime = utcDateTime.ToLocalTime(); break; default: dateTime = utcDateTime; break; } SetToken(JsonToken.Date, dateTime); break; case BsonType.Null: SetToken(JsonToken.Null); break; case BsonType.Regex: string expression = ReadString(); string modifiers = ReadString(); string regex = @"/" + expression + @"/" + modifiers; SetToken(JsonToken.String, regex); break; case BsonType.Reference: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.ReferenceStart; break; case BsonType.Code: SetToken(JsonToken.String, ReadLengthString()); break; case BsonType.CodeWScope: SetToken(JsonToken.StartObject); _bsonReaderState = BsonReaderState.CodeWScopeStart; break; case BsonType.Integer: SetToken(JsonToken.Integer, (long)ReadInt32()); break; case BsonType.TimeStamp: case BsonType.Long: SetToken(JsonToken.Integer, ReadInt64()); break; default: throw new ArgumentOutOfRangeException("type", "Unexpected BsonType value: " + type); } }