码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式-装饰器模式

时间:2020-02-05 12:02:12      阅读:89      评论:0      收藏:0      [点我收藏+]

标签:ndt   this   color   cal   class   led   b2b   bsp   tran   

 

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

Connection

技术图片
public sealed class SqlConnectionWrapper : IDbConnection
    {
        private Action<IDbCommand> trace = null;
        private IDbConnection connection = null;
        public SqlConnectionWrapper(IDbConnection connection, Action<IDbCommand> trace=null)
        {
            this.connection = connection;
            this.trace = trace;
        }

        public event Action<IDbCommand> 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<IDbCommand> trace = null;
        private IDbCommand command = null;
        public SqlCommandWrapper(IDbCommand command, Action<IDbCommand> 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

 

 

 

未完待续...

设计模式-装饰器模式

标签:ndt   this   color   cal   class   led   b2b   bsp   tran   

原文地址:https://www.cnblogs.com/fanfan-90/p/12262725.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!