Пример #1
0
    private static void TryMapToExistingRegistration(
        ITypeRegistrar typeRegistrar,
        ITypeInfo typeInfo,
        TypeContext context,
        string?scope)
    {
        ExtendedTypeReference?normalizedTypeRef = null;
        var resolved = false;

        foreach (TypeComponent component in typeInfo.Components)
        {
            normalizedTypeRef = TypeReference.Create(
                component.Type,
                context,
                scope);

            if (typeRegistrar.IsResolved(normalizedTypeRef))
            {
                resolved = true;
                break;
            }
        }

        if (!resolved && normalizedTypeRef is not null)
        {
            typeRegistrar.MarkUnresolved(normalizedTypeRef);
        }
    }
Пример #2
0
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (ClrTypeReference typeReference in typeReferences.OfType <ClrTypeReference>())
            {
                if (!BaseTypes.IsNonGenericBaseType(typeReference.Type) &&
                    _typeInspector.TryCreate(typeReference.Type, out TypeInfo typeInfo))
                {
                    Type type = typeInfo.ClrType;

                    if (IsTypeSystemObject(type))
                    {
                        ClrTypeReference namedTypeReference = typeReference.With(type);

                        if (!typeRegistrar.IsResolved(namedTypeReference))
                        {
                            typeRegistrar.Register(
                                typeRegistrar.CreateInstance(type),
                                typeReference.Scope,
                                BaseTypes.IsGenericBaseType(type));
                        }
                    }
                    else
                    {
                        TryMapToExistingRegistration(
                            typeRegistrar,
                            typeInfo,
                            typeReference.Context,
                            typeReference.Scope);
                    }
                }
            }
        }
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (ExtendedTypeReference typeReference in
                     typeReferences.OfType <ExtendedTypeReference>())
            {
                if (_typeInspector.TryCreateTypeInfo(typeReference.Type, out ITypeInfo? typeInfo) &&
                    !ExtendedType.Tools.IsNonGenericBaseType(typeInfo.NamedType))
                {
                    Type namedType = typeInfo.NamedType;
                    if (IsTypeSystemObject(namedType))
                    {
                        IExtendedType         extendedType       = _typeInspector.GetType(namedType);
                        ExtendedTypeReference namedTypeReference = typeReference.With(extendedType);

                        if (!typeRegistrar.IsResolved(namedTypeReference))
                        {
                            typeRegistrar.Register(
                                typeRegistrar.CreateInstance(namedType),
                                typeReference.Scope,
                                ExtendedType.Tools.IsGenericBaseType(namedType));
                        }
                    }
                    else
                    {
                        TryMapToExistingRegistration(
                            typeRegistrar,
                            typeInfo,
                            typeReference.Context,
                            typeReference.Scope);
                    }
                }
            }
        }
Пример #4
0
    private static void LazyRegistrationsCanBeResolved(ITypeRegistrar registrar)
    {
        // Given
        var instance      = new MockService();
        var factoryCalled = false;

        registrar.RegisterLazy(typeof(IMockService), () =>
        {
            factoryCalled = true;
            return(instance);
        });
        var resolver = registrar.Build();

        // When
        var actual = resolver.Resolve(typeof(IMockService));

        // Then
        if (!factoryCalled)
        {
            throw new TestFailedException(
                      "Expected the factory to be called, to resolve the lazy registration.");
        }

        if (!ReferenceEquals(actual, instance))
        {
            throw new TestFailedException(
                      "Expected the resolver to return exactly the result of the lazy-registered factory.");
        }
    }
