IBatis.Net study notes eleven: Castle.DynamicProxy use

Further castle is a framework that includes a plurality of aspects of aop, ioc, orm the like, which may be implemented castle.dynamicproxy dynamic proxy functions, this is the basis for many of the frame. In the castle.dynamicproxy ibatis.net it is to use the dynamic operation to the database connections. Also used in other frameworks like nhibernet to this technology.
Now I look at a simple example of how to call to see in our code castle.dynamicproxy:
to have three general categories:
1, interface class:
a using System;
a using System.Collections.Generic;
a using System.Text;

namespace gspring.castletest
{
    public interface itest
    {
        string getname(string pre);
    }
}
2、实现类:
using system;
using system.collections.generic;
using system.text;

gspring.castletest namespace
{
    public class Test: ITest
    {
        public String getName (String pre)
        {
            return pre + ", gspring";
        }
    }
}
these two are normal interfaces and implementations
3, the proxy class:
the using System;
the using System .collections;
a using System.Reflection;
a using Castle.DynamicProxy;

namespace gspring.castletest
{
    /**//// <summary>
    /// summary description for daoproxy.
    /// </summary>
    public class interceptorproxy : iinterceptor
    {
         public object intercept(iinvocation invocation, params object[] arguments)
        {
            object result = null;

            // here can be connected to a database, log hit, exception handling, and other common operating permission determination
            result = invocation.proceed (arguments);

            return result;
        }

    }
}
The first class that implements the interface iinterceptor, then you can add our own logic in the intercept method

Then look at the call mode:
        proxygenerator proxygenerator new new proxygenerator = ();
        IInterceptor new new interceptorproxy Handler = ();
        type [] = {typeof the interfaces (ITest)};
        Test Test Test new new = ();
        ITest ITest = (proxygenerator.createproxy (in the interfaces, Handler, the Test) AS the ITest);
        String the Result = itest.getname ( "the Hello"); the last place a call, the actual execution will be the first intercept method interceptorproxy class.

Reproduced in: https: //www.cnblogs.com/zyfking/archive/2009/01/19/1378525.html

Guess you like

Origin blog.csdn.net/weixin_34205076/article/details/93251770