Entity event for setting custom property value

Posts   
 
    
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 10-Oct-2013 16:49:57   

I have a entity that needs an additional property for use in the UI. (ASP.NET FormView with an LLBLGenDataSource2 control).

The concept would be that when the entity is created, I'd like to go to the database and get a value based on some related information. For the sake of this thread, let's say that I am fetching a CustomerEntity and I want to get the most recent OrderId for the customer. That value may or may not exist.

The key thing is that I will need to make a database call to get this data and I want to put that data into an additional field/property that is not part of the base entity (that is, the entity as it exists in the database model).

When the entity is saved, I will be taking some actions based on that value. I am tapping into the PerformWork event on the datasource control so I think that part will not be a problem.

Is there an event on the entity that I can tap into for this? I would need to have access to the entity field values so that I could use criteria from the entity such as the CustomerId.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 10-Oct-2013 20:37:12   

Do you want to make the DB call, when you create an entity (new), or when it has been fetched?

jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 11-Oct-2013 00:43:04   

I can go either way but it seems to me that it would be better when I create the entity. What are the options for either way?

Using this code:


CustomerEntity customer = new CustomerEntity("CHOPS");
using(DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.FetchEntity(customer);
}
//additional property available now without needing to write additional code in the calling application ("client")
Console.WriteLine(customer.MostRecentOrderId);

I was looking at the code in this post as a starting point but I think the CreateFields() might've changed since then. http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=14110

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Oct-2013 07:27:27   

The CreateFields approach is fine. The downside is that you cannot use that entity for update, but just to fetch. Then, there is no way you can do it at the entity creation, because you are using Adapter. For adapter, your entity cannot access the DB by itself. It needs an external Adapter to make the call..

What I would do is to project a query (LINQ2LLBL, LBLGen API or QuerySpec) into the entity directly. Something like:

using (var adapter = new DataAccessAdapter())
{
    var metaData = new LinqMetaData(adapter);
    var cust = (from c in metaData.Customer
                where c.CustomerId == "ALFKI"
                select new CustomerEntity
                {
                    CustomerId = c.CustomerId,
                    CompanyName = c.CompanyName,
                    //...
                    LastOrderId = c.Orders.OrderByDescending(o => o.OrderId).First().OrderId
                }).FirstOrDefault();
}

You also could use QuerySpec or LLBLGenAPI.

David Elizondo | LLBLGen Support Team
jovball
User
Posts: 443
Joined: 23-Jan-2005
# Posted on: 14-Oct-2013 13:21:02   

If I understand you correctly, I can't do this at entity creation because I am using Adapter.

I can't do what you are proposing below because getting the actual value is more complex than what I am giving in my example.

If I can't do this at entity creation time, I need a way to intercept the entity between time the LLBLGenDataSource2 creates it and the time it is bound to to FormView fields.

Any suggestions?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 14-Oct-2013 22:56:37   

You can disable LLBLGen-DataSource LivePersistence and get your hands on the fetched entity/collection just in time before binding. This would be at the PerformSelect event handler of the DataSource.