PropertyInfoFactory and Silverlight

PropertyInfoFactory and Silverlight

Old forum URL: forums.lhotka.net/forums/t/9236.aspx


sunlu posted on Tuesday, July 20, 2010

PropertyInfoFactory and Silverlight

I am trying to Create Custom FieldData Classes in CSLA according to Jason Bock's article.  It works great in WPF, however it does NOT work in Silverlight.

Jason mentions

The point to take away here is you must set the Factory property somewhere at the "beginning" of your host's initialization. That will differ from host to host (WCF, WPF, WinService, etc.) - for the purposes of this example setting it when the test class initializes is sufficient. Keep in mind that doing this in every test won't work as expected because managed properties are resolved in the class's static constructor, so once that's done for a given class, it will be using whatever factory was set at that time. Changing the factory after that point is meaningless.

So, even If I initialize Factory property in App like

        public App()
        {
            Csla.Core.FieldManager.PropertyInfoFactory.Factory = new PropertyInformationFactory();       
            this.Startup += this.Application_Startup;
            this.Exit += this.Application_Exit;
            this.UnhandledException += this.Application_UnhandledException;

            InitializeComponent();
        }

objects are still created with Csla.Core.FieldManager.FieldData<> class as opposed to FieldDataUsingOriginalValueViaHashCode<> or  FieldDataUsingOriginalValueViaDuplicate<> classes(Actually, when Debugging, I can see that it is correctly set in the SERVER side, however, once DataPortal<T>.FetchCompleted(), Csla SL initializes the object using FieldData<> class).

I believe that the problem stems from the code

#if SILVERLIGHT
          _factory = new DefaultPropertyInfoFactory();
#else
          var typeName = System.Configuration.ConfigurationManager.AppSettings["CslaPropertyInfoFactory"];
          if (string.IsNullOrEmpty(typeName))
          {
            _factory = new DefaultPropertyInfoFactory();
          }
          else
          {
            var type = Type.GetType(typeName);
            _factory = (Csla.Core.IPropertyInfoFactory)Activator.CreateInstance(type);
          }
#endif
in Source\Csla.Silverlight\Core\FieldManager\PropertyInfoFactory.cs.

Is there a way this behavior changed?

Thanks.

RockfordLhotka replied on Tuesday, July 20, 2010

I'll add this to the bug list.

marthac replied on Tuesday, August 10, 2010

I've been able to work around this bug by creating my factory just before using it for the first time
Not sure if this is the correct way to code this but it works for me...

     public class MyBusinessBase<T> : Csla.BusinessBase<T> where T : MyBusinessBase<T>
    {
        protected static bool _factoryLoaded = false;
 

        :
        :

        protected static IMyPropertyInfoFactory GetFactory()
        {
            if (_factoryLoaded == false)
            {   // kluge to get this working until SL bug is fixed (Issue ID: 803 - PropertyInfoFactory can't be configured on Silverlight)
                Csla.Core.FieldManager.PropertyInfoFactory.Factory = (Csla.Core.IPropertyInfoFactory)Activator.CreateInstance(typeof(MyPropertyInfoFactory));
                _factoryLoaded = true;
            }
            return (IMyPropertyInfoFactory)Csla.Core.FieldManager.PropertyInfoFactory.Factory;
        }

         // all you need to do is call GetFactory() in your RegisterProperty functions

    }

 

 

Copyright (c) Marimer LLC