Coder Social home page Coder Social logo

emit.proxy's Introduction

Emit.Proxy

基于EMIT的动态AOP代理

 class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var generator = new ProxyGenerator();
            //多实例拦截器
            generator.CreateInstanse<Service>(new TransactionInterceptor()).DelUser();
            generator.CreateInstanse<Service>(new TransactionInterceptor()).UpdateUser();
            generator.CreateInstanse<Service>(new TransactionInterceptor()).ToDay();
        }
    }

    public class Service
    {
        public virtual void UpdateUser()
        {
            Console.WriteLine("用户已修改");
        }
        public virtual void DelUser()
        {
            Console.WriteLine("用户已删除");
        }
        public virtual DateTime ToDay()
        {
            return DateTime.Now;
        }
    }
    public class TransactionInterceptor : IInterceptor
    {
        private DbConnection Connection { get; set; }
        public DbTransaction Transaction { get; set; }
        public void Intercept(IInvocation invocation)
        {
            try
            {
                if (Connection == null)
                {
                    Connection = new MySql.Data.MySqlClient.MySqlConnection("server=127.0.0.1;user id=root;password=1024;database=test;pooling=true;");
                    Connection.Open();
                    Console.WriteLine("事务已开启");
                    Transaction = Connection.BeginTransaction();
                }
                //执行目标方法
                invocation.Proceed();
                Transaction?.Commit();
                Console.WriteLine("事务提交了...");
            }
            catch (Exception e)
            {
                Console.WriteLine("异常:{0}",e.Message);
                //回滚事务
                Transaction?.Rollback();
                Console.WriteLine("事务回滚了");
                //记录日志:NLog
                Console.WriteLine();
            }
            finally
            {
                Connection?.Close();
            }
        }
    }
    ```

emit.proxy's People

Contributors

soul-soft avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.