Fetch new entity value from the database

Posts   
 
    
Developer
User
Posts: 58
Joined: 05-May-2004
# Posted on: 23-Nov-2006 00:21:04   

Hi,

I am using LLBLGen Pro v2.0 Final (23 Oct 2006) adapter model. I have created instance of ProductEntity class, and Fetch id=1.

How to load another ID in the same instance, as I do not want to create another instance. For example:


            
            ProductEntity product = new ProductEntity();
            using(DataAccessAdapter adapter = new DataAccessAdapter())
            {
                product.ProductId = 1;
                adapter.FetchEntity(product);
                System.Console.WriteLine("ProductID:{0}, Name:{1}", product.ProductId, product.ProductName);
                //Print: ProductID:1, Name:Chai


                product.ProductId = 2;
                adapter.FetchEntity(product);
                System.Console.WriteLine("ProductID:{0}, Name:{1}", product.ProductId, product.ProductName);
                //Print: ProductID:1, Name:Chai ??? 
                //How to load ProductID:2
            }
            

Many thanks.

Regards.

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 23-Nov-2006 02:27:35   

If ProductId is a PK then you could do something similar to this.


            ProductEntity product;
            using(DataAccessAdapter adapter = new DataAccessAdapter())
            {
                product =new ProductEntity(1);
                adapter.FetchEntity(product);
                System.Console.WriteLine("ProductID:{0}, Name:{1}", product.ProductId, product.ProductName);
                //Print: ProductID:1, Name:Chai


                product = new ProductEntity(2);
                adapter.FetchEntity(product);
                System.Console.WriteLine("ProductID:{0}, Name:{1}", product.ProductId, product.ProductName);
                //Print: ProductID:1, Name:Chai ???
                //How to load ProductID:2
            }

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 23-Nov-2006 10:28:55   

The reason why your initial code doesn't work is because you change a PK of a fetched entity and it now doesn't know anymore which value to use for fetching: the new one (which isn't persisted yet) or the value read from the db (the original one). As it knows the db one existed it uses the db value, so the original value (1). this thus means that you can't use the code you wrote initially. If you truly want to keep the entity instance, you should use product.Fields[0].ForcedCurrentValueWrite(2, 2);

though that's not recommended, just create another entity instance.

Frans Bouma | Lead developer LLBLGen Pro
Developer
User
Posts: 58
Joined: 05-May-2004
# Posted on: 24-Nov-2006 08:09:54   

Hi,

Thanks for your response. As you said, ForcedCurrentValueWrite is not recommended. Can you please suggest better architecture than following? I do not want to create all properties, functionality of the datalayer again. so I have designed BusinessLayer inherits from LLBLEntity classes (entity, collection, factory classes). e.g:

namespace business.EntityClasses
{
    public class CountryEntity : data.EntityClasses.CountryEntity
   {

     // All classes

    }
}

I have inherited Factory classes to create instance of BusinessLayer classes instead of DataLayer. Now, Business layer has "Select" method to load an entity based on PK.

public virtual bool Select()
        {
           // this == NULL ??? I can not make NULL to itself to load another entity????
            return this.FetchEntity();// This calls an adapter to load an entity.
        }

Due to this design, I can not make NULL to BL (own) object inside Select method.

Also, I can create new BL instance everytime from UI classes, but I do NOT want to register/unregister events every time PK change???

Any design/architecture suggestion would be highly appreciated.

Regards.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 24-Nov-2006 10:36:56   

I don't fully understand your architecture, but what you can do instead of forcedcurrentvaluewrite is this:


ProductEntity product = new ProductEntity();
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    product.ProductId = 1;
    adapter.FetchEntity(product);
    System.Console.WriteLine("ProductID:{0}, Name:{1}", product.ProductId, product.ProductName);

    ProductEntity dummy = new ProductEntity(2);
    adapter.FetchEntity(dummy);
    product.Fields = dummy.Fields;
    System.Console.WriteLine("ProductID:{0}, Name:{1}", product.ProductId, product.ProductName);
}
Frans Bouma | Lead developer LLBLGen Pro