Code Monkey home page Code Monkey logo

aopunityconsole's Introduction

Aspect Oriented Programming with Unity container

Aspect Oriented Programming (AOP) with Unity container

AOP concepts can be implemented with Unity container via dynamic proxy and C# method attributes. The AOP usage looks like this:

//this is just an example
interface MyInterface
{
    [MyAspect]
    void MyMethod();    
}

Every time an implemenation of MyMethod() is called the attribute MyAspect is executed.

The following steps need to be implemented.

1. Create class attribute

Attribute must be inherited from HandlerAttribute class and a call class that implements ICallHandler interface. Method IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext) is where the attribute is executed.

class DumpAttribute : HandlerAttribute
{
    public override ICallHandler CreateHandler(IUnityContainer container)
    {
        return new DumpCallHandler();
    }
}

class DumpCallHandler : ICallHandler
{
    public int Order { get; set; }

    public DumpCallHandler() : this(0) { }

    public DumpCallHandler(int order)
    {
        Order = order;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
    {        
        var result = getNext().Invoke(input, getNext);        
        return result;
    }
} 

2. Enable TransparentProxyInterceptor

Enable TransparentProxyInterceptor in Unity configuration.

container.RegisterType<IRepository<Customer>, Repository<Customer>>();
container.Configure<Interception>().SetInterceptorFor<IRepository<Customer>>(new TransparentProxyInterceptor());

2. Define target interface

Define a target interface that will be augumented with an aspect and use attribute on methods.

public interface IRepository<T>
{
    [Dump]
    void Add(T entity);    
}

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.