Can this be done?

Posts   
 
    
Posts: 22
Joined: 20-Sep-2006
# Posted on: 25-Sep-2006 15:42:41   

I would like to make a common class in my BLL that handles the C.R.U.D methods.

This class should make a call to that DAL (LLBL Generated) to call the approprate functions. The purpose of this is to avoid having to set up an adapter in every single class and every single crud function.

I came up with this public bool Delete(IEntity2 _entity) { _adapter.OpenConnection(); returnVal = _adapter.DeleteEntity(_entity); _adapter.CloseConnection(); _adapter.Dispose(); return returnVal;

    }

which complies just fine. However when the code is called like this

MTDataPortal.DataCalls _data = new DataCalls(); if (!_data.Delete(_Trailer)) { throw new Exception( "An error occured while attempting to delete this entity", null);

            }

It dosen't delete the entity and throws the exception.

Any thoughts?? Should I even be doing things this way?

TIA,

Justin

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Sep-2006 16:06:12   

Like you, I created a class that has static methods in it so that I could centralize my creation of DataAccessAdapters and minize the "extra lines" in my BL code.

My personal preference is NOT to use a shared DataAccessAdapter instance, but instead create it as necessary.

Using the using statement is recommended so that you don't need to open, close and displose adapter objects.


public static bool DeleteEntity(IEntity2 entity)
{
    bool status;
    using (DataAccessAdapter adapter = new DataAccessAdapter())
    {
        status = adapter.DeleteEntity(entity);
    }
    return status;
}

Then when you want to execute it in your BL code, you can do so as follows:


if (!DataManager.DeleteEntity(_Trailer))
{
    throw new Exception("An error occured while attempting to delete this entity", null);
}

You don't need an instance of _data since the DeleteEntity method is static.

ps. check out the "code" tags in the forum to post formatted code.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 25-Sep-2006 16:08:08   

As far as why it is not deleting your entity, you need to check out the exception being genereated by the DeleteEntity call.

It is possible that the entitty does not exist in the database or that there are foreign key constraints or triggers that are keeping it from being deleted successfully.

Posts: 22
Joined: 20-Sep-2006
# Posted on: 25-Sep-2006 16:53:26   

Thanks for the reply. Changing the calls to static makes a lot more sense. Since it keeps me from instantating objects all over the place, something I was trying to avoid in the first place.

Thanks, Justin

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 25-Sep-2006 17:32:45   

jlazanowski wrote:

public bool Delete(IEntity2 _entity) { _adapter.OpenConnection(); returnVal = _adapter.DeleteEntity(_entity); _adapter.CloseConnection(); _adapter.Dispose(); return returnVal;

    }

Justin

Hi,

The problem that I see is that you are disposing the adapter object, then after your first call to this routine the adapter is disposed and not available for future calls.

Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 25-Sep-2006 20:42:03   

Hi, the boolean result is computed from

Command.ExecuteNonQuery() > 0

where command contains. the delete query.

I also think you shouldn't dispose your adapter, although it should raise an exception, and I had bad experiences in the past trying to reuse adapters arbitrarily.

The result also drivesthe state of your entity to deleted:



                if( deleteSucceeded )
                {
                    entityToDelete.Fields.State = EntityState.Deleted;
                }

False is also returned when the entity does not have a primary key. Can you check it is not the case?

Posts: 22
Joined: 20-Sep-2006
# Posted on: 25-Sep-2006 23:38:13   

Sorry, I suppose I should have mentioned that I found the other problem.

It was returing false because there was nothing to delete. It was issuing an ID value of zero. I found that in the BLL I had left a new instantation of the enity that I had used previously to test. Since it wasn't a loaded entity it couldn't delete it.

Thanks

Justin