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

  • SEARCH
  • Home
  • 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 545859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:48:23+00:00 2026-05-13T10:48:23+00:00

I found what I thought was a great article by Ayende on creating a

  • 0

I found what I thought was a great article by Ayende on creating a simple base test fixture for NHib unit testing with SQLite.

My question here is the code for a test case in concrete test fixture. In EX_1 below, Ayende wraps both the save a fetch in a transaction which he commits, and has a Session.Clear in between. This works, or course, but so does EX_2.

All things equal I’d prefer the more compact, readable EX_2. Any thoughts on why the additional code in EX_1 is worth a bit of clutter?

Cheers,
Berryl

==== EX_1 =====

[Fact]
public void CanSaveAndLoadBlog_EX_1()
{
    object id;

    using (var tx = session.BeginTransaction())
    {
        id = session.Save(new Blog
        {
            AllowsComments = true,
            CreatedAt = new DateTime(2000,1,1),
            Subtitle = "Hello",
            Title = "World",
        });

        tx.Commit();
    }

    session.Clear();


    using (var tx = session.BeginTransaction())
    {
        var blog = session.Get<Blog>(id);

        Assert.Equal(new DateTime(2000, 1, 1), blog.CreatedAt);
        Assert.Equal("Hello", blog.Subtitle);
        Assert.Equal("World", blog.Title);
        Assert.True(blog.AllowsComments);

        tx.Commit();
    }
}

==== EX_2 =====

[Fact]
    public void CanSaveAndLoadBlog_EX_2()
    {
        var id = session.Save(new Blog
                                 {
                                     AllowsComments = true,
                                     CreatedAt = new DateTime(2000, 1, 1),
                                     Subtitle = "Hello",
                                     Title = "World",
                                 });

        var fromDb = session.Get<Blog>(id);

        Assert.Equal(new DateTime(2000, 1, 1), fromDb.CreatedAt);
        Assert.Equal("Hello", fromDb.Subtitle);
        Assert.Equal("World", fromDb.Title);
        Assert.True(fromDb.AllowsComments);

    }
  • 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-05-13T10:48:23+00:00Added an answer on May 13, 2026 at 10:48 am

    I believe with NHibernate it is encouraged to use transactions even when you you’re only querying. Check this article http://nhprof.com/Learn/Alerts/DoNotUseImplicitTransactions.

    Also your EX_2 code might not hit the database depending on what type of primary key you’re using. If you’re using Identity key that autoincrement, NHibernate will hit the database and get a primary key, but if you using guid, guid.comb, or hilo, you won’t hit the database at all. So your Get would be grabbing what NHibernate has cached in memory unless you do a commit changes and then clear the session so you know you got nothing in memory.

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

Sidebar

Related Questions

I found this question in an old question in your website so i thought
I found this in the code I'm working on at the moment and thought
I found this guide for using the flash parameters, thought it might be useful
I need to send MMS thought a C# application. I have already found 2
This is not homework. I saw this article praising Linq library and how great
I have found some great articles ( Maoni , Richter #1 , Richter #2
Found some old code, circa VS 2003. Now I have just VS 2008 (SP1)
Found the following in an Oracle-based application that we're migrating (generalized) : SELECT Table1.Category1,
I found this open-source library that I want to use in my Java application.
I found What are mvp and mvc and what is the difference but it

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.