使用 ServiceStack.ORMLite 实现工作单元和存储库模式的最佳实践

编程入门 行业动态 更新时间:2024-10-27 00:35:16
本文介绍了使用 ServiceStack.ORMLite 实现工作单元和存储库模式的最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设有两个存储库接口:

Supposing that there are two repository interface :

interface IFooRepository { void Delete(int id); } interface IBarRepository { void Delete(int id); }

还有一个 IUnitOfWork 接口,如:

And an IUnitOfWork interface like :

interface IUnitOfWork : IDisposable { void Commit(); void Rollback(); }

使用 ServiceStack.ORMLite 实现这些接口的最佳实践是什么,以便用户可以像使用它们一样

what is the best practices of implementing those interface using ServiceStack.ORMLite so that user can use them like

MyFooRepository.Delete(4); // if an Exception throws here, Bar won't be deleted MyBarRepository.Delete(7);

using (var uow = CreateUnitOfWork()) { MyFooRepository.Delete(4); MyBarRepository.Delete(7); uow.Commit(); //now they are in an transaction }

推荐答案

不确定您是否需要 Repository + UnitOfWork 模式,但我认为 ServiceStack + OrmLite 中有一些替代解决方案可以让您的代码在需要之前保持DRY"引入任何模式(特别是如果您主要寻求事务/回滚支持).下面是我要开始的地方.

Not sure of your need for Repository + UnitOfWork patterns but I think there are some alternative solutions in ServiceStack + OrmLite that keep your code 'DRY' before you need to introduce any patterns (especially if you're mainly seeking Transaction/Rollback support). Something like below is where I would start.

public class Foo //POCO for data access { //Add Attributes for Ormlite public int Id { get; set; } } public class Bar //POCO for data access { //Add Attributes for Ormlite public int Id { get; set; } } //your request class which is passed to your service public class DeleteById { public int Id { get; set; } } public class FooBarService : MyServiceBase //MyServiceBase has resusable method for handling transactions. { public object Post(DeleteById request) { DbExec(dbConn => { dbConn.DeleteById<Foo>(request.Id); dbConn.DeleteById<Bar>(request.Id); }); return null; } } public class MyServiceBase : Service { public IDbConnectionFactory DbFactory { get; set; } protected void DbExec(Action<IDbConnection> actions) { using (var dbConn = DbFactory.OpenDbConnection()) { using (var trans = dbConn.OpenTransaction()) { try { actions(dbConn); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } } } }

一些参考...

github/ServiceStack/ServiceStack.RedisWebServices - 以上代码修改自本例

github/ServiceStack/ServiceStack.RedisWebServices - The above code is modified from this example

groups.google/forum/#!msg/servicestack/1pA41E33QII/R-trWwzYgjEJ - 关于ServiceStack层的讨论

groups.google/forum/#!msg/servicestack/1pA41E33QII/R-trWwzYgjEJ - discussion about layers in ServiceStack

ayende/blog/3955/repository-is-the-new-singleton - Ayende Rahien(NHibernate 核心贡献者)关于 Repository 模式

ayende/blog/3955/repository-is-the-new-singleton - Ayende Rahien (NHibernate core contributor) on Repository pattern

更多推荐

使用 ServiceStack.ORMLite 实现工作单元和存储库模式的最佳实践

本文发布于:2023-11-06 22:24:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564838.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模式   工作   ServiceStack   ORMLite   单元和

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!