Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8037373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:54:41+00:00 2026-06-05T02:54:41+00:00

I have a service class UserService that gets an instance of IDocumentStore injected using

  • 0

I have a service class UserService that gets an instance of IDocumentStore injected using AutoFac. This is working fine but now I’m looking at code like this:

public void Create(User user)
{
    using (var session = Store.OpenSession())
    {
        session.Store(user);
        session.SaveChanges();
    }
} 

Every action that writes to the db uses this same structure:

using (var session = Store.OpenSession())
{
    dosomething...
    session.SaveChanges();
}

What is the best way to eliminate this repetitive code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:54:42+00:00Added an answer on June 5, 2026 at 2:54 am

    The easiest way is implementing OnActionExecuting and OnActionExecuted on a base controller and use it.

    let’s imagine you create your RavenController like this:

    public class RavenController : Controller
    {
        public IDocumentSession Session { get; set; }
        protected IDocumentStore _documentStore;
    
        public RavenController(IDocumentStore documentStore)
        {
            _documentStore = documentStore;
        }
    
        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Session = _documentStore.OpenSession();
            base.OnActionExecuting(filterContext);
        }
    
        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            using (Session)
            {
                if (Session != null && filterContext.Exception == null)
                {
                    Session.SaveChanges();
                }
            }
            base.OnActionExecuted(filterContext);
        }
    }
    

    then all you need to do in your own controllers is inherit from RavenController like this:

    public class HomeController : RavenController
    {
        public HomeController(IDocumentStore store)
            : base(store)
        {
    
        }
    
        public ActionResult CreateUser(UserModel model)
        {
            if (ModelState.IsValid)
            { 
                User user = Session.Load<User>(model.email);
                if (user == null) { 
                    // no user found, let's create it
                    Session.Store(model);
                }
                else {
                    ModelState.AddModelError("", "That email already exists.");
                }
            }
            return View(model);
        }
    }
    

    Interesting enough, I have found a blog post showing exactly this technique …

    it does explain way more that what I did. I hope it helps you better

    Building an ASP.NET MVC app using RavenDB as a Backing Store

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have service classes that look something like this public class UserService :
I have this code that describes a service: public class navigation_web extends Service {
I have a service class that I need to execute a method on a
I have created a service class for my network connection so that my app
I have a wcf service. The service itself (class that inherits the ServiceContract )
Given that I have the following WCF service: class LookUpService { public List<County> GetCounties(string
My homecontroller has a UserService object that gets wired using spring correctly (it renders
I have a service class implemented in Java 6 / Spring 3 that needs
I have a class say UserService which implements Service and annotated with Service StereoType,
I have a web service class that the rest of the framework depends on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.