Пример #5
0
    public Configurator(ITypeRegistrar registrar)
    {
        _registrar = registrar;

        Commands = new List <ConfiguredCommand>();
        Settings = new CommandAppSettings(registrar);
        Examples = new List <string[]>();
    }
        public static void RegisterDependencies(this ITypeRegistrar registrar, CommandModel model)
        {
            var stack = new Stack <CommandInfo>();

            model.Commands.ForEach(c => stack.Push(c));
            if (model.DefaultCommand != null)
            {
                stack.Push(model.DefaultCommand);
            }

            while (stack.Count > 0)
            {
                var command = stack.Pop();

                if (command.SettingsType == null)
                {
                    // TODO: Error message
                    throw new InvalidOperationException("Command setting type cannot be null.");
                }

                if (command.CommandType != null)
                {
                    registrar?.Register(typeof(ICommand), command.CommandType);
                    registrar?.Register(command.CommandType, command.CommandType);
                }

                if (!command.SettingsType.IsAbstract)
                {
                    registrar?.Register(command.SettingsType, command.SettingsType);
                }

                foreach (var parameter in command.Parameters)
                {
                    var pairDeconstructor = parameter?.PairDeconstructor?.Type;
                    if (pairDeconstructor != null)
                    {
                        registrar?.Register(pairDeconstructor, pairDeconstructor);
                    }

                    var typeConverterTypeName = parameter?.Converter?.ConverterTypeName;
                    if (!string.IsNullOrWhiteSpace(typeConverterTypeName))
                    {
                        var typeConverterType = Type.GetType(typeConverterTypeName);
                        Debug.Assert(typeConverterType != null, "Could not create type");
                        registrar?.Register(typeConverterType, typeConverterType);
                    }
                }

                foreach (var child in command.Children)
                {
                    stack.Push(child);
                }
            }
        }
 public void Register(
     ITypeRegistrar typeRegistrar,
     IEnumerable <ITypeReference> typeReferences)
 {
     foreach (ISchemaTypeReference typeReference in
              typeReferences.OfType <ISchemaTypeReference>())
     {
         if (!typeRegistrar.IsResolved(typeReference))
         {
             typeRegistrar.Register((TypeSystemObjectBase)typeReference.Type);
         }
     }
 }
Пример #8
0
    private static void InstanceRegistrationsCanBeResolved(ITypeRegistrar registrar)
    {
        // Given
        var instance = new MockService();

        registrar.RegisterInstance(typeof(IMockService), instance);
        var resolver = registrar.Build();

        // When
        var actual = resolver.Resolve(typeof(IMockService));

        // Then
        if (!ReferenceEquals(actual, instance))
        {
            throw new TestFailedException(
                      "Expected the resolver to resolve exactly the registered instance.");
        }
    }
Пример #9
0
 public void Register(
     ITypeRegistrar typeRegistrar,
     IEnumerable <ITypeReference> typeReferences)
 {
     foreach (ISyntaxTypeReference typeReference in
              typeReferences.OfType <ISyntaxTypeReference>())
     {
         if (Scalars.TryGetScalar(
                 typeReference.Type.NamedType().Name.Value,
                 out IClrTypeReference namedTypeReference))
         {
             if (!typeRegistrar.IsResolved(namedTypeReference))
             {
                 typeRegistrar.Register(typeRegistrar.CreateInstance(namedTypeReference.Type));
             }
         }
     }
 }
Пример #10
0
        public void Handle(ITypeRegistrar typeRegistrar, ITypeReference typeReference)
        {
            var typeRef = (SchemaTypeReference)typeReference;

            if (!typeRegistrar.IsResolved(typeReference))
            {
                ITypeSystemMember tsm = typeRef.Type;

                // if it is a type object we will make sure it is unwrapped.
                if (typeRef.Type is IType type)
                {
                    tsm = type.NamedType();
                }

                if (tsm is TypeSystemObjectBase tso)
                {
                    typeRegistrar.Register(tso, typeReference.Scope);
                }
            }
        }
