Coder Social home page Coder Social logo

expressaop's Introduction

Express AOP

install from nuget Build status

NuGet Install

PM> Install-Package ExpressAOP

### Test case You can define a fillter like this:

    public class MyFilter : AOPFilterAttribute
    {
        private readonly string _key;
        public MyFilter(String key)
        {
            _key = key;
        }
        protected override void Executing(IProcesser processer)
        {
            Setting.List.Add("Executing-" + _key);
        }
        protected override void Executed(IProcesser processer)
        {
            Setting.List.Add("Executed-" + _key);
        }
    }

eg: You have a class like following:

    class MyClass {
        public void Foo1()
        {
        }
    }

And you want the filter is working on the Method. You have 2 methods to use the filter;

1.Use the ContextBoundObject baseclass

    [AOPProxy]// Use the AOP
    class MyModel2 : ContextBoundObject// baseclass
    {
        [MyFilter(":filter")]//AFilter
        public void Foo1()
        {
            Setting.List.Add("method");
        }
    }

I write a test case for this way:

        [TestMethod]
        public void ClassContextBoundObject()
        {
            var o = new MyModel2();
            o.Foo1();
            Assert.AreEqual(new[]
                                {
                                    "Executing-:filter",
                                    "method",
                                    "Executed-:filter"
                                }.GetStr(), Setting.List.GetStr());
        }

It'll be working well.

2.Use the Interface

You must impl a interface for your class

    public interface IMyModel
    {
        void Foo(int i);
    }
 public class MyModel : IMyModel
    {
        private readonly string _key;

        public MyModel(string key)
        {
            _key = key;
        }

        [MyFilter("method:filter")]
        public void Foo(int i)
        {
            Setting.List.Add("method-foo-" + _key);
        }
    }

And use this code:

  var proxy = new AOPProxy<IMyModel>(new MyModel("1"));
  var obj = proxy.GetObject();
  obj.Foo(1);

It'll be running.

3.Other

You can use Filter like this:

    [MyFilter("I:filter")]
    public interface IMyModel
    {

        [MyFilter("I-Method1:filter")]
        [MyFilter("I-Method2:filter")]
        void Foo(int i);

        string MyProperty
        {
            [MyFilter("I-prop-get:filter")]
            get;
        }
    }
    [MyFilter("class:filter")]
    public class MyModel : IMyModel
    {
        private readonly string _key;

        public MyModel(string key)
        {
            _key = key;
        }

        [MyFilter("method:filter")]
        public void Foo(int i)
        {
            Setting.List.Add("method-foo-" + _key);
        }


        public string MyProperty
        {
            [MyFilter("prop-get:filter")]
            get
            {
                Setting.List.Add("prop-ins-" + _key);
                return _key;
            }

        }
    }

Execute Order is

Order Description
1 The Filter on Interface
2 The Filter on Class
3 The Filter on Interface's Method or Property
4 The Filter on Class 's Method or Property
5 Method or Property

References

http://en.wikipedia.org/wiki/Aspect-oriented_programming

http://weibo.com/chsword

http://lnkd.in/b6vqP_c

expressaop's People

Contributors

chsword avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

chteam

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.