Skip to content

Instantly share code, notes, and snippets.

@vlapenkov
Created September 10, 2018 05:15
Show Gist options
  • Select an option

  • Save vlapenkov/e9122673548d990092971c391a76b4e2 to your computer and use it in GitHub Desktop.

Select an option

Save vlapenkov/e9122673548d990092971c391a76b4e2 to your computer and use it in GitHub Desktop.
public abstract class BaseController<T> : Controller where T : BaseController<T>
{
private DbCachingService _dbCs;
protected DbCachingService DbCS => _dbCs?? (_dbCs= (DbCachingService)HttpContext.RequestServices.GetService(typeof (DbCachingService)));
private ApplicationDbContext _dbContext;
protected ApplicationDbContext DbContext => _dbContext ?? (_dbContext = (ApplicationDbContext)HttpContext.RequestServices.GetService(typeof(ApplicationDbContext)));
}
public class SessionManageController : BaseController<SessionManageController>
{
const string SessionKeyName = "_Name";
const string SessionKeyYearsMember = "_YearsMember";
const string SessionKeyDate = "_Date";
private readonly ApplicationDbContext _dbContext;
public SessionManageController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
public IActionResult Index()
{
if (base.DbContext == _dbContext) return Content("objects are the same");
var name = HttpContext.Session.GetString(SessionKeyName);
var yearsMember = HttpContext.Session.GetInt32(SessionKeyYearsMember);
// return Content($"Name: , Membership years: \"{yearsMember}\"");
return Content($"Name: {base.DbCS.FirstPerson.Name}, Membership years: \"{yearsMember}\"");
}
public IActionResult SetSessionVariables()
{
HttpContext.Session.SetString(SessionKeyName, "Rick");
HttpContext.Session.SetInt32(SessionKeyYearsMember, 3);
return RedirectToAction("Index");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment