I have a table to Log visitor Count Like :
public class VisitorLog:BaseEntity
{
public int Id { get; set; }
public DateTime VisitedOn { get; set; }
public string LocationIP { get; set; }
public string BrowserName { get; set; }
public VisitorLog()
{
VisitedOn = DateTime.Now;
}
}
and in Global.asax
I have this :
protected void Application_BeginRequest(object sender,EventArgs e)
{
if (new HttpRequestWrapper(Request).IsAjaxRequest()) return;
foreach (var task in ApplicationObjectFactory.Container.GetAllInstances<IRunOnEachRequest>())
{
task.Execute();
}
}
and Implementation in Service Layer is like below :
public bool Add(string locationId, string browserName)
{
if (string.IsNullOrEmpty(locationId) || string.IsNullOrEmpty(browserName)) throw new ArgumentNullException("Visitor Log");
return Add(new AddVisitLogViewModel { BrowserName=browserName,LocationIP=locationId.ToLower()});
}
public bool Add(AddVisitLogViewModel visitorLogViewModel)
{
if (visitorLogViewModel == null) throw new ArgumentNullException("visitorLog");
if (Any(row => row.LocationIP == visitorLogViewModel.LocationIP && DbFunctions.TruncateTime(row.VisitedOn) ==DbFunctions.TruncateTime(DateTime.Now))) return false;
var model = Mapper.Map<VisitorLog>(visitorLogViewModel);
AddItem(model);
return true;
}
In Application_BeginRequest
I have to check request is Ajax or Action Call
and others , is there a better way to check this or Improve these code ?