设计模式-装饰器模式

 

案例1:扩展Connection、Command对象,为增删改操作添加事件

Connection

public sealed class SqlConnectionWrapper : IDbConnection
    {
        private Action trace = null;
        private IDbConnection connection = null;
        public SqlConnectionWrapper(IDbConnection connection, Action trace=null)
        {
            this.connection = connection;
            this.trace = trace;
        }

        public event Action Trace
        {
            add { trace += value; }
            remove { trace -= value; }
        }

        public string ConnectionString { get => connection.ConnectionString; set => connection.ConnectionString = value; }

        public int ConnectionTimeout => connection.ConnectionTimeout;

        public string Database => connection.Database;

        public ConnectionState State => connection.State;

        public IDbTransaction BeginTransaction()
        {
            return connection.BeginTransaction();
        }

        public IDbTransaction BeginTransaction(IsolationLevel il)
        {
            return connection.BeginTransaction(il);
        }

        public void ChangeDatabase(string databaseName)
        {
            connection.ChangeDatabase(databaseName);
        }

        public void Close()
        {
            connection.Close();
        }

        public IDbCommand CreateCommand()
        {
            var cmd = connection.CreateCommand();
            return new SqlCommandWrapper(cmd, this.trace);
        }

        public void Dispose()
        {
            connection.Dispose();
        }

        public void Open()
        {
            connection.Open();
        }

        public override string ToString()
        {
            return this.connection.ConnectionString;
        }
    }
View Code

Command

public sealed class SqlCommandWrapper : IDbCommand
    {
        private Action trace = null;
        private IDbCommand command = null;
        public SqlCommandWrapper(IDbCommand command, Action trace = null)
        {
            this.command = command;
            this.trace = trace;
        }



        public string CommandText { get => command.CommandText; set => command.CommandText = value; }
        public int CommandTimeout { get => command.CommandTimeout; set => command.CommandTimeout = value; }
        public CommandType CommandType { get => command.CommandType; set => command.CommandType = value; }
        public IDbConnection Connection { get => command.Connection; set => command.Connection = value; }

        public IDataParameterCollection Parameters { get => command.Parameters; }

        public IDbTransaction Transaction { get => command.Transaction; set => command.Transaction = value; }
        public UpdateRowSource UpdatedRowSource { get => command.UpdatedRowSource; set => command.UpdatedRowSource = value; }

        public void Cancel()
        {
            command.Cancel();
        }

        public IDbDataParameter CreateParameter()
        {
            return command.CreateParameter();
        }

        public void Dispose()
        {
            command.Dispose();
        }

        public int ExecuteNonQuery()
        {
            trace?.Invoke(this);
            return command.ExecuteNonQuery();
        }

        public IDataReader ExecuteReader()
        {
            trace?.Invoke(this);
            return command.ExecuteReader();
        }

        public IDataReader ExecuteReader(CommandBehavior behavior)
        {
            trace?.Invoke(this);
            return command.ExecuteReader(behavior);
        }

        public object ExecuteScalar()
        {
            trace?.Invoke(this);
            return command.ExecuteScalar();
        }
        public void Prepare()
        {
            command.Prepare();
        }
    }
View Code

 

 

 

未完待续...

你可能感兴趣的:(设计模式-装饰器模式)