软件开发过程中的事务处理(二)

在上一篇 软件开发中的事务处理 中我们已经比较了将connection的打开、关闭以及提交和回滚放在service层和DAO层,而笔者认为,放在service层更为合理,这样可以更有效的控制事务,但是这样的做法带来了一些弊端,其中一个就是异常,因为DAO层不可避免地要掷出异常,而在写操作的方法中,我们则需要catch这个异常,同时把connection回滚。

那么如何来处理这个问题呢?我个人的做法是这样的:

1、尽量拆分DAO的方法,最好可以做到DAO的方法,只负责读写而没有任何逻辑的判断。要做到这一点,我们首先要放弃一个service层的方法对应一个DAO方法的思想,DAO本来就是为了Service服务的,所以一个Service方法应该是调用一个以上的DAO方法,而不是一一对应的关系。

2、通过步骤一,尽量做到,不在DAO层抛出任何异常,尤其是要被service层中包含写的方法调用的DAO方法,这样一来就可以避免
catch ( *** Exception e) {
    ConnectionPool.rollback(Connection);
    
throw e;
}
这样的代码出现了。

而这样的做法,还可以极大的提高代码的重用性,使我们的系统更容易维护和升级。

下面是一个简单的例子。

一个论坛系统,用户可以删除自己的帖子。

如果我们采用service和DAO方法一一对应的设计,接口如下:

service中的方法为:void deleteThread(int userId, int threadId);
DAO方法:void deleteThread(int userId, int threadId);
第一个方法只是简单的调用第二个方法而已,可以说只是一个简单的代理,而所有的逻辑判断等都在dao.deleteThread中

那么dao.deleteThread方法应该处理以下操作:
deleteThread( int  userId,  int  threadId)  throws  ObjectNotFoundException {
Thread thread 
= getThread(This may be just some codes rather than a method)
if(thread == null){
throw new ObjectNotFoundException();
}
else{
deleteThead(This may be just some codes rather than a method)
}

}

而如果我们不是用一一对应的方式最这个功能进行设计的话,代码则如下:
service.deleteThread( int  userId,  int  threadId) {
Thead thread 
= dao.getThread(threadId);
if(thread==null){
throw new ObjectNotFoundException();
}
else{
if(thread.getUserId==userId){
dao.deleteThread(threadId);
}
else{
throw new RequestDeniedException();
}

}

}

DAO当中则包括两个方法,
dao.getThread(int threadId);
dao.deleteThread(int threadId);

仅仅从这一部分代码中,我们很难看到什么好处,但是当我们的论坛系统功能增加时,我们会看到,dao.getThread(int threadId) 这个方法会重复使用了几次。比如,我们的系统增加了推荐功能,让用户可以针对其他人的帖子进行推荐,而每个用户不能推荐自己的帖子,推荐次数之可以是一次。

这样做,还会留下一个问题,就是说,每次在serivce层判断thread是否为null,如果是,则抛出异常这一段代码会显得非常繁琐。而如果在DAO中判断,抛出异常的话,又会让我们走到了文章开头提到的圈子。

这个问题,等到下一章结合Hibernate的一些设计在
软件开发过程中的事务处理(三)来讲。



