kristofferahl/FluentSecurity — Issue #16: Policy violation handlers and conventions
Author: kristofferahl
Created: 2012-03-09 21:03:04+00:00
State: open
Labels: feedback-needed

It's that time again when I ask for your help. This time it's about **improving the experience of handling violations**.

At the time of writing (v.1.4.0) **FluentSecurity allows you to specify a violation handler for policy using a simple naming convention**. An example would be that MyPolicy maps to MyPolicyViolationHandler. The only requirement is that you register your handlers in your IoC-container (or factory method). While this works well when you are already using an IoC-container it doesn't feel as natural when you are not.

**For version 2.0 we are looking at adding better and cleaner support for policy violation handlers.** This is what is being spiked at the moment:

https://github.com/kristofferahl/FluentSecurity/tree/spike-policy-violation-handlers

## Option 1
A new (default) naming convention for specifying a default violation handler will be shipped.
By adding a DefaultPolicyViolationHandler to your container or factory, that instance will be resolved whenever a violation of a policy occurs and there is no better matching handler available.

## Option 2

The ability to set a default policy violation handler.
```configuration.DefaultPolicyViolationHandlerIs();```
or
```configuration.DefaultPolicyViolationHandlerIs(() => new MyDefaultPolicyViolationHandler());```

## Option 3

The ability to fluently specify what violationhandler is used when a violation occurs:

```C#
configuration.Conventions(conventions =>
{
conventions.ViolationsOf().IsHandledBy();
conventions.ViolationsOf().IsHandledBy(() => new MyCustomPolicyViolationHandler());
conventions.ViolationsMatching(exception => "predicate goes here").IsHandledBy();

conventions.Add(new MyCustomConvention());
});
```

As you can see in the examples above you have a few options. You can either map a policy to a violation handler as is done on the first two lines. On the third line we can use a predicate and map all matches to a violation handler of your choice.

The last line simply enables you to create your own custom conventions and use it to find a suitable violation handler.
This is what the convention interface looks like:

```C#
public interface IPolicyViolationHandlerConvention : IConvention
{
IPolicyViolationHandler GetHandlerFor(PolicyViolationException exception);
}
```

## Feedback needed!

**So hit me with your feedback, whatever it is!!**

* What do you like?
* What don't you like?
* How could the syntax be improved?
* Do you see a better way of doing this?