Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application architecture - Transactions w/ RavenDB

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!

like image 854
Gui Avatar asked Dec 07 '22 18:12

Gui


2 Answers

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.

like image 155
Ayende Rahien Avatar answered Dec 25 '22 02:12

Ayende Rahien


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:

public class RavenUnitOfWork {
    private IDocumentSession m_session;

    public UnitOfWork() {
        // initialize m_session here
    }

    public Repository<T> GetRepository() {
        return new Repository<T>(m_session);
    }

    public void Commit() {
        m_session.SaveChanges();
    }
}

public class RavenRepository<T> {
    public Repository(IDocumentSession session) {
        // Store the session
    }
    public T Load(string id) {...}
    public void Store(T entity) {...}
    public void Delete(T entity) {...}
    public IQueryable<T> Query() {...}
}

Here you explicitly implement those patterns.

P.S.: Of course you can declare IUnitOfWork and IRepository at your taste...

like image 32
Igor S. Avatar answered Dec 25 '22 03:12

Igor S.