MarimerLLC / cslaforum

Discussion forum for CSLA .NET
https://cslanet.com
Other
31 stars 6 forks source link

CSLA 4.9.0 CommandBase Object serialization in .net core not working #735

Open AMT1990 opened 5 years ago

AMT1990 commented 5 years ago

Question I tried to execute below program in .net core with CSLA 4.9.0

[Serializable]
public class OrderStatus : CommandBase<OrderStatus>
{
    string _orderID = string.Empty;
    string _newStatus = "Data";
    public OrderStatus()
    { }
    private OrderStatus(string orderID, string newStatus)
    {
        _orderID = orderID;
        _newStatus = newStatus;
    }

    public static void SetStatus(string orderID, string newStatus)
    {
        var orderStatus = new OrderStatus(orderID, newStatus);

        DataPortal.Execute<OrderStatus>(orderStatus);
    }

    protected override void DataPortal_Execute()
    {
        string orderId;
        string newStatus;

        if (this._orderID != null)
            orderId = this._orderID;
        if (this._newStatus != null)
            newStatus = this._newStatus;
    }
}

To run the code sent values to OrderStatus.SetStatus("1", "new"); method. From SetStatus method provide and assign that values to global variables like _orderID and _newStatus i.e _orderID=1 and _newStatus="new".

Then run the below line of code DataPortal.Execute(orderStatus);

After execute it will goes to DataPortal_Execute() method But I am getting this._orderID and this._newStatus as null, but we already assign value earlier to global variables as _orderID=1 and _newStatus="new" That problem only in .net core but in .net framework its working fine. Please help with any solution for it.

Version and Platform CSLA version: 4.9.0 OS: Windows. Platform: .NET Core.

rockfordlhotka commented 5 years ago

This has become a common enough question that I've created an FAQ page to help answer.

https://github.com/MarimerLLC/csla/blob/master/docs/serialization.md

Your class is using private backing fields with MobileFormatter. You need to either use managed backing fields (recommended) or override OnGetState/OnSetState to get/set each field value in the serialization stream.

rockfordlhotka commented 5 years ago
    [Serializable]
    public class OrderStatus : CommandBase<OrderStatus>
    {
        public static readonly PropertyInfo<string> OrderIdProperty = RegisterProperty<string>(c => c.OrderId);
        public string OrderId
        {
            get { return ReadProperty(OrderIdProperty); }
            private set { LoadProperty(OrderIdProperty, value); }
        }

        public static readonly PropertyInfo<string> NewStatusProperty = RegisterProperty<string>(c => c.NewStatus);
        public string NewStatus
        {
            get { return ReadProperty(NewStatusProperty); }
            private set { LoadProperty(NewStatusProperty, value); }
        }

        public OrderStatus()
        { }
        private OrderStatus(string orderID, string newStatus)
        {
            OrderId = orderID;
            NewStatus = newStatus;
        }

        public static void SetStatus(string orderId, string newStatus)
        {
            var orderStatus = new OrderStatus(orderId, newStatus);

            DataPortal.Execute<OrderStatus>(orderStatus);
        }

        protected override void DataPortal_Execute()
        {
            string orderId;
            string newStatus;

            if (OrderId != null)
                orderId = OrderId;
            if (NewStatus != null)
                newStatus = NewStatus;
        }
    }