Exemple #1
0
        public virtual void RegisterCommandsFromContainer()
        {
            var registrations       = _container.GetRootRegistrations();
            var registeredTypes     = registrations.Select(x => x.Registration.ImplementationType).Distinct().ToList();
            var commandHandlerTypes = registeredTypes.Where(x => x.GetCustomAttribute <DecoratorAttribute>() == null).Where(x =>
                                                                                                                            x.GetTypeInfo().IsAssignableToGenericType(typeof(ICommandHandler <,>)) ||
                                                                                                                            x.GetTypeInfo().IsAssignableToGenericType(typeof(IAsyncCommandHandler <,>))).ToList();

            RegisterCommands(commandHandlerTypes);
        }
Exemple #2
0
        private void RegisterCommands()
        {
            var registrations       = _container.GetRootRegistrations();
            var registeredTypes     = registrations.Select(x => x.ServiceType).ToList();
            var commandHandlerTypes = registeredTypes.Where(x =>
                                                            !x.IsInterface &&
                                                            x.GetTypeInfo().IsAssignableToGenericType(typeof(ICommandHandler <,>))).ToList();


            foreach (var commandHandlerType in commandHandlerTypes)
            {
                var descriptionAttribute = commandHandlerType.GetTypeInfo().GetCustomAttribute <DescriptionAttribute>();
                var genericType          = commandHandlerType.GetInterfaces().Single(x => x.GetTypeInfo().IsAssignableToGenericType(typeof(ICommandHandler <,>)));
                var genericArguments     = genericType.GetGenericArguments();
                var commandType          = genericArguments[0];
                var resultType           = genericArguments[1];

                var exposeAttribute = commandType.GetCustomAttribute <ExposeAttribute>();

                if (exposeAttribute == null)
                {
                    continue;
                }

                //
                // For each command here I would like to create a mutation that has 1 input of type commandType and returns the resultType
                // Example command can be found inside Commands/TestCommand.cs
                //
                // Each command can be resolved using: var result = (new "commandHandlerType"()).Handle("command instance");
                //

                var inputTypeName = commandType.Name; // + "Input";
                var inputType     = FindType(inputTypeName);

                if (inputType == null)
                {
                    var inputObjectType = typeof(InputObjectGraphType <>).MakeGenericType(commandType);

                    inputType = (IGraphType)Activator.CreateInstance(inputObjectType);

                    inputType.Name = inputTypeName;

                    RegisterType(inputType);
                }

                var resultTypeName = resultType.Name;
                var resultGqlType  = FindType(resultTypeName);

                if (resultGqlType == null)
                {
                    var returnObjectType = typeof(ObjectGraphType <>).MakeGenericType(resultType);

                    resultGqlType      = (IGraphType)Activator.CreateInstance(returnObjectType);
                    resultGqlType.Name = resultTypeName;

                    RegisterType(resultGqlType);
                }


                var queryArgument = new QueryArgument(inputType);
                queryArgument.Name = "command";

                var commandQueryParameters = new List <QueryArgument>()
                {
                    queryArgument
                };

                var mutationName = CamelCase(new Regex("CommandHandler$").Replace(commandHandlerType.Name, ""));

                if (!Mutation.HasField(mutationName))
                {
                    var type = new FieldType
                    {
                        Type         = resultGqlType.GetType(), //.ToGraphType(),
                        ResolvedType = resultGqlType,
                        Name         = CamelCase(mutationName),
                        Description  = descriptionAttribute?.Description,
                        Arguments    = new QueryArguments(commandQueryParameters)
                    };

                    Mutation.AddField(type);
                }
            }
        }