Best way to do this?

Posts   
 
    
Posts: 15
Joined: 01-Oct-2003
# Posted on: 25-Nov-2003 12:20:14   

Is there a more efficient to do the following, basically just an add/update:-

Dim moduleSetting As NewsModuleSettingEntity = New NewsModuleSettingEntity

If (moduleSetting.FetchUsingPK(ModuleId)) Then
    moduleSetting.CategoryId = CType(drpCategories.SelectedItem.Value, Integer)
    moduleSetting.MaxNumber = CType(drpNumber.SelectedItem.Value, Integer)
    moduleSetting.Age = CType(drpDateRange.SelectedItem.Value, Integer)
    moduleSetting.Save()
Else
    Dim newModuleSetting As NewsModuleSettingEntity = New NewsModuleSettingEntity

    newModuleSetting.ModuleId = ModuleId
    newModuleSetting.CategoryId = CType(drpCategories.SelectedItem.Value, Integer)
    newModuleSetting.MaxNumber = CType(drpNumber.SelectedItem.Value, Integer)
    newModuleSetting.Age = CType(drpDateRange.SelectedItem.Value, Integer)

    newModuleSetting.Save()
End If

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 25-Nov-2003 13:35:07   

Dim moduleSetting As NewsModuleSettingEntity = New NewsModuleSettingEntity(ModuleId)

If (moduleSetting.Fields.State <> EntityState.Fetched) Then
    moduleSetting.ModuleId = ModuleId
End If
moduleSetting.CategoryId = CType(drpCategories.SelectedItem.Value, Integer)
moduleSetting.MaxNumber = CType(drpNumber.SelectedItem.Value, Integer)
moduleSetting.Age = CType(drpDateRange.SelectedItem.Value, Integer)
moduleSetting.Save()

This should do it I think. Basicly, the two code paths have a lot in common, in fact, the 'it exists' version is a subset of the 'new' path. So, in these situations you check if you have a new one on your hands: if so, do the actions for the new version, and continue with the rest of the codepath which is the codepath for 'it exists'.

Sort of like: Dim foo As MyObject = Nothing; If Bar=SomeValue Then ' initialize foo foo = New MyObject End If

This is thus without the 'Else' branche with the = Nothing set. Seems nothing, but the philosophy is the same: simply initialize the variable and only set it to a different value when that situation occurs.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 15
Joined: 01-Oct-2003
# Posted on: 25-Nov-2003 22:42:49   

Otis wrote:


Dim moduleSetting As NewsModuleSettingEntity = New NewsModuleSettingEntity(ModuleId)

If (moduleSetting.Fields.State <> EntityState.Fetched) Then
    moduleSetting.ModuleId = ModuleId
End If
moduleSetting.CategoryId = CType(drpCategories.SelectedItem.Value, Integer)
moduleSetting.MaxNumber = CType(drpNumber.SelectedItem.Value, Integer)
moduleSetting.Age = CType(drpDateRange.SelectedItem.Value, Integer)
moduleSetting.Save()

This should do it I think. Basicly, the two code paths have a lot in common, in fact, the 'it exists' version is a subset of the 'new' path. So, in these situations you check if you have a new one on your hands: if so, do the actions for the new version, and continue with the rest of the codepath which is the codepath for 'it exists'.

Sort of like: Dim foo As MyObject = Nothing; If Bar=SomeValue Then ' initialize foo foo = New MyObject End If

This is thus without the 'Else' branche with the = Nothing set. Seems nothing, but the philosophy is the same: simply initialize the variable and only set it to a different value when that situation occurs.

Thanks Frans, I had something similar, but I couldn't find a way to test if the entity had been retrieved or not, other then the FetchUsingPK method, and that wouldn't allow me to set the moduleID if it returned false.

Thanks!

Posts: 15
Joined: 01-Oct-2003
# Posted on: 26-Nov-2003 10:26:47   

smcculloch-home wrote:

Otis wrote:


Dim moduleSetting As NewsModuleSettingEntity = New NewsModuleSettingEntity(ModuleId)

If (moduleSetting.Fields.State <> EntityState.Fetched) Then
    moduleSetting.ModuleId = ModuleId
End If
moduleSetting.CategoryId = CType(drpCategories.SelectedItem.Value, Integer)
moduleSetting.MaxNumber = CType(drpNumber.SelectedItem.Value, Integer)
moduleSetting.Age = CType(drpDateRange.SelectedItem.Value, Integer)
moduleSetting.Save()

This should do it I think. Basicly, the two code paths have a lot in common, in fact, the 'it exists' version is a subset of the 'new' path. So, in these situations you check if you have a new one on your hands: if so, do the actions for the new version, and continue with the rest of the codepath which is the codepath for 'it exists'.

Sort of like: Dim foo As MyObject = Nothing; If Bar=SomeValue Then ' initialize foo foo = New MyObject End If

This is thus without the 'Else' branche with the = Nothing set. Seems nothing, but the philosophy is the same: simply initialize the variable and only set it to a different value when that situation occurs.

Thanks Frans, I had something similar, but I couldn't find a way to test if the entity had been retrieved or not, other then the FetchUsingPK method, and that wouldn't allow me to set the moduleID if it returned false.

Thanks!

Hi Frans,

No matter what I seem to put in I always get moduleSetting.Fields.State equalling EntityState.Fetched

 Dim moduleSetting As NewsModuleSettingEntity = New NewsModuleSettingEntity(ModuleID)

Any ideas?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Nov-2003 12:05:07   

Weird, since this is the code:


