I am attempting to build a system that allows users to perform certain actions, but their account must have a specific 'Ticket' per time they do it. For instance, suppose they wish to create a Product
, they would need a CreateProductTicket
.
I could simply do this with some 'if' statements, sure, but I want to try a bit more of a robust solution. My structure looks something like this...
interface ITicket<T> where T : ITicketable
{
}
My basic goal is to build an Attribute, perhaps like the following..
public class TicketRequiredAttribute : Attribute
{
public TicketRequiredAttribute(ITicket<T> ticket)
{
if(ticket == null)
return;
}
}
And to be able to decorate Controller or Repository Actions with this. So like ...
ProductsControlller
[TicketRequired(CreateProductTicket)]
public ActionResult CreateProduct(Product product)
{
// ... **I am unsure how to tell if TicketRequired was true or not**
}
Problem 1
I'm not familiar enough with attributes to know how to tell if TicketRequired was 'met' or not. Can anyone enlighten me on this?
Problem 2
The problem I am running into is with database querying. I want to be able to check the user (IMembershipRepository
has a GetUser
method), but I'm not entirely certain how to do that through an attribute.
Using Castle.Windsor
, I have my Dependency Injection set up to inject repositories into controllers. I suppose I could pass the IMembershipRepository
through the TicketRequired
constructor, but I have a feeling that will become very messy - and extremely unstable. Is there a more logical way to approach this?
ActionFilters
utilize myWindsor
Dependency Injection! Thank you so much, this has me on the right track. I still don't know how I'll handle this on the Repository Level, but I'll figure something out. – Masculine