How to find the entity itself within the CreateDeleteDQ method (Adapter model)

Posts   
 
    
Hans
User
Posts: 9
Joined: 18-Mar-2013
# Posted on: 21-Mar-2013 10:40:41   

Context:

General: We use the LLBLgen designer version 3.5, the LLBLgen framework and the LLBLgen OData provider to expose our business model for client applications.

Business model: Within the LLBLgen designer we created an entity model based on our Business. This model contains our functional business entities, business properties and the business relations necessary for our client applications. It contains no database specific information other than a pk property and fk relations. Each entity contains also an external generated business type id which is necessary for client notifications. The business type id was added as a partial class property.

Physical model database: The database has his own complex physical data model and contains specific business logic for different issues and subjects like “database synchronization” and “project authorization”. The interface to the ORM is implemented by database views and stored procedures. The view is used for the retrieve (R) action and the specific create, update and delete (CUD) actions are done by stored procedures.

Mapping/Interfacing database with LLBLgen ORM framework: The LLBLgen framework with its business objects are mapped with the database by the database views and its stored procedures. There is a specific mapping for each business entity. This is implemented by one database view and three stored procedures. The mapping of the database View can easily be mapped in the designer by the Field Mappings tab and then the Target selection. The view can be selected in the Target drop down box witch is available for each entity. The CUD actions on the other hand are implemented using the LLBLgen framework Adapter model. The CUD actions are implemented in the Adapter class by a override of the CreateInsertDQ, CreateUpdateDQ, CreateDeleteDQ methods, see the included code:


protected override IActionQuery CreateInsertDQ(IEntity2 entityToSave, IFieldPersistenceInfo[] persistenceInfoObjects)
{
    ActionQuery_SPCreate query = new ActionQuery_SPCreate(this, entityToSave);
    return query;
}

protected override IActionQuery CreateUpdateDQ(IEntity2 entityToSave, IFieldPersistenceInfo[] persistenceInfoObjects, List<IPredicate> pkFilters)
{
    ActionQuery_SPUpdate query = new ActionQuery_SPUpdate(this, entityToSave);
    return query;
}

protected override IActionQuery CreateDeleteDQ(IFieldPersistenceInfo[] fieldsPersistenceInfo, List<IPredicate> pkFilters)
{
    Predicate predicate = (Predicate)pkFilters.First();
    FieldCompareValuePredicate fcvp = (FieldCompareValuePredicate)((PredicateExpression)predicate).First().Contents;
    string tableName = fcvp.PersistenceInfo.SourceObjectName;
    long recordId = (int)fcvp.Value;
    ActionQuery_SPDelete query = new ActionQuery_SPDelete(null, this, tableName, recordId);
    return query;
}

The CUD Stored Procedure calls itself (not shown here) are implemented in the ActionQueries of the CreateXXDQ methods. Both the CreateInsertDQ and CreateUpdateDQ where very simple to implement because they have all the business properties of the entity available there. And also the additional business type ID is available there to be redirected to the stored procedure as a parameter. A second stored procedure parameter is used for concurrency control and is also be implemented here, no problem.

Issue: But we have a problem with the CreateDeleteDQ method. It contains no reference to the instance who he supposed to be delete. The ID and the entity/table name can be found using some additional steps, see above. But the property values cannot be found and used here. Is there a hook to get it available?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 21-Mar-2013 19:41:40   

The ID and the entity/table name can be found using some additional steps, see above. But the property values cannot be found and used here. Is there a hook to get it available?

What property values are you looking for? And why do you need them?

Hans
User
Posts: 9
Joined: 18-Mar-2013
# Posted on: 22-Mar-2013 10:47:59   

The properties I am looking for in the CreateDeleteDQ method are:

  • DateModified: The property is valid and updated by the database each time the business entity is created or updated. This property is used for the concurrency detection I spoke about. In this architecture the database can be changed due to a database synchronization by a number of other databases. So concurrency detection is very importante to have implemented for . If detected .... a transaction can be rolled back and a merge with the new data in the database.

  • GetBusinessId: The business model has its own business types. The business entities are not 1:1 related to the tables in the database. That's why this additional property is added as a partial class/property to the business entities. For business notification reasons and logging the business type id have to passed throu the stored procedure to the database.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 22-Mar-2013 11:08:45   

All delete queries for entities are created from the virtual method DeleteEntity(entity, filter). Overriding that method allows you to store the entity to delete somewhere in a private variable, then in the override of CreateDeleteDQ, you read / use the entity you just stored.

Would that be what you could use?

Frans Bouma | Lead developer LLBLGen Pro
Hans
User
Posts: 9
Joined: 18-Mar-2013
# Posted on: 25-Mar-2013 12:00:54   

Yes I have implemented the virtual DeleteEntity method like you suggested with a local variable in combination with the CreateDeleteDQ method, see the code. All unit tests works fine with this implementation ....

Does this implementation also works for version 4.0 (and other future versions)?


public partial class DataAccessAdapter
{
    private IEntity2 _entityToDelete; // temporary saved for CreateDeleteDQ

    protected override IActionQuery CreateInsertDQ(IEntity2 entityToSave, IFieldPersistenceInfo[] persistenceInfoObjects)
    {
        ActionQuery_SPCreate query = new ActionQuery_SPCreate(this, entityToSave);
        return query;
    }

    protected override IActionQuery CreateUpdateDQ(IEntity2 entityToSave, IFieldPersistenceInfo[] persistenceInfoObjects, List<IPredicate> pkFilters)
    {
        ActionQuery_SPUpdate query = new ActionQuery_SPUpdate(this, entityToSave);
        return query;
    }
    
    public override bool DeleteEntity(IEntity2 entityToDelete, IPredicateExpression deleteRestriction)
    {
        _entityToDelete = entityToDelete;
        return base.DeleteEntity(entityToDelete, deleteRestriction);
    }

    protected override IActionQuery CreateDeleteDQ(IFieldPersistenceInfo[] fieldsPersistenceInfo, List<IPredicate> pkFilters)
    {
        ActionQuery_SPDelete query = new ActionQuery_SPDelete(this, _entityToDelete);
        return query;
    }   
}

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 25-Mar-2013 12:45:10   

Yes, should work in v4 as it does in v3.

Frans Bouma | Lead developer LLBLGen Pro
Hans
User
Posts: 9
Joined: 18-Mar-2013
# Posted on: 25-Mar-2013 13:55:18   

Thanks!