if(dataSource.Read())
{
    // sets current value directly, bypassing NewValue. If the resultset doesn't contain the requested column, the underlying persistent
    // storage has been changed compared to the generated code and the code should be re-generated. A column mismatch will result in an exception.
    ReadRowIntoFields(dataSource, rowDestination);

    // set state to fetched
    rowDestination.State = EntityState.Fetched;
}

This means that IF there are rows (according to the docs of DataReader.Read(), if there are rows, it returns true), it will set the state to fetched, otherwise it won't. THe state is set to New in the Fields constructor, so it should be New if there are no rows read. (dataSource in the snippet above is a datareader).

disappointed

I'll look into this, and check if there are situations where the MS docs do not tell the truth and Read() will return true even if there are no rows.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Nov-2003 12:20:11   

Doing this: CustomerEntity c = new CustomerEntity("AAAAA");

on northwind, gives in the debugger for Fields.State: New, after executing this line, as expected. So the ID you pass in does exist in the database, the fetch does succeed.

I don't know how old the runtime library (ORMSupportClasses dll) is you're using or the generated code, 1.5 months ago, an issue was fixed related to this, in older versions it didn't set the state correctly when there wasn't a row, in all situations. (it was done in generated code, which wasn't correct. )

Your fetch routine in the entity should look like this:


(VB.NET, copied from Order)
Private Function Fetch(ByVal orderID As System.Int32) As Boolean
    Dim dao As OrderDAO = DAOFactory.CreateOrderDAO()

    ' Load EntityFields of Order
    MyBase.Fields = dao.FetchOrder(MyBase.Transaction, orderID)

    Dim fetchResult As Boolean = False
    If MyBase.Fields.State = EntityState.Fetched Then
        MyBase.IsNew = False
        fetchResult = True
    End If

    Return fetchResult
End Function

Can you check whether your DAL code is old (like 2 months old so it was generated with old templates) and/or the ormsupportclasses lib is old?

Frans Bouma | Lead developer LLBLGen Pro
Posts: 15
Joined: 01-Oct-2003
# Posted on: 26-Nov-2003 12:32:40   

Otis wrote:

Doing this: CustomerEntity c = new CustomerEntity("AAAAA");

on northwind, gives in the debugger for Fields.State: New, after executing this line, as expected. So the ID you pass in does exist in the database, the fetch does succeed.

I don't know how old the runtime library (ORMSupportClasses dll) is you're using or the generated code, 1.5 months ago, an issue was fixed related to this, in older versions it didn't set the state correctly when there wasn't a row, in all situations. (it was done in generated code, which wasn't correct. )

Your fetch routine in the entity should look like this:


(VB.NET, copied from Order)
Private Function Fetch(ByVal orderID As System.Int32) As Boolean
    Dim dao As OrderDAO = DAOFactory.CreateOrderDAO()

    ' Load EntityFields of Order
    MyBase.Fields = dao.FetchOrder(MyBase.Transaction, orderID)

    Dim fetchResult As Boolean = False
    If MyBase.Fields.State = EntityState.Fetched Then
        MyBase.IsNew = False
        fetchResult = True
    End If

    Return fetchResult
End Function

Can you check whether your DAL code is old (like 2 months old so it was generated with old templates) and/or the ormsupportclasses lib is old?

I haven't updated in like 2 months -- i'll do that now .. and get back simple_smile

Posts: 15
Joined: 01-Oct-2003
# Posted on: 26-Nov-2003 12:43:36   

smcculloch-home wrote:

Otis wrote:

Doing this: CustomerEntity c = new CustomerEntity("AAAAA");

on northwind, gives in the debugger for Fields.State: New, after executing this line, as expected. So the ID you pass in does exist in the database, the fetch does succeed.

I don't know how old the runtime library (ORMSupportClasses dll) is you're using or the generated code, 1.5 months ago, an issue was fixed related to this, in older versions it didn't set the state correctly when there wasn't a row, in all situations. (it was done in generated code, which wasn't correct. )

Your fetch routine in the entity should look like this:


(VB.NET, copied from Order)
Private Function Fetch(ByVal orderID As System.Int32) As Boolean
    Dim dao As OrderDAO = DAOFactory.CreateOrderDAO()

    ' Load EntityFields of Order
    MyBase.Fields = dao.FetchOrder(MyBase.Transaction, orderID)

    Dim fetchResult As Boolean = False
    If MyBase.Fields.State = EntityState.Fetched Then
        MyBase.IsNew = False
        fetchResult = True
    End If

    Return fetchResult
End Function

Can you check whether your DAL code is old (like 2 months old so it was generated with old templates) and/or the ormsupportclasses lib is old?

I haven't updated in like 2 months -- i'll do that now .. and get back simple_smile

Works perfect -- my fault for not updating the software -- sorry!

What is the easiest way to update the software? I removed the existing app and reinstalled, is there a simpler way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 26-Nov-2003 12:52:22   

The simplest way is to uninstall and reinstall, if you have waited for a long time. When there is just a template update for example, you can also just update the templates. After re-installing, which should take a few seconds, you can simply regenerate the code, recompile your dal and you're set.

We have a release often policy because we think it is best for customers to have a fix when it is available, however because the application consists of so many components in different files/filetypes, it is sometimes hard to keep track of whether you have upgraded all the components to the latest version. With each fix of a component we update the installer as well, so you can just use that if you haven't updated for some time.

We're now after the initial introduction phase so the initial startup probs of every new application are finally going away so we don't have to patch that often, or just in small area's.

There is no need to patch with every version, if you're not experiencing a bug that is fixed, you can wait till later of course, but the choice is there, that's why we release patches when they're available instead of releasing 1 patch in 3 months, like other companies do. wink

Frans Bouma | Lead developer LLBLGen Pro