CSLA .NET

From Rockford Lhotka's Expert C# 2005 and VB 2005 Business Objects books

Welcome to CSLA .NET Sign in | Join | Help
in Search

Is there a way to access a list of managed properties (3.5 Managed Properties)

Last post 08-25-2008, 11:54 AM by bt1. 1 replies.
Sort Posts: Previous Next
  •  08-21-2008, 9:31 AM 25627

    Is there a way to access a list of managed properties (3.5 Managed Properties)

    I am trying to handle DateTimeOffset? (Nullable) conversion from UTC to local in a custom class that is subclassing BusinessBase<T>.

    public abstract class CustomBusinessBase<T> : Csla.BusinessBase<T> where T : CustomBusinessBase<T>

    I would like to have a pair of methods that convert DateTimeOffset to and from local time.

    protected TimeSpan ToUniversalTime()

    protected TimeSpan ToLocalTime();

    Each of these methods needs to loop through all properties that are of type DateTimeOffset?

    Dates in the database are UTC.  On the portal fetch the properties are loaded up with dates that are UTC (offset 0)

    DateTimeOffset? dtoTemp;

    dtoTemp = drSafe.GetDateTimeNullable(Index);

    if (dtoTemp != null)

    {

    dtoTemp = new DateTimeOffset(dtoTemp.Value.Ticks, new TimeSpan(0));

    }

    LoadProperty<DateTimeOffset?>(DateProperty, dtoTemp);

    In the factory methods  I would like to call ToLocalTime()  to convert the datetime to the local offset (which would be done locally on a smart client)

    So the ToLocalTime would need to get/loop a list of properties of type DateTimeOffset? and adjust the offset.  I have code generated the ToLocalTime and put it into my business methods and a sample of the current code is show below.  However, I think I should be able to move this in to a class that subclasses BusinessBase<T> and make ToLocalTime dynamic by getting a list of all managed properties and if the property's type is DateTimeOffset the adjust to Local.

    protected override TimeSpan ToLocalTime()

    {

       TimeSpan offset = new TimeSpan(0);

       this.ValidationRules.SuppressRuleChecking = true;

       DateTimeOffset? dtoTempDatetime = ReadProperty<DateTimeOffset?>(DatetimeProperty);

       if (dtoTempDatetime != null)

       {

          dtoTempDatetime = dtoTempDatetime.Value.ToLocalTime();

          //Must Clear current value because dates shifted to different offsets (timezones)

          //compare equal and CSLA will not update fields whos new value is equal to current value.

          //ex. "5/10/2007 10:33:52 AM -05:00" == "5/10/2007 3:33:52 PM +00:00"

          LoadProperty<DateTimeOffset?>(DatetimeProperty, null);

          LoadProperty<DateTimeOffset?>(DatetimeProperty, dtoTempDatetime);

          offset = dtoTempDatetime.Value.Offset;

    }

    this.ValidationRules.SuppressRuleChecking = false;

    return offset;

    }   

     

     

    Private static PropertyInfo<DateTimeOffset?> DatetimeProperty = new PropertyInfo<DateTimeOffset?>("Datetime");
    public string Datetime
    {
      get { return GetProperty<DateTimeOffset?>(DatetimeProperty ); }
      set { SetProperty<DateTimeOffset?>(DatetimeProperty , value); }
    }

  •  08-25-2008, 11:54 AM 25721 in reply to 25627

    Re: Is there a way to access a list of managed properties (3.5 Managed Properties)

    Ok, I think I found a way to do this.  Does anyone see a problem with the use of FieldManager.GetRegisteredProperties(); in the method below?

     

    protected TimeSpan ToLocalTime()

    {

        TimeSpan offset = new TimeSpan(0);

        this.ValidationRules.SuppressRuleChecking = true;

     

        //Get list of all Managed properties

        List<Csla.Core.IPropertyInfo> l = FieldManager.GetRegisteredProperties();

     

        if (l != null)

        {

            foreach (Csla.Core.IPropertyInfo item in l)

            {

                Csla.PropertyInfo<DateTimeOffset?> propInfoNull;

                Csla.PropertyInfo<DateTimeOffset> propInfo;

     

                propInfoNull = item as Csla.PropertyInfo<DateTimeOffset?>;

                propInfo = item as Csla.PropertyInfo<DateTimeOffset>;

     

                //If the PropertyInfo type is a DateTimeOffset? (nullable)

                if (propInfoNull != null)

                {

                    if (FieldManager.FieldExists(item))

                    {

                        DateTimeOffset? dtoNullable = null;

                        dtoNullable = ReadProperty<DateTimeOffset?>(propInfoNull);

                        if (dtoNullable.HasValue)

                        {

                            dtoNullable = dtoNullable.Value.ToLocalTime();

                            //Must Clear current value because dates shifted to different timezones

                            //compare equal and CSLA will not update fields whos new value is equal to current value.

                            //ex. "5/10/2007 10:33:52 AM -05:00" == "5/10/2007 3:33:52 PM +00:00"

                            LoadProperty<DateTimeOffset?>(propInfoNull, null);

                            LoadProperty<DateTimeOffset?>(propInfoNull, dtoNullable);

                            offset = dtoNullable.Value.Offset;

                        }

                    }

                }

                //If the PropertyInfo type is a DateTimeOffset

                if (propInfo != null)

                {

                    if (FieldManager.FieldExists(item))

                    {

                        DateTimeOffset dto;

                        dto = ReadProperty<DateTimeOffset>(propInfo);

                        dto = dto.ToLocalTime();

                        //Must Clear current value because dates shifted to different timezones

                        //compare equal and CSLA will not update fields whos new value is equal to current value.

                        //ex. "5/10/2007 10:33:52 AM -05:00" == "5/10/2007 3:33:52 PM +00:00"

                        LoadProperty<DateTimeOffset>(propInfo, new DateTimeOffset(1970, 1, 1, 0, 0, 0, 1, new TimeSpan(0)));

                        LoadProperty<DateTimeOffset>(propInfo, dto);

                        offset = dto.Offset;

                    }

                }

     

            }

        }

     

        ValidationRules.SuppressRuleChecking = false;

        return offset;

    }

     

View as RSS news feed in XML

Please contact Magenic for your .NET consulting and CSLA .NET mentoring needs.
Please consider making a donation to help support the ongoing development of CSLA .NET.

Make donation through PayPal - it's fast, free and secure!
Why donate?
Powered by Community Server, by Telligent Systems