Coder Social home page Coder Social logo

Comments (17)

hishamco avatar hishamco commented on May 23, 2024 1

You're right @Eilon the object must be serializable, but what i mean is to introduce something like what @davidfowl suggest or we can do something like this

if(obj.GetType().IsSerializable)
{
...
}

from session.

muratg avatar muratg commented on May 23, 2024 1

I don't believe we will be implementing something like this in V1. Putting it in backlog for now.

from session.

OlsonDev avatar OlsonDev commented on May 23, 2024 1

I find it strange you picked on decimal; ushort seems a much better candidate to pick on. 😛

var price = HttpContext.Session.Get<decimal>("one-time-discounted-price-offered");

Either way, I listed all of the primitive types as I figured the code would be pretty simple.

Personally, I'd much prefer to use Get<Int32>() over GetInt32(); better/faster IntelliSense/ReSharper refactoring options.

from session.

Eilon avatar Eilon commented on May 23, 2024

The problem with having fully generic type support is that there is no standard way to convert from an object to a completely arbitrary type. The critical thing to keep in mind with session state is that all data must be serializable because session state might (and often is) on a remote server. This means that complex object graphs or "live" objects cannot be used in session state.

If there are some specific types that you think should be added, we can certainly look at those (e.g. bool).

from session.

davidfowl avatar davidfowl commented on May 23, 2024

We could also look into allowing a generic overload that requires an implementation of an interface, ISerializer to go from T -> byte[]. Somebody can plug in protobuf or bond

from session.

hishamco avatar hishamco commented on May 23, 2024

@davidfowl @Eilon If this may helps the developer to use various types such as bool, DataTable and so on, I can start to plug protobuf or bond

from session.

davidfowl avatar davidfowl commented on May 23, 2024

/cc @Tratcher @lodejard

from session.

lodejard avatar lodejard commented on May 23, 2024

+1 for @davidfowl 's earlier suggestion. I like the idea of a set of byte[] <-> T formatters the web dev can add explicitly.

Though TBH, I think you would probably add that system as a different service which takes the ISessionCollection as a dependency. Separation of concerns between session provider and the data formatting, ya know.

from session.

hishamco avatar hishamco commented on May 23, 2024

@davidfowl @lodejard i think it will be nice if we can provide getting and setting the primitive data types out of the box and let web devs to implement ISerializer for custom and complex objects

from session.

hishamco avatar hishamco commented on May 23, 2024

This is a prototype for ProtobufSessionFormatter that implements ISessionFormatter which is suggested here aspnet/HttpAbstractions#286

public class ProtobufSessionFormatter<T> : ISessionFormatter<T>
    {
        public ProtobufSessionFormatter()
        {
            if (typeof(T).IsValueType==false)
            {
                var members = GetPublicMembers();
                RuntimeTypeModel.Default.Add(typeof(T), false).Add(members);
            }
        }

        private string[] GetPublicMembers()
        {
            return typeof(T).GetMembers(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly).Where(m=>m.MemberType==MemberTypes.Field||m.MemberType==MemberTypes.Property).Select(m => m.Name).ToArray();
        }

        public T Deserialize(byte[] value)
        {
            T obj;
            using (var ms = new MemoryStream(value))
            {
                obj= Serializer.Deserialize<T>(ms);
            }
            return obj;
        }

        public byte[] Serialize(T value)
        {
            byte[] data;
            using (var ms = new MemoryStream())
            {
                Serializer.Serialize(ms, value);
                data = ms.ToArray();
            }
            return data;
        }
    }

from session.

hishamco avatar hishamco commented on May 23, 2024

Thanks @muratg, hope to be in consideration in the future

from session.

OlsonDev avatar OlsonDev commented on May 23, 2024

Understandable that you wouldn't implement everything in V1 but how about support for the "easy" ones?

// Not valid syntax, just conveying a list of types to support
public T Get<T>(string key) where T : 
    byte,
    sbyte,
    int,
    uint,
    short,
    ushort,
    long,
    ulong,
    float,
    double,
    char,
    bool,
    decimal,
    System.Guid,
    string {
    // basically struct, without the ability to have nested reference types (like an actual struct type)
    // ...
}

public T Get<T>(string key) where T : struct, IConvertible {
    if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enum");
    // ...
}

Int32 & String alone is pretty limited. 😕

from session.

Eilon avatar Eilon commented on May 23, 2024

We're not necessarily opposed to adding at least some of these, but I doubt a lot of apps store a decimal as a standalone value in session state 😄

from session.

304NotModified avatar 304NotModified commented on May 23, 2024

Well an (serializable) object storing in session would be nice. (and also bools etc)

from session.

hishamco avatar hishamco commented on May 23, 2024

@muratg is the closing mean that this will not support?!! It was in the backlog

from session.

muratg avatar muratg commented on May 23, 2024

Bringing back for re-triage.

from session.

aspnet-hello avatar aspnet-hello commented on May 23, 2024

This issue was moved to dotnet/aspnetcore#2407

from session.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.