应用程序架构 - RavenDB 事务
我正在一个项目中实现 RavenDB,在尝试数据库几天后,我现在正在构建这个应用程序,但我有一个问题。
我正在为每个实体(几乎)编写业务层,并且我有一个独特的存储库类来处理查询和 DocumentStore。 我对如何沿着服务层共享 DocumentStore 并处理事务感到困惑。
我正在展示一个示例,其中我尝试在单个事务中存储和读取文档。
存储库的一个示例:
public class RavenRepository
{
private static DocumentStore _store;
private IDocumentSession _session;
public RavenRepository(DocumentStore store)
{
_store = (_store==null) ? new DocumentStore() { Url = "http://wsk-gcardoso:8081" } : store;
}
public T SingleOrDefault<T>(Func<T, bool> predicate) where T : BaseModel
{
using (var session = _store.OpenSession())
{
return session.Query<T>().SingleOrDefault(predicate);
}
}
public T Add<T>(T item) where T : BaseModel
{
using (var session = _store.OpenSession())
{
session.Advanced.AllowNonAuthoritiveInformation = this.AllowNonAuthoritiveInformation;
session.Store(item);
session.SaveChanges();
}
return item;
}
public void Initialize() {
_store.Initialize();
}
public void Dispose() {
_store.Dispose();
}
}
业务层如下所示:
public class NewsletterBusiness
{
private RavenRepository repository;
public NewsletterBusiness(RavenRepository ravenRepository)
{
repository = (ravenRepository == null) ? RavenRepository(null) : ravenRepository;
}
public Newsletter Add(Newsletter newsletter)
{
Newsletter news = repository.Add(newsletter);
return news;
}
public Newsletter GetById(long Id)
{
Newsletter news = repository.SingleOrDefault<Newsletter>(x => x.Id == Id);
return news;
}
}
现在我尝试在同一事务中保存并读取对象(新闻通讯)。 根据我的阅读,我必须将 documentStore 的 AllowNonAuthoritativeInformation 设置为 false 以等待事务完成。但是从我处理层和存储库的方式来看,我是否在单个事务中存储和查询数据库?
老实说,我认为我对 OpenSession 存储方法感到困惑。我认为我将会话与事务混淆了。
例如,这段代码:
var repository = new RavenRepository(null);
newsletterBusiness = new NewsletterBusiness(repository);
repository.Initialize();
using (var tx = new TransactionScope())
{
Newsletter new = newsletterBusiness.Add(new Newsletter { Title = "Created by Tests", Content = "Created By Tests" });
Newsletter objectCreated = newsletterBusiness.GetById(new.Id);
repository.Dispose();
tx.Complete();
}
如果我创建第二个业务层(例如图片)并设置picturesBusiness.repository =存储库(为businessLayer设置的相同“RavenRepository对象”)我将工作在 newsletterBusiness.repository 的同一会话中,
picturesBusiness = new PicturesBusiness(repository);
Picture pic = picturesBusiness.GetById(20);
我非常感谢有关此主题的帮助, 来自葡萄牙的欢呼!
I'm implementing RavenDB in a project and after a few days trying the database i'm now structuring this application but i'm having a question.
I'm writing business layers for each entity (almost) and i've a unique repository class that handles the queries and the DocumentStore.
I'm confused about how i should share the DocumentStore along the services layers and handle the transactions.
I'm showing an example where i'm trying to store and read a document in a single transaction.
An example of the repository:
public class RavenRepository
{
private static DocumentStore _store;
private IDocumentSession _session;
public RavenRepository(DocumentStore store)
{
_store = (_store==null) ? new DocumentStore() { Url = "http://wsk-gcardoso:8081" } : store;
}
public T SingleOrDefault<T>(Func<T, bool> predicate) where T : BaseModel
{
using (var session = _store.OpenSession())
{
return session.Query<T>().SingleOrDefault(predicate);
}
}
public T Add<T>(T item) where T : BaseModel
{
using (var session = _store.OpenSession())
{
session.Advanced.AllowNonAuthoritiveInformation = this.AllowNonAuthoritiveInformation;
session.Store(item);
session.SaveChanges();
}
return item;
}
public void Initialize() {
_store.Initialize();
}
public void Dispose() {
_store.Dispose();
}
}
A business layer would be like this:
public class NewsletterBusiness
{
private RavenRepository repository;
public NewsletterBusiness(RavenRepository ravenRepository)
{
repository = (ravenRepository == null) ? RavenRepository(null) : ravenRepository;
}
public Newsletter Add(Newsletter newsletter)
{
Newsletter news = repository.Add(newsletter);
return news;
}
public Newsletter GetById(long Id)
{
Newsletter news = repository.SingleOrDefault<Newsletter>(x => x.Id == Id);
return news;
}
}
Now i'm trying to save and get read an object (Newsletters) in the same transaction.
From what i've read, i have to set the AllowNonAuthoritativeInformation of the documentStore to false to wait until the transaction is completed. But from the way i'm dealing with the layers and the repository, am i storing and querying the db in a single transaction?
To be honest i think i'm confused about the OpenSession method of store. I think i'm confuse the session with the transaction.
For example, this code:
var repository = new RavenRepository(null);
newsletterBusiness = new NewsletterBusiness(repository);
repository.Initialize();
using (var tx = new TransactionScope())
{
Newsletter new = newsletterBusiness.Add(new Newsletter { Title = "Created by Tests", Content = "Created By Tests" });
Newsletter objectCreated = newsletterBusiness.GetById(new.Id);
repository.Dispose();
tx.Complete();
}
If i create a second business layer (for pictures for example) and i set the picturesBusiness.repository = repository (the same "RavenRepository object seted for businessLayer) i'll be working in the same session of the newsletterBusiness.repository?
picturesBusiness = new PicturesBusiness(repository);
Picture pic = picturesBusiness.GetById(20);
I would really appreciate help on this subject,
Cheers from Portugal!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您没有正确使用 RavenDB,这就是您遇到此问题的原因。
您可以将会话视为数据库连接。在您的模型中,您将为每种类型创建一个单独的会话。但你确实没有真正的理由要这样做。
事实上,您不想这样做,因为会话完全能够同时处理多种类型。
此外,您所做的实际上是减少会话机会来优化诸如向服务器发送一批更新之类的事情。
如果您希望进行跨类型的事务写入,根据您的架构,您必须使用 DTC,因为每个不同的会话都是与数据库的不同连接。如果您使用“Session per Request”模型,您将只有一个会话,并且只需调用一次 SaveChanges() 即可获得事务语义。
You aren't using RavenDB properly, which is why you are running into this problem.
You can think of the session as the database connection. In your model, you are creating a separate session for each type. But there is really no real reason why you want to do that.
In fact, you don't want to do that, because the session is perfectly capable of handling multiple types at the same time.
Moreover, what you have done is actually decrease the session chances to optimize things like sending updates in one batch to the server.
If you want to have a transactional write that spans types, given your architecture, you would have to use DTC, because each different session is a different connection to the database. If you were using the Session per Request model, you would have only a single session, and you would have transactional semantics by just calling SaveChanges() once.
您试图通过存储库模式抽象 raven 的会话(基本上是一个工作单元)。
我建议您采用以下方法 - 创建一个 UnitOfWork 抽象,它将封装 ravenDB 会话并从中实例化存储库。这是一些伪代码:
在这里您显式地实现这些模式。
PS:当然,您可以根据自己的喜好声明 IUnitOfWork 和 IRepository...
You're trying to abstract raven's session (which is basically an UnitOfWork) by a repository pattern.
I'd suggest you the following approach - create a UnitOfWork abstraction that will encapsulate ravenDB session and instantiate Repositories from it. Here's some the pseudocode:
Here you explicitly implement those patterns.
P.S.: Of course you can declare IUnitOfWork and IRepository at your taste...