コード例 #1
0
        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);
                }
            }
        }
コード例 #2
0
        public void AddCommand <TCommand>(string name) where TCommand : class, ICommandLimiter <TSettings>
        {
            var settingsType = ConfigurationHelper.GetSettingsType(typeof(TCommand));
            var command      = new ConfiguredCommand(name, typeof(TCommand), settingsType);

            _command.Children.Add(command);

            // Register the command and the settings.
            _registrar?.Register(typeof(ICommand), typeof(TCommand));
            _registrar?.Register(typeof(TCommand), typeof(TCommand));
            _registrar?.Register(settingsType, settingsType);
        }
コード例 #3
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);
                    }
                }
            }
        }
コード例 #4
0
        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);
                    }
                }
            }
        }
コード例 #5
0
 public void Register(
     ITypeRegistrar typeRegistrar,
     IEnumerable <ITypeReference> typeReferences)
 {
     foreach (ISchemaTypeReference typeReference in
              typeReferences.OfType <ISchemaTypeReference>())
     {
         if (!typeRegistrar.IsResolved(typeReference))
         {
             typeRegistrar.Register((TypeSystemObjectBase)typeReference.Type);
         }
     }
 }
コード例 #6
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));
             }
         }
     }
 }
コード例 #7
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);
                }
            }
        }
コード例 #8
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}.");
        }
    }
コード例 #9
0
        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);
                    }
                }
            }
        }
コード例 #10
0
        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);
                    }
                }
            }
        }
コード例 #11
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);
            }
        }
    }
コード例 #12
0
ファイル: CommandExecutor.cs プロジェクト: gep13/spectre.cli
 public CommandExecutor(ITypeRegistrar registrar)
 {
     _registrar = registrar ?? throw new ArgumentNullException(nameof(registrar));
     _registrar.Register(typeof(DefaultPairDeconstructor), typeof(DefaultPairDeconstructor));
 }
コード例 #13
0
 public void Register <TService, TImplementation>()
     where TImplementation : TService
 {
     _registrar.Register(typeof(TService), typeof(TImplementation));
 }
コード例 #14
0
 public static void RegisterCommand(this ITypeRegistrar registrar, Type commandType, Type settingsType)
 {
     registrar?.Register(typeof(ICommand), commandType);
     registrar?.Register(commandType, commandType);
     registrar?.Register(settingsType, settingsType);
 }