Make DeviceDetection a concrete dependency of DemoController:
public class DemoController : Controller
{
private readonly ICommonOperationsRepository _commonRepo;
private readonly DeviceDetection dd;
public DemoController (
ICommonOperationsRepository commonRepo,
DeviceDetection dd)
{
_commonRepo = commonRepo;
this.dd = dd;
}
public ActionResult Default()
{
var model = new DemoModel();
try
{
this.dd.DetectDevice();
model.ListTopListing.AddRange(_commonRepo.GetListings());
}
catch (Exception ex)
{
ExceptionHandler objErr = new ExceptionHandler(ex, "DemoController .Default()\n Exception : " + ex.Message);
objErr.LogException();
}
return View(model);
}
}
This should enable you to create an instance of DemoController without relying on the Request property:
var sut = new DemoController(someStupRepository, new DeviceDetection("foo"));
You can do this in e.g. a unit test.
When you compose DemoController in your application, you pass in request.ServerVariables["HTTP_X_REWRITE_URL"].ToString() to DeviceDetection. You can get the request variable from the requestContext argument of CreateController.