Пример #11
0
        private void TryMapToExistingRegistration(
            ITypeRegistrar typeRegistrar,
            TypeInfo typeInfo,
            TypeContext context)
        {
            ClrTypeReference?normalizedTypeRef = null;
            bool             resolved          = false;

            for (int i = 0; i < typeInfo.Components.Count; i++)
            {
                normalizedTypeRef = new ClrTypeReference(
                    typeInfo.Components[i],
                    context);

                if (typeRegistrar.IsResolved(normalizedTypeRef))
                {
                    resolved = true;
                    break;
                }
            }

            if (!resolved && normalizedTypeRef is { })
Пример #12
0
    private static void RegistrationsCanBeResolved(ITypeRegistrar registrar)
    {
        // Given
        registrar.Register(typeof(IMockService), typeof(MockService));
        var resolver = registrar.Build();

        // When
        var actual = resolver.Resolve(typeof(IMockService));

        // Then
        if (actual == null)
        {
            throw new TestFailedException(
                      $"Expected the resolver to resolve an instance of {nameof(MockService)}. Actually resolved null.");
        }

        if (actual is not MockService)
        {
            throw new TestFailedException(
                      $"Expected the resolver to resolve an instance of {nameof(MockService)}. Actually resolved {actual.GetType().Name}.");
        }
    }
Пример #13
0
        public Configurator(ITypeRegistrar registrar, Type defaultCommand = null)
        {
            _registrar = registrar;

            Commands = new List <ConfiguredCommand>();
            ShouldPropagateExceptions = false;
            ParsingMode = ParsingMode.Relaxed;

            if (defaultCommand != null)
            {
                if (!typeof(ICommand).IsAssignableFrom(defaultCommand))
                {
                    throw new ArgumentException($"The specified default command type '{defaultCommand}' is not a command.", nameof(defaultCommand));
                }

                // Initialize the default command.
                var settingsType = ConfigurationHelper.GetSettingsType(defaultCommand);
                DefaultCommand = new ConfiguredCommand(Constants.DefaultCommandName, defaultCommand, settingsType, true);

                // Register the default command.
                _registrar.RegisterCommand(defaultCommand, settingsType);
            }
        }
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (SyntaxTypeReference typeReference in
                     typeReferences.OfType <SyntaxTypeReference>())
            {
                if (Scalars.TryGetScalar(
                        typeReference.Type.NamedType().Name.Value,
                        out Type? scalarType))
                {
                    ExtendedTypeReference namedTypeReference =
                        _typeInspector.GetTypeRef(scalarType);

                    if (!typeRegistrar.IsResolved(namedTypeReference))
                    {
                        typeRegistrar.Register(
                            typeRegistrar.CreateInstance(namedTypeReference.Type.Type),
                            typeReference.Scope);
                    }
                }
            }
        }
Пример #15
0
    private static void ResolvingNotRegisteredServiceReturnsNull(ITypeRegistrar registrar)
    {
        // Given no registration
        var resolver = registrar.Build();

        try
        {
            // When
            var actual = resolver.Resolve(typeof(IMockService));

            // Then
            if (actual != null)
            {
                throw new TestFailedException(
                          $"Expected the resolver to resolve null, since no service was registered. Actually resolved {actual.GetType().Name}.");
            }
        }
        catch (Exception ex)
        {
            throw new TestFailedException(
                      $"Expected the resolver not to throw, but caught {ex.GetType().Name}.", ex);
        }
    }
        public void Register(
            ITypeRegistrar typeRegistrar,
            IEnumerable <ITypeReference> typeReferences)
        {
            foreach (SchemaTypeReference typeReference in
                     typeReferences.OfType <SchemaTypeReference>())
            {
                if (!typeRegistrar.IsResolved(typeReference))
                {
                    ITypeSystemMember tsm = typeReference.Type;

                    // if it is a type object we will make sure it is unwrapped.
                    if (typeReference.Type is IType type)
                    {
                        tsm = type.NamedType();
                    }

                    if (tsm is TypeSystemObjectBase tso)
                    {
                        typeRegistrar.Register(tso, typeReference.Scope);
                    }
                }
            }
        }
