General fetch function by PK

Posts   
 
    
lgege
User
Posts: 27
Joined: 01-Sep-2005
# Posted on: 11-Nov-2005 16:36:42   

Hi,

I want to create a general function that can fetch a single entity from all tables by PK. I would like to pass into the function the following parameters:tablename, PK field name and PK value. I would like to use adapter.

Function FetchEntityByPK(TableName, PKFieldName, PKValue) 'code to fetch the entity with [PKValue] from the [TableName]

End Function

Is this possible?

Thanks.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 12-Nov-2005 04:27:20   

This is fairly similar to the fetchentity that is available with adapter. Are you wanting to just pass the string of the table name and then use that to specify which factory to use and then return an entity of that type that was fetched using the specified parameters?

lgege
User
Posts: 27
Joined: 01-Sep-2005
# Posted on: 14-Nov-2005 19:19:40   

Yes, that's what i want. I am not sure whether it is necessary for me to pass in the PK name? Also how to instantiate the factory?

bclubb wrote:

This is fairly similar to the fetchentity that is available with adapter. Are you wanting to just pass the string of the table name and then use that to specify which factory to use and then return an entity of that type that was fetched using the specified parameters?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 15-Nov-2005 11:01:17   

You need to pass in a factory OR an instance of the entity you want to fetch. You can achieve an instance of the entity to fetch by doing:


string entityName = "BoardMemberEntity";
// create enum
EntityType typeOfEntity = (EntityType)Enum.Parse(typeof(EntityType), entityName, false);
IEntity2 boardMember = GeneralEntityFactory.Create(typeOfEntity);

something like that. Now, you then set boardMember's PK fields, by simply using: boardMember.SetNewFieldValue(name, value);

and you can pass it to adapter.FetchEntity(boardMember)

Frans Bouma | Lead developer LLBLGen Pro
lgege
User
Posts: 27
Joined: 01-Sep-2005
# Posted on: 17-Nov-2005 23:12:14   

I tried and it is not working as expected. Here is the code:

Private Enum RefTables
    CustomerAlertRef = 1
    CustomerCallbackReasonRef = 2
    DiscountReasonRef = 3
    EmployeeTitleRef = 4
    EmployeeTypeRef = 5
    MarketingTypeRef = 6
    OfficeTypeRef = 7
    OrderActionReasonRef = 8
    OtherPaymentTypeRef = 9
    RoomTypeRef = 10
    SalutationRef = 11
    SfducategoryRef = 12
    TruckEventTypeRef = 13
End Enum

    //bind Enum RefTables to a dropdownlist

    //Dim RefTable as String = dropdownlsit.selectedvalue

    Dim EntType As RefTables = [Enum].Parse(GetType(RefTables), RefTable, False)
    Dim ent As IEntity2 = GeneralEntityFactory.Create(EntType)

During debugging, the value for variables are: RefTable "CustomerAlertRef" EntType CustomerAlertRef ent {Sears.UltraCare.DAL.EntityClasses.AddressTypeRefEntity}

Can you let me know what is wrong?

Thanks.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Nov-2005 11:31:55   

please step into GeneralEntityFactory.Create and report what happens.

Frans Bouma | Lead developer LLBLGen Pro
lgege
User
Posts: 27
Joined: 01-Sep-2005
# Posted on: 18-Nov-2005 14:46:05   

Ok, I found the problem. I didn't know the integer value in enumeration should correpond to the table position among all tables.

Thanks.

lgege
User
Posts: 27
Joined: 01-Sep-2005
# Posted on: 18-Nov-2005 15:22:59   

Sorry to bother again. But I am somewhat uncomfortable with this. If some tables are added or removed, I always have to go into the codes to change the integer values in enumeration. Is there some other way to do so?

I have some thought. We already have a function that could return EntityCollection once you pass in TableName as parameter. Is there a way to get EntityType from EntityCollection. Thus I could use Dim RefEnt As IEntity2 = GeneralEntityFactory.Create(EntityType)

Thanks a lot.

sparmar2000 avatar
Posts: 341
Joined: 30-Nov-2003
# Posted on: 19-Nov-2005 17:33:02   

There is an easier way. There is an Enum called EntityType

BL.EntityType

Then there are also an Enum for each entity see this thread which is just a few posps below.

http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=3309