我可以ninject实例postsharp方面? [英] Can I make ninject instantiate postsharp aspects?

查看:154
本文介绍了我可以ninject实例postsharp方面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net的WebAPI项目,我是能够做到在控制器中的方法

I have an asp.net webapi project and I was to be able to do some aspect orientated programming on the methods within the controllers

[Audit("Getting all foos")]
public IEnumerable<Foo> GetAll()
{
    return _fooService.GetAll();
}

我创建了纵横/属性称为审计。我使用ninject创建对象,但在其他地方,但仅仅方面对自己创建自己。

The aspect/attribute I've created is called Audit. I'm using ninject for object creation but elsewhere but the aspects just create themselves on their own.

这会导致两个问题,首先,我不能注入到性能方面等都有方面中手动创建对象。

This causes two problems, first I can't inject properties into the aspect so have to manually create objects within the aspect.

其次,因为我不能覆盖创作审计方面这么一个实际的测试,而不是嘲笑之一期间创建的创建行为,它是打破我的单元测试。

Secondly it is breaking my unit tests because I'm not able to override the creation behaviour of the creation of the audit aspect so a real one is created during the tests instead of a mocked one.

我没有很多国际奥委会的经验或ninject所以说实话我得到自己有点混淆。

I don't have a lot of experience with IOC or ninject so to be honest I'm getting myself a bit mixed up.

有没有办法通过ninject?给力这些方面创造

我使用的是最新版本的所有库是否有帮助。

I'm using the latest version of all libraries if that helps.

我也碰到 Ninject拦截,但似乎更像是一个替代品postsharp尽管我可能是很错了。

I also came across Ninject Intercept but that seems more like an alternative to postsharp though I could be very wrong about that.

推荐答案

Postsharp修改您的code后编译。有没有办法Ninject可以通过依赖这种方法。但也有一些事情可以做:

Postsharp modifies your code post compilation. There is no way Ninject can pass dependencies with this approach. But there are some things you can do:


  1. 使用Ninject截取为需要依赖方面。这种工作方式不同。 Ninject将动态创建装饰类前和调用后调用你的方面。这样,它可以传递方面的一些依赖。

  1. Use Ninject Interception for aspects that need dependencies. This works differently. Ninject will create dynamically a decorator class and calls your aspect before and after the call. That way it can pass the aspect some dependencies.

方面通常用于横切关注点。在这些情况下,它是完全合理的创建依赖环境上下文。这是一个静态类,它只是提供了一种依赖如一个IAuditLog。

Aspects are usually used for cross cutting concerns. In these situations it is perfectly reasonable to create an ambient context for the dependencies. This is a static class that provides just one kind of dependency e.g. an IAuditLog.

public static class AuditLogProvider
{
    public static AuditLog 
    {
        get { return autitLog; }
        set {
                if (this.auditLog != null) throw new InvlaidOperationExcpetion("Audit is already configured"); 
                this.auditLog = value; 
            }
    }
}

在你的引导程序,你现在可以初始化环境方面:

In your bootstrapper you can now initialize that ambient context:

AuditLogProvider.AutitLog = kernel.Get<IAuditLog>();

这篇关于我可以ninject实例postsharp方面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