Best Practices for "Not Found" logic

Posts   
 
    
asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 15-Jun-2009 01:39:34   

I have read through the help to find the answer to this question, but I have not come across it. Forgive me if it is there and I have missed it. I have set up an entity that has a Unique Constraint column called MachineName. My code needs to check if there is a record in this table with this MachineName andif there is update it, otherwise, add one. All of the "fetch" examples show passing in a value that assumes that you will get something back ("Chops" in most cases). What do you get if you do a Unique Constraint fetch and it doesn't find a matching record? Is the entity "null"? I can't find this anywhere.

I read that you can start an update without reading from the database by creating an entity, then assigning values and then setting the IsNew property based on whether this is a new record, but I'm still stuck on what the best pattern in LLBLGen is to first check if there is matching record and if not insert one or if there is, update the one that is there.

Thanks for your help.

Allen

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Jun-2009 10:11:49   

I'll assume you are using SelfServicing.

You should use code like this

CustomerEntity customer = new CustomerEntity();
customer.FetchUsingUCCompanyName("Chop-suey Chinese");

customer.ContactName = ......;
customer.AnotherField = ....;

customer.Save();

This will either update the existing entity, or insert a new one.

asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 15-Jun-2009 13:46:06   

Oh, so I don't actually have to check first, it just "knows". Very cool!

Thanks!

asowles
User
Posts: 46
Joined: 23-Apr-2008
# Posted on: 19-Jun-2009 16:01:10   

Does this work the same with Adapter?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 20-Jun-2009 07:46:42   

very similar... but you need an adapter to fetch/update:

DataAccessAdapter adapter = new DataAccessAdapter();
CustomerEntity customer = new CustomerEntity();
adapter.FetchUsingUsingUniqueConstraint(new PredicateExpression(CustomerFields.CompanyName == "Chop-suey Chinese"));

customer.ContactName = ......;
customer.AnotherField = ....;

adapter.SaveEntity(customer);

And... for your second question...

I read that you can start an update without reading from the database by creating an entity, then assigning values and then setting the IsNew property based on whether this is a new record, but I'm still stuck on what the best pattern in LLBLGen is to first check if there is matching record and if not insert one or if there is, update the one that is there.

Yes you can. The approximate code is:

// instantiate a new customer... no fetch here.
Customer toUpdate = new Customer("ALFKI");

// some changes...
toUpdate.CompanyName = "something...";

// mark the entity as not new. So at save, this will generate an UPDATE, no INSERT
toUpdate.IsNew = false;

// now save the entity
adapter.SaveEntity(toUpdate);
David Elizondo | LLBLGen Support Team