Aman
Aman

Reputation: 1382

MVC3 Transactions Counters

I want to count the number of transactions which retrieves some reports from database (SQL Server). I am using EF4.1 for database interactions and MVC3 for client.

Could anyone suggest best way to do and place (in controller or in reposity service etc).

I want to restrict the user to maximum 100 transactions per hour. After this limit I would lock the user and log-out as well. Thanks in advance!!

Upvotes: 1

Views: 194

Answers (2)

Roy Dictus
Roy Dictus

Reputation: 33139

This is clearly business logic, so the "action" (decision) needs to be in your business logic layer.

In the DB, keep a table with consumed quota per day and update that table every time the user performs a counted action (such as retrieving a particular report or writing to the database). That is, your business logic contains instructions to update the consumed quota counter per such operation.

Add validation logic in your business layer that reads the consumed quota for the user, and only validate (i.e., allow) if the daily quota for that user has not been reached.

Clean up the quota every day if necessary, but you may want to keep it for accounting purposes.

Upvotes: 1

Brad Christie
Brad Christie

Reputation: 101614

I would probably look in to a ActionFilterAttribute that can be applied globally (across all controllers) in the MvcApplication class, RegisterGlobalFilters method. Something like:

public class FilterTrafficAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        // Log and track visitor count for the hour here, and decide if they
        // should proceed on or not.
        // You can also change the ActionResult of the request by altering the
        // filterContext.Result.
    }
}

Then in the MvcApplication:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandlerErrorAttribute());

    // add this line:
    filters.Add(new FilterTrafficAttribute());
}

Then you're insuring it's applied to all calls, and have a lot of handling ability. There are other overrides you may be interested in with ActionFilterAttribute

Upvotes: 1

Related Questions