Пример #1
1
		//private readonly IRepositoryBulkReader BulkReader;
		//private readonly Lazy<World>[] LazyWorlds = new Lazy<World>[512];

		public Context(IObjectFactory factory, IDatabaseQueryManager manager)
		{
			var scope = factory.CreateScope(null);
			scope.RegisterInterfaces(manager.StartQuery(false));
			WorldRepository = scope.Resolve<IPersistableRepository<World>>();
			FortuneRepository = scope.Resolve<IQueryableRepository<Fortune>>();
			//BulkReader = scope.BulkRead(ChunkedMemoryStream.Static());
		}
Пример #2
0
        private Scope SetupReadonlyScope()
        {
            var inner = Factory.CreateScope(null);

            try
            {
                var query = Queries.StartQuery(false);
                inner.RegisterInstance(query);
                return(new Scope(inner, query));
            }
            catch (Exception ex)
            {
                TraceSource.TraceEvent(TraceEventType.Critical, 5301, "{0}", ex);
                inner.Dispose();
                throw;
            }
        }
Пример #3
0
        //private readonly IRepositoryBulkReader BulkReader;
        //private readonly Lazy<World>[] LazyWorlds = new Lazy<World>[512];

        public Context(IObjectFactory factory, IDatabaseQueryManager manager)
        {
            var scope = factory.CreateScope(null);

            scope.RegisterInterfaces(manager.StartQuery(false));
            WorldRepository   = scope.Resolve <IPersistableRepository <World> >();
            FortuneRepository = scope.Resolve <IQueryableRepository <Fortune> >();
            //BulkReader = scope.BulkRead(ChunkedMemoryStream.Static());
        }
Пример #4
0
 public IServiceScope CreateScope()
 {
     return(new AspNetContainer(Container.CreateScope(null)));
 }
Пример #5
0
        /// <summary>
        /// Create unnamed scope. This scope will not be tracked.
        /// </summary>
        /// <param name="factory">container service</param>
        /// <returns>nested service</returns>
        public static IObjectFactory CreateInnerFactory(this IObjectFactory factory)
        {
            Contract.Requires(factory != null);

            return(factory.CreateScope(null));
        }
