Coder Social home page Coder Social logo

Comments (7)

rockfordlhotka avatar rockfordlhotka commented on September 26, 2024

This is because MobileFormatter has no concept of the NonSerialized attribute, nor any way to flag a property so it is not serialized.

Right now your workaround is to use a managed property, not a private backing field, and allow it to serialize.

from cslaforum.

JacoJordaan avatar JacoJordaan commented on September 26, 2024

Thanks, that explains it. Although, I am a bit confused how the normal serialization for saving of object then works which is also using the MobileFormatter?

from cslaforum.

rockfordlhotka avatar rockfordlhotka commented on September 26, 2024

No, you are right - this isn't a MobileFormatter thing at all.

The problem is this line that calls LoadProperty.

Or more accurately a limitation of the overloads of LoadProperty in the ObjectFactory class:

https://github.com/MarimerLLC/csla/blob/master/Source/Csla.Shared/Server/ObjectFactory.cs#L120

Your property is truly read-only - it has no setter, and it uses a private backing field, so the LoadProperty method has absolutely no way to set the value of the property.

I do think this is related somewhat to the limitation of MobileFormatter where we have no way to mark a property as "nonserialized". No way to indicate that the property should be ignored.

The workaround thus far has been that such a property shouldn't be managed at all. If there's no registered IPropertyInfo for a property it is ignored by pretty much everything (except BinaryFormatter and NDCS, which follow their own rules).

from cslaforum.

JacoJordaan avatar JacoJordaan commented on September 26, 2024

Does this mean that with the MobileFormatter that this property will be serialized when doing a normal save operation? Or is that determined by the OnGetState and OnSetState methods for private fields?

I just want to make sure that there is no unnecessary data serialized and at the same time want to have the csla property info fields for binding support.

from cslaforum.

rockfordlhotka avatar rockfordlhotka commented on September 26, 2024

@JacoJordaan regarding serialization, I'm pretty confident that the private backing field is not serialized unless you explicitly implement the onget/onset state overrides and manually move the field value through the serialization stream.

This code establishes that the City value is not cloned.

using Csla;
using System;

namespace ConsoleApp2
{
  class Program
  {
    static void Main(string[] args)
    {
      var obj1 = DataPortal.Create<PersonEdit>();
      obj1.Name = "Rocky";
      obj1.City = "Eden Prairie";
      var obj2 = obj1.Clone();
      Console.WriteLine($"clone Name {obj2.Name}");
      Console.WriteLine($"clone City {obj2.City}");

      Console.ReadLine();
    }
  }

  [Serializable]
  public class PersonEdit : BusinessBase<PersonEdit>
  {
    public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(nameof(Name));
    public string Name
    {
      get => GetProperty(NameProperty);
      set => SetProperty(NameProperty, value);
    }

    public static readonly PropertyInfo<string> CityProperty = RegisterProperty<string>(nameof(City), RelationshipTypes.PrivateField);
    [NonSerialized]
    private string _city = CityProperty.DefaultValue;
    public string City
    {
      get => GetProperty(CityProperty, _city);
      set => _city = value;
    }
  }
}

from cslaforum.

rockfordlhotka avatar rockfordlhotka commented on September 26, 2024

Back to the original question, I think maybe the answer is for GraphMerger to handle properties defined with RelationshipTypes.PrivateField differently.

Specifically, I can change the behavior to invoke the property setter directly. This would require you implement at least a private setter.

I think this is probably the right answer.

With this change the following code works

      var obj1 = DataPortal.Create<PersonEdit>("Rocky", "Eden Prairie");
      var obj2 = DataPortal.Create<PersonEdit>();
      new Csla.Core.GraphMerger().MergeGraph(obj2, obj1);
      Console.WriteLine($"clone Name {obj2.Name}");
      Console.WriteLine($"clone City {obj2.City}");

Resulting in this output

clone Name Rocky
clone City Eden Prairie

So we can see it did merge obj1 into obj2, including the City property.

This is with the City property declared like this:

    public static readonly PropertyInfo<string> CityProperty = RegisterProperty<string>(nameof(City), RelationshipTypes.PrivateField);
    [NonSerialized]
    private string _city = CityProperty.DefaultValue;
    public string City
    {
      get => GetProperty(CityProperty, _city);
      private set => _city = value;
    }

from cslaforum.

JacoJordaan avatar JacoJordaan commented on September 26, 2024

@rockfordlhotka Yes, this will resolve the issue.

Thank you for also clarifying the serialization question.

from cslaforum.

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.