Add custom property to entity

Posts   
 
    
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 17-Jan-2012 02:06:14   

Hello,

I searched the forum and found that I can add a new property to an entity by going to the bottom of the file and adding it in the special section for user's code. The posting said that the code would be preserved.

My situation is that DevExpress' Silverlight Scheduler control does not permit us to easily change the color of an appointment in the Schedule at runtime. We need to change the color of the appointment based on how close it is to arriving.

Today or in the past: RED Tomorrow or tomorrow + 7 days: YELLOW All other dates in the future: GREEN

Currently, the colorId must be loaded from the database/entity being added to the Scheduler. This means that it is a static value. They have a worka-around for dynamic data, but it is ugly. I love most of their controls, but this one needs work on the Silverlight platform. Anyway, I was wondering whether it would be possible to add a property to our entity that would calculate the correct colorId. The new property would need to be visible to bound controls and appear like it came from a field in the database. It is a read-only value.


Proposed entity property:

        /// <summary>
        /// Determine the LabelId's color based on the current DateTime.
        /// </summary>
        public int LabelId 
        {
            get
            {
                // Assumes that we can access other fields in the current entity.
                const int ExpiredStatus = 1;    // Red
                const int WarningStatus = 10;   // Yellow
                const int NormalStatus = 3;  //(3 = Green, 0 = White)

                // The color code to return to the Scheduler control.
                int labelId = 0;

                // Get the appointment's DateTime in UTC and set its HH:MM:SS.sss to zero (start of the day).
                // The DateTime coming from the database is converted to local time by the Scheduler, so
                // what we need to compare will be local time here.  ExpirationDateTimeInUTC is a field
                // in this entity.
                DateTime apptStart = new DateTime(
                    ExpirationDateTimeInUTC
                    ExpirationDateTimeInUTC.Year, 
                    ExpirationDateTimeInUTC.Month, 
                    ExpirationDateTimeInUTC.Day, 0, 0, 0);

                // Current Date set to 00:00:00.000
                DateTime orgCurrent = DateTime.Now;
                DateTime current = new DateTime(orgCurrent.Year, orgCurrent.Month, orgCurrent.Day, 0, 0, 0);

                if (apptStart < current)
                { 
                    // Expired, use red background
                    labelId = ExpiredStatus;                    
                }
                else if (apptStart >= current &&
                         apptStart <= current.AddDays(9))
                {
                    // Warning period, use yellow background
                    labelId = 0;
                }
                else if (e.ViewInfo.Appointment != null)
                {
                    // Normal, use green background
                    labelId = NormalStatus;
                }

                return labelId;
            }
        }

Can you please tell me the best way to implement this concept so that the bound control would have no idea of our "deception"?

I edited an entity and found where I can create a property and give it a value. But, I need to insert code to dynamically set the value.

Thanks for your help!

Mike

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 17-Jan-2012 05:29:11   

Additionally to the USER_CODE_REGION you can add all code you want in a entity partial class. Let say:

namespace <yourRootNamespace>.EntityClasses
{
     public class AppointmentEntity
     {
         // ... your own code for the entity: private fields, public properties, methods, etc.
     }
}

I don't quite understand what you mean by "But, I need to insert code to dynamically set the value". Shouldn't the change of the ExpirationDateTimeInUTC field would automatically change the value of LabelId?

David Elizondo | LLBLGen Support Team
ABOH
User
Posts: 148
Joined: 06-Sep-2011
# Posted on: 17-Jan-2012 06:12:32   

Hi Daelmo,

Great, great, GrEaT, suggestion! I didn't think about using a partial class and adding our property there. I cut-n-pasted the code that I posted, compiled, et voila! It worked! Thank you for nudging me in the correct direction!

Mike