1

我正在使用ObjectDataSource对 执行 CRUD 操作GridView。当一个项目被删除时,我希望能够确保没有违反外键约束,如果是这样,在表示层中使用函数向用户显示一条ObjectDataSourceStatusEventArgs消息ObjectDataSource Deleted

protected void ods_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
{
    if (e.Exception != null)
    {
        // Display message that was set in either the service or data access layer
    }
}

我认为可以在服务层或数据访问层中检查约束。

服务层

// This is the DeleteMethod used by the ObjectDataSource
[DataObjectMethod(DataObjectMethodType.Delete, true)]
public virtual bool Remove(Entity entity)
{
    bool CanDelete = functionToSeeIfAnythingIsUsingThisKey(entity.ID);
    if (CanDelete)
    {
        _repository.Remove(entity);
        return true;
    }
    else
    {
        // would like to populate ObjectDataSourceStatusEventArgs that are sent
        // back to the ObjectDataSource onDeleted function in Presentation layer
        return false;
    }
}

数据访问层

public virtual void Remove(Entity entity)
{
    _session = NHibernateSessionProvider.GetSession();
    try
    {
        using (ITransaction transaction = _session.BeginTransaction())
        {
            _session.Delete(entity);
            transaction.Commit();
        }
    }
    catch
    {
        // database will throw an error if the constraint check fails,
        // which is caught here
    }
}

我知道从服务层函数返回的任何内容都可以在e.ReturnValue返回到表示层时看到,但是是否可以设置其他成员ObjectDataSourceStatusEventArgs(即 AffectedRows、Exception、OutputParemters 等)?

4

0 回答 0