definitions.cs

来自「ajax patterns 这是关于ajax设计模式方面的原代码」· CS 代码 · 共 137 行

CS
137
字号
/*
    Copyright 2006 Christian Gross http://www.devspace.com
    From the book Ajax Patterns and Best Practices

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
using System;
using System.Web;
using System.Collections.Generic;
using Devspace.Commons.TDD;
using System.Collections;
using System.Threading;

namespace Devspace.Ajax.RestMVC {
    public interface IRunnable {
        void Run();
    }
    public interface IRequest {
    }
    public interface IResult { }
    
    public interface IParent {
        void AddResult(IResult result);
        void AddCommand(ICommand cmd);
        ICommand[] Commands { get; }
        void ClearAllCommands();
        void ProcessRequest(IRequest request);
        void ProcessRequest(String type, IRequest request);
    }
    
    public interface ICommand {
        void SetRequest(IRequest request);
        void AssignParent(IParent parent);
        String Identifier { get; }
    }
    
    
    public abstract class ParentBase : IParent {
        private List< ICommand> _commands = new List< ICommand>();
        
        public virtual void AddCommand(ICommand cmd) {
            _commands.Add(cmd);
        }
        public virtual ICommand[] Commands {
            get {
                return _commands.ToArray();
            }
        }
        public virtual void ClearAllCommands() {
            _commands.Clear();
        }
        
        protected List< Thread> _runningThreads = new List<Thread>();
        public abstract void AddResult(IResult result);
        
        public virtual void ProcessRequest(IRequest request) {
            _runningThreads.Clear();
            foreach (ICommand command in _commands) {
                command.SetRequest(request);
                command.AssignParent(this);
                Thread thread = new Thread(new ThreadStart(((IRunnable)command).Run));
                _runningThreads.Add(thread);
                thread.Start();
            }
        }
        public void ProcessRequest(String impl, IRequest request) {
            _runningThreads.Clear();
            foreach (ICommand command in _commands) {
                if (command.Identifier == impl) {
                    command.SetRequest(request);
                    command.AssignParent(this);
                    Thread thread = new Thread(new ThreadStart(((IRunnable)command).Run));
                    _runningThreads.Add(thread);
                    thread.Start();
                    break;
                }
            }
        }
    }
    
    public class SynchronousParent : ParentBase, IEnumerable {
        private List< IResult> _results = new List<IResult>();
        
        public override void AddResult(IResult result) {
            lock (_results) {
                _results.Add(result);
            }
        }
        public IEnumerator GetEnumerator() {
            return _results.GetEnumerator();
        }
        public SynchronousParent() {
        }
        public override void ProcessRequest(IRequest request) {
            base.ProcessRequest(request);
            foreach (Thread thread in _runningThreads) {
                thread.Join();
            }
        }
    }
    
    public class AsynchronousParent<type> : ParentBase where type : class{
        private Queue< IResult> _results = new Queue<IResult>();
        
        public override void AddResult(IResult result) {
            Monitor.Enter( _results);
            _results.Enqueue( result);
            Monitor.Pulse( _results);
            Monitor.Exit( _results);
        }
        public type GetResult() {
            Monitor.Enter( _results);
            if( _results.Count > 0) {
                return _results.Dequeue() as type;
            }
            else {
                Monitor.Wait( _results, 15000);
                if( _results.Count > 0) {
                    return _results.Dequeue() as type;
                }
            }
            return null;
        }
        public AsynchronousParent() { }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?