Пример #17
0
    public void Handle(ITypeRegistrar typeRegistrar, ITypeReference typeReference)
    {
        var typeRef = (ExtendedTypeReference)typeReference;

        if (_typeInspector.TryCreateTypeInfo(typeRef.Type, out ITypeInfo? typeInfo) &&
            !ExtendedType.Tools.IsNonGenericBaseType(typeInfo.NamedType))
        {
            if (typeInfo.NamedType == typeof(IExecutable))
            {
                throw ThrowHelper.NonGenericExecutableNotAllowed();
            }

            Type namedType = typeInfo.NamedType;
            if (IsTypeSystemObject(namedType))
            {
                IExtendedType         extendedType       = _typeInspector.GetType(namedType);
                ExtendedTypeReference namedTypeReference = typeRef.With(extendedType);

                if (!typeRegistrar.IsResolved(namedTypeReference))
                {
                    typeRegistrar.Register(
                        typeRegistrar.CreateInstance(namedType),
                        typeReference.Scope,
                        ExtendedType.Tools.IsGenericBaseType(namedType));
                }
            }
            else
            {
                TryMapToExistingRegistration(
                    typeRegistrar,
                    typeInfo,
                    typeReference.Context,
                    typeReference.Scope);
            }
        }
    }
Пример #18
0
 internal TypeRegistrar(ITypeRegistrar registrar)
 {
     _registrar = registrar ?? throw new ArgumentNullException(nameof(registrar));
 }
Пример #19
0
 public CommandExecutor(ITypeRegistrar registrar)
 {
     _registrar = registrar ?? throw new ArgumentNullException(nameof(registrar));
     _registrar.Register(typeof(DefaultPairDeconstructor), typeof(DefaultPairDeconstructor));
 }
 public static void RegisterCommand(this ITypeRegistrar registrar, Type commandType, Type settingsType)
 {
     registrar?.Register(typeof(ICommand), commandType);
     registrar?.Register(commandType, commandType);
     registrar?.Register(settingsType, settingsType);
 }
Пример #21
0
 public Configurator(ITypeRegistrar registrar)
 {
     _registrar = registrar;
     Commands   = new List <ConfiguredCommand>();
 }
Пример #22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandApp"/> class.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 /// <param name="defaultCommand">The default command type.</param>
 internal CommandApp(ITypeRegistrar registrar = null, Type defaultCommand = null)
 {
     _configurator = new Configurator(registrar, defaultCommand);
     _executor     = new CommandExecutor(registrar);
 }
Пример #23
0
 public CommandExecutor(ITypeRegistrar registrar)
 {
     _binder    = new CommandBinder();
     _registrar = registrar;
 }
Пример #24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandApp"/> class.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 public CommandApp(ITypeRegistrar registrar = null)
     : this(registrar, null)
 {
 }
Пример #25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandApp{TDefaultCommand}"/> class.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 public CommandApp(ITypeRegistrar registrar = null)
 {
     _app = new CommandApp(registrar, typeof(TDefaultCommand));
 }
Пример #26
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CommandApp"/> class.
 /// </summary>
 /// <param name="registrar">The registrar.</param>
 public CommandApp(ITypeRegistrar registrar = null)
 {
     _configurator = new Configurator(registrar);
     _executor     = new CommandExecutor(registrar);
 }
Пример #27
0
 public CommandExecutor(ITypeRegistrar registrar)
 {
     _registrar = registrar;
 }
 public CommandAppSettings(ITypeRegistrar registrar)
 {
     Registrar       = new TypeRegistrar(registrar);
     CaseSensitivity = CaseSensitivity.All;
 }
Пример #29
0
 public CommandAppSettings(ITypeRegistrar registrar)
 {
     Registrar = new TypeRegistrar(registrar);
 }
Пример #30
0
 public Configurator(ConfiguredCommand command, ITypeRegistrar registrar)
 {
     _command   = command;
     _registrar = registrar;
 }