BL question

Posts   
 
    
TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 22-Feb-2006 18:16:11   

I'm used to doing this kind of thing with SQL so bare with me as I try and unlearn all my bad habits. I feel like I'm really close to the light bulb finally going off but still have a little ways to go.

I'm using the demo version of LLBLGen Pro and have been toying with the selfserving two class configuration.

This is what I need. I have a table (time off) that tracks all the days the employees are off.

I have an entity based on this table and would like to add some additional properties to it.

I've have a class that returns a timeoffcollection based on a date parameter I supply. So I pull a list of all the people who are out of the office for a given day. What I would like is to add a property that returns info based on the parameter entered.

As an example: Calculate the days between StartDate and the date parameter given to the function.

Is this something that I should add to the timeoff entity and if so how do I go about passing the parameter down to the entity?

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 23-Feb-2006 04:02:26   

These sound like properties that you could add in the EntityClass inside the // __LLBLGENPRO_USER_CODE_REGION_START CustomEntityCode // __LLBLGENPRO_USER_CODE_REGION_END section. Let us know if you have any problems or more explanation.

TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 23-Feb-2006 20:31:50   

That's how I did it but I'm really not happy with my solution. I sure there is a better way to do it I just don't know what that is.

This is what I've added to my TimeOff Entity class


       Private _reportDate As DateTime


       Public Property ReportDate() As Date
           Get
                Return _reportDate
            End Get
            Set(ByVal value As Date)
                _reportDate = value
            End Set
        End Property


        Public ReadOnly Property DailyStatus() As String
            Get
                Dim sTime, eTime As DateTime

                sTime = _reportDate.AddHours(8)
                eTime = _reportDate.AddHours(17)

                If StartDate <= sTime And EndDate >= eTime Then
                    Return "All Day"
                ElseIf StartDate <= sTime Then
                    Return "8 AM to " + EndDate.ToShortTimeString
                ElseIf EndDate >= eTime Then
                   Return StartDate.ToShortTimeString + " to 5 PM"
                Else
                    Return  StartDate.ToShortTimeString + " to " + EndDate.ToShortTimeString
                End If

            End Get
        End Property

And I've added this to my TimeOffCollection Class


       Public Function GetTimeOffByDay(ByVal myDate As Date) As TimeOffCollection

            Dim pe As IPredicateExpression = New PredicateExpression
            pe.Add(PredicateFactory.CompareValue(TimeOffFieldIndex.StartDate, _
                ComparisonOperator.LessEqual, myDate.AddDays(1).AddMilliseconds(-1)))
            pe.AddWithAnd(PredicateFactory.CompareValue(TimeOffFieldIndex.EndDate, _
                ComparisonOperator.GreaterThan, myDate))

            Dim TimeOff As New TimeOffCollection
            TimeOff.GetMulti(pe)

            Dim aTimeOff As TimeOffEntity
            For Each aTimeOff In TimeOff
                aTimeOff.ReportDate = myDate
            Next

            Return TimeOff
        End Function

I can then bind this to a gridview to display the name and daily status.

This works but, like I said, I'm sure it's really the wrong way to go about doing it.

My initial thought is that DailyStatus should be a function (not a property) that takes the date as a parameter and returns a string. Doing it that way I wasn't able to bind it to my gridview.

I also don't like the idea of adding the reportdate property since it really has nothing to do with my entity.

I realize these are more OO type questions than LLBLGen but I've yet to find a good OO programming forum.

Thanks, Warren

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 23-Feb-2006 22:32:14   

Personally, I dont like putting business logic into entity classes. I would create a business logic class to do the work.

  1. Create your custom property DailyStatus in your entity
  2. Remove ReportDate if you dont want it in the entity
  3. Create a HelperClass with static methods that do whatever you want, i.e.

create a class called TimeOffHelper with a static / shared fuction called CalculateStatus(DateTime dateToUse, TimeOffEntity currentEntity)

and put this code inside of the CalculateStatus function:

            Dim sTime, eTime As DateTime

            sTime =dateToUse.AddHours(8) 
            eTime = dateToUse.AddHours(17)

            If currentEntity.StartDate <= sTime And currentEntity.EndDate >= eTime Then
                Return "All Day"
            ElseIf currentEntity.StartDate <= sTime Then
                Return "8 AM to " + currentEntity.EndDate.ToShortTimeString
            ElseIf currentEntity.EndDate >= eTime Then
             Return currentEntity.StartDate.ToShortTimeString + " to 5 PM"
            Else
                Return currentEntity.StartDate.ToShortTimeString + " to " + EndDate.ToShortTimeString
            End If
  1. In the function GetTimeOffByDay replace this line : aTimeOff.ReportDate = myDate

with this line: aTimeOff.DailyStatus = TimeOffHelper.CalculateStatus(myDate, aTimeOff)

TogasPoon
User
Posts: 42
Joined: 09-Feb-2006
# Posted on: 24-Feb-2006 18:23:49   

Thanks,

That really clears things up for me.

What would be the best way for me to return that collection as a datatable? I've tried the GetMultiAsDataTable but that doesn't have my DailyStatus field.

Devildog74
User
Posts: 719
Joined: 04-Feb-2004
# Posted on: 24-Feb-2006 20:01:55   

I dont know if Self Servicing or the GetMultiAsDataTable deals with exposing custom properties in the resultant data table, but in C#, you could always build one manually like this:

function DataTable CollectionToDataTable(EntityCollection collection) { DataTable table = new DataTable();

for each (SomeEntity item in collection)
{
    table.Rows.Add(new object[]{item.Field1, item.Field2, item.Field3});
}

return table;

}