Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save complex object to session ASP .NET CORE 2.0

Tags:

asp.net-core

I am quite new to ASP .NET core, so please help. I would like to avoid database round trip for ASP .NET core application. I have functionality to dynamically add columns in datagrid. Columns settings (visibility, enable, width, caption) are stored in DB.

So I would like to store List<,PersonColumns> on server only for actual session. But I am not able to do this. I already use JsonConvert methods to serialize and deserialize objects to/from session. This works for List<,Int32> or objects with simple properties, but not for complex object with nested properties.

My object I want to store to session looks like this:

[Serializable]
  public class PersonColumns
  {
    public Int64 PersonId { get; set; }
    List<ViewPersonColumns> PersonCols { get; set; }

    public PersonColumns(Int64 personId)
    {
      this.PersonId = personId;
    }

    public void LoadPersonColumns(dbContext dbContext)
    {
      LoadPersonColumns(dbContext, null);
    }

    public void LoadPersonColumns(dbContext dbContext, string code)
    {
      PersonCols = ViewPersonColumns.GetPersonColumns(dbContext, code, PersonId);
    } 

    public static List<ViewPersonColumns> GetFormViewColumns(SatisDbContext dbContext, string code, Int64 formId, string viewName, Int64  personId)
    {
      var columns = ViewPersonColumns.GetPersonColumns(dbContext, code, personId);

      return columns.Where(p => p.FormId == formId && p.ObjectName == viewName).ToList();
    }


  }

I would like to ask also if my approach is not bad to save the list of 600 records to session? Is it better to access DB and load columns each time user wants to display the grid?

Any advice appreciated

Thanks

EDIT: I have tested to store in session List<,ViewPersonColumns> and it is correctly saved. When I save object where the List<,ViewPersonColumns> is property, then only built-in types are saved, List property is null.

The object I want to save in session

[Serializable]
  public class UserManagement
  {
    public String PersonUserName { get; set; }
    public Int64 PersonId { get; set; }
    public List<ViewPersonColumns> PersonColumns { get; set; } //not saved to session??

    public UserManagement() { }

    public UserManagement(DbContext dbContext, string userName)
    {
      var person = dbContext.Person.Single(p => p.UserName == userName);

      PersonUserName = person.UserName;
      PersonId = person.Id;
    }

    /*public void PrepareUserData(DbContext dbContext)
    {
      LoadPersonColumns(dbContext);
    }*/

    public void LoadPersonColumns(DbContext dbContext)
    {
      LoadPersonColumns(dbContext, null);
    }

    public void LoadPersonColumns(DbContext dbContext, string code)
    {
      PersonColumns = ViewPersonColumns.GetPersonColumns(dbContext, code, PersonId);
    }

    public List<ViewPersonColumns> GetFormViewColumns(Int64 formId, string viewName)
    {
      if (PersonColumns == null)
        return null;

      return PersonColumns.Where(p => p.FormId == formId && p.ObjectName == viewName).ToList();
    }

  }

Save columns to the session

UserManagement userManagement = new UserManagement(_context, user.UserName);
userManagement.LoadPersonColumns(_context);
HttpContext.Session.SetObject("ActualPersonContext", userManagement);
HttpContext.Session.SetObject("ActualPersonColumns", userManagement.PersonColumns);

Load columns from the session

//userManagement build-in types are set. The PersonColumns is null - not correct
UserManagement userManagement = session.GetObject<UserManagement>("ActualPersonContext");
//The cols is filled from session with 600 records - correct
List<ViewPersonColumns> cols = session.GetObject<List<ViewPersonColumns>>("ActualPersonColumns");
like image 508
MartinM Avatar asked Dec 17 '22 21:12

MartinM


1 Answers

Use list for each column is better than use database. you can't create and store sessions in .net core like .net framework 4.0

Try Like this

Startup.cs

 public void ConfigureServices(IServiceCollection services)
        {
            //services.AddDbContext<GeneralDBContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));        

            services.AddMvc().AddSessionStateTempDataProvider();
            services.AddSession();
        }

Common/SessionExtensions.cs

sing Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace IMAPApplication.Common
{
    public static class SessionExtensions
    {
        public static T GetComplexData<T>(this ISession session, string key)
        {
            var data = session.GetString(key);
            if (data == null)
            {
                return default(T);
            }
            return JsonConvert.DeserializeObject<T>(data);
        }

        public static void SetComplexData(this ISession session, string key, object value)
        {
            session.SetString(key, JsonConvert.SerializeObject(value));
        }
    }
}

Usage

  • ==> Create Session*

public IActionResult Login([FromBody]LoginViewModel model)
        {               
          LoggedUserVM user = GetUserDataById(model.userId);   
          
          //Create Session with complex object   
          HttpContext.Session.SetComplexData("loggerUser", user);         
                
          return Json(new { status = result.Status, message = result.Message });              

        }
  • ==> Get Session data*

 public IActionResult Index()
        {
            //Get Session data     
            LoggedUserVM loggedUser = HttpContext.Session.GetComplexData<LoggedUserVM>("loggerUser");                         
        }

Hope this is helpful. Good luck.

like image 86
Isanka Thalagala Avatar answered Jan 23 '23 04:01

Isanka Thalagala