Пример #6
0
        public IProcessingResult <TOutput> Execute <TInput, TOutput>(IServerCommandDescription <TInput>[] commandDescriptions)
        {
            if (commandDescriptions == null || commandDescriptions.Length == 0)
            {
                return
                    (ProcessingResult <TOutput> .Create(
                         "There are no commands to execute.",
                         HttpStatusCode.BadRequest,
                         null,
                         0));
            }

            var stopwatch = Stopwatch.StartNew();

            foreach (var c in commandDescriptions)
            {
                if (!Permissions.CanAccess(c.CommandType))
                {
                    Logger.Trace(
                        () => "Access denied. User: {0}. Target: {1}.".With(
                            Thread.CurrentPrincipal.Identity.Name,
                            c.CommandType.FullName));
                    return
                        (ProcessingResult <TOutput> .Create(
                             "You don't have permission to execute command: " + c.CommandType.FullName,
                             HttpStatusCode.Forbidden,
                             null,
                             stopwatch.ElapsedMilliseconds));
                }
            }

            var inputSerializer  = GetSerializer <TInput>();
            var outputSerializer = GetSerializer <TOutput>();

            var commands       = new CommandInfo <TInput> [commandDescriptions.Length];
            var useTransaction = false;

            for (int i = 0; i < commands.Length; i++)
            {
                var c = commands[i] = new CommandInfo <TInput>(commandDescriptions[i], ActualCommands);
                useTransaction = useTransaction || !c.IsReadOnly;
                if (c.Target == null)
                {
                    Logger.Trace(
                        () => "Unknown target. User: {0}. Target: {1}.".With(
                            Thread.CurrentPrincipal.Identity.Name,
                            commandDescriptions[i].CommandType.FullName));
                    return
                        (ProcessingResult <TOutput> .Create(
                             "Unknown command: {0}. Check if requested command is registered in the system".With(
                                 c.Description.CommandType),
                             HttpStatusCode.BadRequest,
                             null,
                             stopwatch.ElapsedMilliseconds));
                }
            }

            var scopeID = useTransaction ? Guid.NewGuid().ToString() : null;

            using (var scope = ObjectFactory.CreateScope(scopeID))
            {
                var            executedCommands = new List <ICommandResultDescription <TOutput> >(commandDescriptions.Length);
                IDatabaseQuery query            = null;
                try
                {
                    try
                    {
                        query = TransactionManager.StartQuery(useTransaction);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error("Can't start query. Error: " + ex.ToString());
                        return(Exceptions.DebugMode
                                                        ? ProcessingResult <TOutput> .Create(
                                   ex.ToString(),
                                   HttpStatusCode.ServiceUnavailable,
                                   null,
                                   stopwatch.ElapsedMilliseconds)
                                                        : ProcessingResult <TOutput> .Create(
                                   "Unable to create database connection",
                                   HttpStatusCode.ServiceUnavailable,
                                   null,
                                   stopwatch.ElapsedMilliseconds));
                    }
                    scope.RegisterInstance(query);
                    if (useTransaction)
                    {
                        scope.RegisterInstance <IEnumerable <IServerCommandDescription <TInput> > >(commandDescriptions);
                        scope.RegisterInstance <IEnumerable <ICommandResultDescription <TOutput> > >(executedCommands);
                        scope.RegisterType(typeof(ProcessingContext), typeof(IProcessingEngine), InstanceScope.Singleton);
                    }
                    foreach (var cmd in commands)
                    {
                        var result = cmd.GetCommand(scope).Execute(inputSerializer, outputSerializer, cmd.Description.Data);
                        if (result == null)
                        {
                            throw new FrameworkException("Result returned null for " + cmd.Target.FullName);
                        }
                        executedCommands.Add(CommandResultDescription <TOutput> .Create(cmd.Description.RequestID, result));
                        if ((int)result.Status >= 400)
                        {
                            TransactionManager.EndQuery(query, false);
                            return(ProcessingResult <TOutput> .Create(
                                       result.Message,
                                       result.Status,
                                       executedCommands,
                                       stopwatch.ElapsedMilliseconds));
                        }
                    }

                    TransactionManager.EndQuery(query, true);
                    return
                        (ProcessingResult <TOutput> .Create(
                             "Commands executed in: " + stopwatch.ElapsedMilliseconds.ToString() + " ms.",
                             HttpStatusCode.OK,
                             executedCommands,
                             stopwatch.ElapsedMilliseconds));
                }
                catch (SecurityException ex)
                {
                    Logger.Trace(
                        () => "Security error. User: {0}. Error: {1}.".With(
                            Thread.CurrentPrincipal.Identity.Name,
                            ex.ToString()));
                    TransactionManager.EndQuery(query, false);
                    return
                        (ProcessingResult <TOutput> .Create(
                             "You don't have authorization to perform requested action: " + ex.Message,
                             HttpStatusCode.Forbidden,
                             executedCommands,
                             stopwatch.ElapsedMilliseconds));
                }
                catch (AggregateException ex)
                {
                    Logger.Trace(
                        () => "Multiple errors. User: {0}. Error: {1}.".With(
                            Thread.CurrentPrincipal.Identity.Name,
                            ex.GetDetailedExplanation()));
                    TransactionManager.EndQuery(query, false);
                    return(Exceptions.DebugMode
                                                ? ProcessingResult <TOutput> .Create(
                               ex.GetDetailedExplanation(),
                               HttpStatusCode.InternalServerError,
                               executedCommands,
                               stopwatch.ElapsedMilliseconds)
                                                : ProcessingResult <TOutput> .Create(
                               string.Join(Environment.NewLine, ex.InnerExceptions.Select(it => it.Message)),
                               HttpStatusCode.InternalServerError,
                               executedCommands,
                               stopwatch.ElapsedMilliseconds));
                }
                catch (OutOfMemoryException ex)
                {
                    Logger.Error("Out of memory error. Error: " + ex.GetDetailedExplanation());
                    TransactionManager.EndQuery(query, false);
                    return(Exceptions.DebugMode
                                                ? ProcessingResult <TOutput> .Create(
                               ex.GetDetailedExplanation(),
                               HttpStatusCode.ServiceUnavailable,
                               executedCommands,
                               stopwatch.ElapsedMilliseconds)
                                                : ProcessingResult <TOutput> .Create(
                               ex.Message,
                               HttpStatusCode.ServiceUnavailable,
                               executedCommands,
                               stopwatch.ElapsedMilliseconds));
                }
                catch (Exception ex)
                {
                    Logger.Trace(
                        () => "Unexpected error. User: {0}. Error: {1}.".With(
                            Thread.CurrentPrincipal.Identity.Name,
                            ex.GetDetailedExplanation()));
                    TransactionManager.EndQuery(query, false);
                    return(Exceptions.DebugMode
                                                ? ProcessingResult <TOutput> .Create(
                               ex.GetDetailedExplanation(),
                               HttpStatusCode.InternalServerError,
                               executedCommands,
                               stopwatch.ElapsedMilliseconds)
                                                : ProcessingResult <TOutput> .Create(
                               ex.Message,
                               HttpStatusCode.InternalServerError,
                               executedCommands,
                               stopwatch.ElapsedMilliseconds));
                }
            }
        }