Using FieldManager to Apply a set of (Templated) property values for existing BO

Using FieldManager to Apply a set of (Templated) property values for existing BO

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


Kevin Fairclough posted on Tuesday, November 17, 2009

Hi All

I have a use case in which one or more templates can be applied to a BO that will automatically set values of the BO with those of the template.  I have the template RO object that contains a list of properties and values stored currently as string values, so i'll need conversion too.

I ideally don't want to use reflection in my BO in order to set property values, I'm using ManagedFields can I use FieldManager in some way for this?

It is also likely that other BOs will require similar functionality in the future.

Has anyone done something similar?  A pointer to the best approach would be great!

TIA
Kevin

chrduf replied on Tuesday, November 17, 2009

Hi Kevin

 

We have the same requirement. Here’s what we did….

 

Create a new factory object in your BO

 

 1438         #region CreateFromTemplate

 1439 

 1440 #if !(SILVERLIGHT)

 1441         public static RequestEntity CreateFromTemplate(Guid newRequestId, Guid templateId, string contactId, string customerId, string projectId, bool clientPortal)

 1442         {

 1443             return DataPortal.Create<RequestEntity>(new CriteriaCreateFromTemplate(newRequestId, templateId, contactId, customerId, projectId, clientPortal));

 1444         }

 1445 #endif

 1446 

 1447         public static void CreateFromTemplateAsync(Guid newRequestId, Guid templateId, string contactId, string customerId, string projectId, bool clientPortal, EventHandler<DataPortalResult<RequestEntity>> handler)

 1448         {

 1449             DataPortal<RequestEntity> dp = new DataPortal<RequestEntity>();

 1450             dp.FetchCompleted += handler;

 1451             dp.BeginCreate(new CriteriaCreateFromTemplate(newRequestId, templateId, contactId, customerId, projectId, clientPortal));

 1452         }

 1453 

 1454         #endregion

 

 

 1919         private void DataPortal_Create(CriteriaCreateFromTemplate criteria)

 1920         {

 1921             // get your default settings from your template object

 1922             RequestTemplateEntity requestFrom = RequestTemplateEntity.GetForCreateFromTemplate(criteria);

 1923 

 1924             // use the template properties to initialize your BO

 1925             LoadProperty<short?>(QuantityProperty, requestFrom.Quantity);

 1926             LoadProperty<string>(DescriptionProperty, requestFrom.Description);

 1927 

 1928             ValidationRules.CheckRules();

 1929         }

 

 

From: Kevin Fairclough [mailto:cslanet@lhotka.net]
Sent: Tuesday, November 17, 2009 11:46 AM
To: chris.dufour@wigets.net
Subject: [CSLA .NET] Using FieldManager to Apply a set of (Templated) property values for existing BO

 

Hi All

I have a use case in which one or more templates can be applied to a BO that will automatically set values of the BO with those of the template.  I have the template RO object that contains a list of properties and values stored currently as string values, so i'll need conversion too.

I ideally don't want to use reflection in my BO in order to set property values, I'm using ManagedFields can I use FieldManager in some way for this?

It is also likely that other BOs will require similar functionality in the future.

Has anyone done something similar?  A pointer to the best approach would be great!

TIA
Kevin


Kevin Fairclough replied on Wednesday, November 18, 2009

Hi

Thanks for the reply.

Similar to our requirement but not quite the same.  We also need to apply many templates to an already fetched BO.  I would like to be able to use FieldManager to read and write properties and property values in a generic way.  Has anyone used FieldManager in this way?

Kevin

Pawz.777 replied on Thursday, November 19, 2009

This might help you.. in this instance I'm using a customized PropertyInfo to declare the database field name and use it to retrieve the correct fields for the property.. It would be easy enough to swap it around and match up the name of the property (field.Name) with the properties of your template...


            foreach (var field in FieldManager.GetRegisteredProperties())
            {
                if (field.GetType().GetGenericTypeDefinition() == typeof(CsPropertyInfo<>))
                {
                    var fieldProp = (IPropertyDatabase) field;
                    if (!FieldManager.FieldExists(fieldProp))
                    {
                            LoadProperty(field, dr.GetValue(fieldProp.DatabaseName));
                    }
                }
            }

Copyright (c) Marimer LLC