Exemple #1
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var rootType = DomainModel.Find(argument.Name);

            if (rootType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find domain object type {0}.".With(argument.Name),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!typeof(IObjectHistory).IsAssignableFrom(rootType))
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) does not support history tracking. 
Please check your arguments.".With(argument.Name), null));
            }

            if (!Permissions.CanAccess(rootType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            if (argument.Uri == null || argument.Uri.Length == 0)
            {
                return(CommandResult <TOutput> .Fail(
                           "Uri not specified.",
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            try
            {
                var commandType = typeof(FindCommand <>).MakeGenericType(rootType);
                var command     = Activator.CreateInstance(commandType) as IFindCommand;
                var result      = command.Find(output, locator, argument.Uri);

                return(CommandResult <TOutput> .Success(result.Result, "Found {0} item(s)", result.Count));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Exemple #2
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
            if (!typeof(IEvent).IsAssignableFrom(eventType))
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an event. 
Please check your arguments.".With(argument.Name), null));
            }
            if (!Permissions.CanAccess(eventType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            try
            {
                ISubmitCommand command;
                if (!Cache.TryGetValue(eventType, out command))
                {
                    var commandType = typeof(SubmitEventCommand <>).MakeGenericType(eventType);
                    command = Activator.CreateInstance(commandType) as ISubmitCommand;
                    var newCache = new Dictionary <Type, ISubmitCommand>(Cache);
                    newCache[eventType] = command;
                    Cache = newCache;
                }
                var result = command.Submit(input, output, locator, argument.ReturnInstance ?? false, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Event stored"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Exemple #3
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
            if (!Permissions.CanAccess(eventType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            try
            {
                IQueueCommand command;
                if (!Cache.TryGetValue(eventType, out command))
                {
                    var commandType = typeof(QueueEventCommand <>).MakeGenericType(eventType);
                    command = Activator.CreateInstance(commandType) as IQueueCommand;
                    var newCache = new Dictionary <Type, IQueueCommand>(Cache);
                    newCache[eventType] = command;
                    Cache = newCache;
                }
                command.Queue(input, locator, DataContext, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Accepted, default(TOutput), "Event queued"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Exemple #4
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var reportType = DomainModel.Find(argument.ReportName);

            if (reportType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find report type {0}.".With(argument.ReportName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
            var ri = reportType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IReport <>));

            if (ri == null)
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an report. 
Please check your arguments.".With(argument.ReportName), null));
            }
            if (!Permissions.CanAccess(reportType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.ReportName));
            }
            try
            {
                var commandType = typeof(PopulateReportCommand <,>).MakeGenericType(reportType, ri.GetGenericArguments()[0]);
                var command     = Activator.CreateInstance(commandType) as IPopulateReport;
                var result      = command.Populate(input, output, locator, argument.Data);

                return(CommandResult <TOutput> .Success(result, "Report populated"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var eventType = DomainModel.Find(argument.Name);

            if (eventType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find event type {0}.".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var aggregateInterface = eventType.GetInterfaces().FirstOrDefault(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IDomainEvent <>));
            var aggregateType      = aggregateInterface != null?aggregateInterface.GetGenericArguments()[0] : null;

            if (aggregateType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "{0} does not implement IDomainEvent<TAggregate>.".With(eventType.FullName),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(eventType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(eventType.FullName));
            }
            if (!Permissions.CanAccess(aggregateType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(aggregateType.FullName));
            }
            try
            {
                IQueueCommand command;
                if (!Cache.TryGetValue(eventType, out command))
                {
                    var commandType = typeof(QueueEventCommand <,>).MakeGenericType(eventType, aggregateType);
                    command = Activator.CreateInstance(commandType) as IQueueCommand;
                    var newCache = new Dictionary <Type, IQueueCommand>(Cache);
                    newCache[eventType] = command;
                    Cache = newCache;
                }
                var result = command.Queue(input, output, locator, argument.Uri, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Accepted, result, "Event queued"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var rootType = DomainModel.Find(argument.RootName);

            if (rootType == null)
            {
                return(CommandResult <TOutput> .Fail("Couldn't find root type: {0}".With(argument.RootName), @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!typeof(IAggregateRoot).IsAssignableFrom(rootType))
            {
                return(CommandResult <TOutput> .Fail(@"Specified type ({0}) is not an aggregate root. 
Please check your arguments.".With(argument.RootName), null));
            }

            if (!Permissions.CanAccess(rootType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.RootName));
            }
            if (argument.ToInsert == null && argument.ToUpdate == null && argument.ToDelete == null)
            {
                return(CommandResult <TOutput> .Fail("Data to persist not specified.", @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, rootType))));
            }

            try
            {
                IPersistCommand command;
                if (!Cache.TryGetValue(rootType, out command))
                {
                    var commandType = typeof(PersistAggregateRootCommand <>).MakeGenericType(rootType);
                    command = Activator.CreateInstance(commandType) as IPersistCommand;
                    var newCache = new Dictionary <Type, IPersistCommand>(Cache);
                    newCache[rootType] = command;
                    Cache = newCache;
                }
                var uris = command.Persist(input, locator, argument.ToInsert, argument.ToUpdate, argument.ToDelete);

                return(CommandResult <TOutput> .Success(output.Serialize(uris), "Data persisted"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Exemple #7
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var documentType = DomainModel.FindNested(argument.CubeName, argument.TemplaterName);

            if (documentType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find Templater type {0} for {1}.".With(argument.TemplaterName, argument.CubeName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(documentType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden("{0} in {1}.".With(argument.TemplaterName, argument.CubeName)));
            }
            if (!typeof(IDocumentReport <DataTable>).IsAssignableFrom(documentType))
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Templater type {0} for {1} is not IDocumentReport<DataTable>. Check {0}.".With(
                             documentType.FullName,
                             argument.CubeName),
                         null));
            }

            IDocumentReport <DataTable> report;

            try
            {
                report = locator.Resolve <IDocumentReport <DataTable> >(documentType);
            }
            catch (Exception ex)
            {
                return(CommandResult <TOutput> .Fail(
                           @"Can't create document report. Is report {0} registered in system?".With(documentType.FullName),
                           ex.GetDetailedExplanation()));
            }
            try
            {
                var table  = AnalyzeOlapCube.PopulateTable(input, output, locator, DomainModel, argument, principal, Permissions);
                var result = report.Create(table);
                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, Serialize(output, result), "Report created"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Exemple #8
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var objectType = DomainModel.Find(argument.Name);

            if (objectType == null)
            {
                return(CommandResult <TOutput> .Fail("Couldn't find domain object type {0}.".With(argument.Name), null));
            }

            if (!Permissions.CanAccess(objectType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            if (argument.Uri == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Uri to read not specified.",
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, objectType))));
            }

            try
            {
                IFindCommand command;
                if (!Cache.TryGetValue(objectType, out command))
                {
                    var commandType = typeof(FindCommand <>).MakeGenericType(objectType);
                    command = Activator.CreateInstance(commandType) as IFindCommand;
                    var newCache = new Dictionary <Type, IFindCommand>(Cache);
                    newCache[objectType] = command;
                    Cache = newCache;
                }
                var result = command.Find(output, locator, Permissions, principal, argument.Uri);

                return(result != null
                                        ? CommandResult <TOutput> .Success(result, "Object found")
                                        : CommandResult <TOutput> .Return(
                           HttpStatusCode.NotFound,
                           result,
                           "Can't find {0} with Uri: {1}.".With(objectType.FullName, argument.Uri)));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output, objectType))));
            }
        }
Exemple #9
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            Type domainType = DomainModel.Find(argument.DomainObjectName);

            if (domainType == null)
            {
                return(CommandResult <TOutput> .Fail(
                           "Couldn't find domain object type {0}.".With(argument.DomainObjectName),
                           @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            if (!Permissions.CanAccess(domainType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.DomainObjectName));
            }
            var validationType = DomainModel.FindNested(domainType.FullName, argument.ValidationName);

            if (validationType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find validation {0} for {1}".With(
                             argument.ValidationName,
                             argument.DomainObjectName),
                         null));
            }

            if (!Permissions.CanAccess(validationType.FullName, principal))
            {
                return
                    (CommandResult <TOutput> .Return(
                         HttpStatusCode.Forbidden,
                         default(TOutput),
                         "You don't have permission to access: {0}.",
                         argument.ValidationName));
            }

            var bindingType = string.IsNullOrWhiteSpace(argument.BindingObjectName) ? null : DomainModel.Find(argument.BindingObjectName);

            try
            {
                var commandType = typeof(FindInvalidDataCommand <>).MakeGenericType(domainType);
                var command     = Activator.CreateInstance(commandType) as IFindInvalidDataCommand;
                var result      = command.FindInvalid(input, output, DomainModel, locator, validationType, bindingType, argument.Specification);
                return(CommandResult <TOutput> .Success(result.Result, "Found {0} item(s)", result.Count));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }
Exemple #10
0
        public ICommandResult <TOutput> Execute <TInput, TOutput>(
            IServiceProvider locator,
            ISerialization <TInput> input,
            ISerialization <TOutput> output,
            IPrincipal principal,
            TInput data)
        {
            var either = CommandResult <TOutput> .Check <Argument <TInput>, TInput>(input, output, data, CreateExampleArgument);

            if (either.Error != null)
            {
                return(either.Error);
            }
            var argument = either.Argument;

            var serviceType = TypeResolver.Resolve(argument.Name);

            if (serviceType == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Couldn't find service: {0}".With(argument.Name),
                         @"Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }

            var serviceInterface =
                serviceType.GetInterfaces()
                .FirstOrDefault(it => it.IsGenericType && typeof(IServerService <,>) == it.GetGenericTypeDefinition());

            if (serviceInterface == null)
            {
                return
                    (CommandResult <TOutput> .Fail(
                         "Object: {0} is not a valid service.".With(argument.Name),
                         "{0} must implement {1} to be executed as a service call".With(argument.Name, typeof(IServerService <,>).FullName)));
            }

            if (!Permissions.CanAccess(serviceType.FullName, principal))
            {
                return(CommandResult <TOutput> .Forbidden(argument.Name));
            }
            try
            {
                IExecuteCommand command;
                if (!Cache.TryGetValue(serviceType, out command))
                {
                    var commandType = typeof(ExecuteServiceCommand <,>).MakeGenericType(serviceInterface.GetGenericArguments());
                    command = Activator.CreateInstance(commandType) as IExecuteCommand;
                    var newCache = new Dictionary <Type, IExecuteCommand>(Cache);
                    newCache[serviceType] = command;
                    Cache = newCache;
                }
                var result = command.Execute(input, output, locator, serviceType, argument.Data);

                return(CommandResult <TOutput> .Return(HttpStatusCode.Created, result, "Service executed"));
            }
            catch (ArgumentException ex)
            {
                return(CommandResult <TOutput> .Fail(
                           ex.Message,
                           ex.GetDetailedExplanation() + @"
Example argument: 
" + CommandResult <TOutput> .ConvertToString(CreateExampleArgument(output))));
            }
        }