版权所有: idilent 网站转载请注明作者并链接到http://blog.csdn.net/idilent,
其他转载方式请与作者联系(idilent@yahoo.com.cn)。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 我们可以使用 Revit API 来创建楼板。下面是一个示例代码:// 创建一个新的楼板 Floor floor = Floor.Create(doc, lineArray); // 设置楼板的高度 floor.get_Parameter(BuiltInParameter.FLOOR_HEIGH_PARAM).Set(10); // 设置楼板的类型 floor.get_Parameter(BuiltInParameter.FLOOR_ATTR_THICKNESS_PARAM).Set(floorType.Id); ### 回答2: Revit次开发是指在Revit软件的基础上,使用API(Application Programming Interface)进行编程,以创建自定义功能、定制工具和插件。以下是展示Revit次开发的创建楼板的示例代码: ```csharp using Autodesk.Revit.UI; using Autodesk.Revit.DB; using Autodesk.Revit.Attributes; [Transaction(TransactionMode.Manual)] public class CreateFloorCommand : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // 获取Revit文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 获取楼层板类型 ElementId floorTypeId = null; FilteredElementCollector floorTypeCollector = new FilteredElementCollector(doc) .OfClass(typeof(FloorType)); foreach (FloorType floorType in floorTypeCollector) { if (floorType.Name == "楼板类型名称") { floorTypeId = floorType.Id; break; } } if (floorTypeId != null) { // 获取楼层高度 double floorHeight = 3000; // 楼层高度为3000毫米 // 获取边界线 Line floorBoundaryLine = Line.CreateBound(new XYZ(0, 0, 0), new XYZ(5000, 0, 0)); CurveArray floorBoundary = new CurveArray(); floorBoundary.Append(floorBoundaryLine); // 创建楼板 using(Transaction trans = new Transaction(doc, "创建楼板")) { trans.Start(); Floor floor = doc.Create.NewFloor(floorBoundary, false); floor.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM) .Set(floorHeight); floor.FloorType = doc.GetElement(floorTypeId) as FloorType; trans.Commit(); } return Result.Succeeded; } else { message = "找不到指定的楼板类型"; return Result.Failed; } } } ``` 以上代码使用了Revit API的相关类和方法,通过获取Revit文档,楼板类型,楼层高度和边界线等信息,创建了一个自定义的楼板。需要替换代码的`楼板类型名称`为实际楼板类型的名称,在使用该代码之前,请确保已经正确安装并配置了Revit API开发环境。 ### 回答3: Revit次开发创建楼板的代码如下: 首先,我们需要导入Revit API的命名空间,包括`Autodesk.Revit.DB`和`Autodesk.Revit.UI`。 然后,创建一个继承自`IExternalCommand`接口的类,以便我们可以在Revit运行这个命令。我们将命令命名为"CreateFloor"。 在类,我们需要实现`Execute`方法,该方法将在运行命令时被调用。在这个方法,我们将创建楼板并将其添加到当前项目。 以下是示例代码: ```csharp using Autodesk.Revit.DB; using Autodesk.Revit.UI; public class CreateFloor : IExternalCommand { public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements) { // 获取当前文档 Document doc = commandData.Application.ActiveUIDocument.Document; // 创建标高 Level level = new FilteredElementCollector(doc) .OfClass(typeof(Level)) .FirstOrDefault(e => e.Name.Equals("Level 1")) as Level; // 定义楼板的几何形状 CurveArray curveArray = new CurveArray(); XYZ p1 = new XYZ(0, 0, 0); XYZ p2 = new XYZ(10, 0, 0); XYZ p3 = new XYZ(10, 10, 0); XYZ p4 = new XYZ(0, 10, 0); Line line1 = Line.CreateBound(p1, p2); Line line2 = Line.CreateBound(p2, p3); Line line3 = Line.CreateBound(p3, p4); Line line4 = Line.CreateBound(p4, p1); curveArray.Append(line1); curveArray.Append(line2); curveArray.Append(line3); curveArray.Append(line4); // 创建楼板 using (Transaction transaction = new Transaction(doc, "Create Floor")) { transaction.Start(); Floor floor = doc.Create.NewFloor(curveArray, false); floor.get_Parameter(BuiltInParameter.FLOOR_HEIGHTABOVELEVEL_PARAM).Set(0); // 设置楼板高度 transaction.Commit(); } return Result.Succeeded; } } ``` 以上示例代码演示了如何使用Revit API创建楼板。我们首先获取当前文档,然后创建一个标高。接下来,我们定义了楼板的几何形状,最后创建了楼板并将其添加到项目。在事务运行代码,以确保操作的完整性和一致性。 以上是简单的创建楼板代码示例,该代码仅供参考。在实际开发过程,可能需要根据具体需求进行更多的参数设置和处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值