Created
September 10, 2018 05:15
-
-
Save vlapenkov/e9122673548d990092971c391a76b4e2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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))); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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