Castle Windsor 实现依赖注入和APO

添加以下项目目录,Repository实现IRepository 接口,Service里面通过构造函数注入,实现Repository的实例化,ConsoleApp1里面通过构造函数注入,实现Service的实例化

namespace IRepository
{
    public interface ITestRepository
    {
        void Say();
    }
}

public class TestRepository : ITestRepository
    {
        public void Say()
        {
            Console.WriteLine("这是在TestRepository");
        }
 }

 public interface ITestService
    {
        void Say();
    }

public class TestService : ITestService
    {
        ITestRepository _testRepository;

        public TestService(ITestRepository testRepository)
        {
            _testRepository = testRepository;
        }

        public void Say()
        {
            _testRepository.Say();
            Console.WriteLine("这是在TestService");
        }
    }

 

通过Resolve TestIOCClass来实现Service的依赖注入

public class TestIOCClass
    {
        ITestService _testeService;
        public TestIOCClass(ITestService testeService)
        {
            _testeService = testeService;
        }

        public void Say()
        {
            _testeService.Say();
        }

    }

在ConsoleApp1 里面的Program里面实例化容器,并且注册所有的程序集,然后实例化TestIOCClass来进行实例化调用

var container = new WindsorContainer();
            container.Register(Classes.FromAssemblyNamed("IRepository")
                            .Where(type => type.Name.EndsWith("Repository")).WithServiceDefaultInterfaces().LifestyleTransient());
            container.Register(Classes.FromAssemblyNamed("Repository")
                            .Where(type => type.Name.EndsWith("Repository")).WithServiceDefaultInterfaces().LifestyleTransient());
            container.Register(Classes.FromAssemblyNamed("IService")
                            .Where(type => type.Name.EndsWith("Service")).WithServiceDefaultInterfaces().LifestyleTransient());
            container.Register(Classes.FromAssemblyNamed("Service")
                            .Where(type => type.Name.EndsWith("Service")).WithServiceDefaultInterfaces().LifestyleTransient());

            container.Register(Component.For<TestIOCClass>());

            var test = container.Resolve<TestIOCClass>();
            test.Say();

 

实现AOP 

创建一个拦截器,TestIntercept继承自IInterceptor

public class TestIntercept : IInterceptor
    {
        public void Intercept(IInvocation invocation)
        {
            Console.WriteLine("这是头部");
            invocation.Proceed();
            Console.WriteLine("这是尾部");

        }
    }

然后定义一个方法

 public class DataClass
    {
        public virtual void TestMethod()
        {
            Console.WriteLine("这是TestData");
        }
    }

 

在Program里面调用

ProxyGenerator generator = new ProxyGenerator();
                TestIntercept interceptor = new TestIntercept();
                var pg = generator.CreateClassProxy<DataClass>(interceptor);
                pg.TestMethod();

会发现会自动的在TestMethod前面和后面包裹了一段输出内容

 

转载于:https://www.cnblogs.com/notyourdog/p/10647234.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值