Oren Eini

CEO of RavenDB

a NoSQL Open Source Document Database

Get in touch with me:

oren@ravendb.net +972 52-548-6969

Posts: 7,640
|
Comments: 51,263
Privacy Policy · Terms
filter by tags archive
time to read 3 min | 523 words

Just thought that it would be interesting post to talk about the evolution of a single method. This is used solely for the integration tests, by the way.

public void WaitForAllMessages()
{ 
  while (mailBox.IsEmpty == false)
  {
    Thread.Sleep(100);
  }
}

It started its life very simply, as just a method that waited until the mailbox was empty. But then we run into a problem, sometimes the method returned while a message was being processed, and that caused the tests to fail.

Then we had this:

public void WaitForAllMessages()
{ 
  while (mailBox.IsEmpty == false  || currentlyProcessingMessages > 0)
  {
    Thread.Sleep(100);
  }
}

There are other pieces of the code, that update the currentlyProcessingMessages piece, but it isn’t really interesting. Some time passed, and we run into additional issues, which led to this beast:

public void WaitForAllMessages()
{
    // we wait in case there are additional messages that have not yet arrived
    // to the listner. We arbitrarily define that if we wait for 5 consecutive 
    // times with no new messages, there are no more messages
    int countOfTimesTriesToWaitAndThereWereNoMessages = 0;
    while(countOfTimesTriesToWaitAndThereWereNoMessages < 5)
    {
          if (WaitForAllMessagesInMemoryQueueOrProcessing())
            countOfTimesTriesToWaitAndThereWereNoMessages = 0;
        else
              countOfTimesTriesToWaitAndThereWereNoMessages += 1;
          Thread.Sleep(100);
    }
}

private bool WaitForAllMessagesInMemoryQueueOrProcessing()
{
    bool waited = false;
    while (mailBox.IsEmpty == false ||
           currentlyProcessingMessages > 0)
    {
        waited = true;
        Thread.Sleep(100);
    }
    return waited;
}

And that is where we are now.

time to read 1 min | 103 words

I noticed the lack when I was teaching the Core NHibernate course. While NH Prof was able to detect loading of an entity from the second level cache and the loading of query from the query cache, it wouldn’t show when we were loading a collection from the collection cache.

That was actually quite easy to fix :-)

Take a look:

image

This feature is now available on Build 277 and up.

Have fun, and happy cache tracing :-)

time to read 1 min | 193 words

One of the most frustrating things about working on Open Source projects is that you don’t have feedback about your users unless they have a problem. During my Progressive.NET NHibernate workshop, there was hardly place in a big room, and talking about this issue with the attendees afterward, I kept hearing things like: “I have seen NHibernate used in my last 3 jobs”, or “We have been using NHibernate for the last 2 years.”

On the one hand, it is heartwarming to hear that, on the other, it is incredibly frustrating not knowing.

If you are using NHibernate, I would like to ask two things out of you:

  • Register yourself as an NHibernate user in ohloh by clicking this link:
  • And please register your experience with NHibernate in our Success Stories page.

This is not a lot to ask, I feel, and it would make the NHibernate team feel much more appreciated.

Thanks…

time to read 3 min | 412 words

image

Probably the most common problem with unintended complexity in software is the unthinking application of patterns. I see a lot more code that has became more complex through that practice than code that has became simpler.

David Wheeler said: “All problems in computer science can be solved by another level of indirection;” However, we tend to forget the extra bit that Kevlin Henney's tacked at the end, "...except for the problem of too many layers of indirection."

Most patterns templates come with a clearly defined sections for “when to use” and “what are the disadvantages”. The problem is that patterns has became hip and cool, so people are actively seeking to use them.

Memento is my favorite pattern.

Do you know why?

I have never had found any need to implement it.

Remember, patterns are just formalized ways to work around limitations in the programming language or environment. Despite all the coolness points associated with them, they represent a non trivial cost in time, complexity and implications on a code base.

When you use a pattern, you have to be able to justify that use. What does it buy you? Have you considered the ramifications? What does it cost you? Is it worth it?  I tend to use patterns quite a bit, but in the last year or so I force myself to make a conscious decision whenever I want to apply one. Quite often, there is nothing that really requires it.

If you can get by without patterns, it usually means that you have a simpler solution. If you have a problem that fits a pattern’s problem description, and the pattern is applicable for your programming language and environment, you should use it.

Just don’t apply patterns all over the place, it end up being an overly engineered codebase that has significant maintenance overhead because of the patterns. I have seen too many apps that could have been simple turn into a multi layer mess of indirect calls that no one could really follow.

When I was in the army, I usually follow the following maxim:

Think, then do. And you have better be able to explain you actions.

It worked for me then and since.

time to read 4 min | 693 words

I have been forcibly reminded lately that I have been doing this for quite some time. In fact, I have been doing working with Open Source on the .Net platform for over 5 years now. And a few conversations with friends have given me quite a retrospective on the state of OSS.Net.

5 years ago, it was 2004, .Net 1.1 was still a hot thing, and Stored Procedures on top of datasets where still a raging debate. Open source was considered a threat and Steve Balmer was busy blasting at any OSS project that showed up. The very existence or need for OSS on the .NET platform was frequently questioned.

I remember trying to find work in 2005, after over a year of actively working on Open Source projects and with Rhino Mocks making steady but sure progress in the .NET TDD community and not being able to leverage that experience into job interviews. It was only commercial experience that counted for the gate keepers.

The last 5 years have been quite interesting in the .NET ecosystem from the OSS world. It has gotten to the point where the use of OSS tools, frameworks and platforms is no longer a strange exception, but is quite common place.

There are several data points on which I am basing this statement:

  • Books about OSS projects are commonly published.
  • Microsoft is doing a lot to encourage OSS on the .Net platform.
  • NHibernate’s download numbers are consistently above ten thousands a month, usually closer or above twenty thousands a month.
  • I released Windsor 2.0 not even two weeks ago, and it has over 1,200 downloads already.
  • The number of messages to the NHibernate users mailing list is usually above a thousands per month.
  • My NHibernate course sold out and I have to do a repeat course to satisfy demand.

And then, there is my own experience, both as a member in the community and as a consultant. I see OSS being used quite often. A lot of my engagements are about OSS tools and framework, and I am rarely the person to introduce them into the company.

I think that there are several factors playing around here, but most of that is around maturity. The OSS players in the .NET world had had some time to work on things, and most established projects have been around for years. NHibernate is 6 years old, Castle is 5, Rhino Mocks 4. It is not the Open Source world that represent stability. With Microsoft replacing their data access strategy every two years, it might be best to use NHibernate, because it has been around for a long time already.

There is also the issue of maturity in the ecosystem itself. It has became quite clear that it is acceptable and even desirable to use OSS projects. And we have companies making explicit decisions to support Open Source projects (iMeta decision to donate 3 dev months is just one example, although the most prominent one). Recently I was working with a client on strategies for Open Sourcing their software, and how to manage a good Open Source project. In another client, a decision has been reached to put all the infrastructure stuff as Open Source, even newly developed one, because they are infrastructure. Infrastructure is seen as a commodity, and as such, there is little value in trying to make it unique.

There is a lot of value, however, in making it Open Source and accepting improvements from others. And I was able to point out to that client how outside contributions to the infrastructure has enabled us to do things that we would have to do ourselves.

Things are changing, I don’t think that we are at the balance point yet, but I think that we are seeing a very big shift, happening very very slowly. And from the Open Source perspective, things are looking quite good.

time to read 1 min | 149 words

In a very few weeks, I am going to spend some time in my favorite north America conference, DevTeach. It is my favorite for several reason, great attendees, great organization, great speakers line up, and an always interesting occurrences. Thankfully, no one published the real pictures from the last one :-)

This time, in addition to all the nice features that are usually part of DevTeach, we also have the bonus of having ALT.Net Canada happening in the weekend directly after DevTeach. This means that I am going to be having throughout the week.

As an aside, I am going to stay a day or two after the weekend is over, the original plan was to give me some time to recover from all of that, but if you are in Vancouver and would like me to drop in and do some consulting, do ping me.

time to read 14 min | 2794 words

A while ago I sat down and talked with a colleague about the Entity Framework, he raved about how important the separation of the logical model from the physical one is. I don’t really buy into that, but that is  beside the point.

Last week, on the Progressive.NET NHibernate workshops, I setup, quite accidently, to create a single domain model and map it to several physical data models. I promised to share the code, and I think that this form is as good as any.

Let us start from the following domain model:

image

I am going to present three things for each physical data model manifestation. The mapping, the database schema and the result of the following query:

s.CreateQuery("from Owner owner where owner.Name = 'ayende'")
    .List<Owner>();

We will start with the classic, table per class, which looks like this:

<class name="Company">
    <id column="Id"  type="System.Int32">
        <generator class="hilo"/>
    </id>
    <property name="Name"/>
    <property name="CompanyRegistrationId"/>
    <set name="Horses" table="CompanyHorses">
        <key column="Company"/>
        <many-to-many class="Horse" column="Horse"/>
    </set>
</class>

<class name="Individual">
    <id column="Id"  type="System.Int32">
        <generator class="hilo"/>
    </id>
    <property name="Name"/>
    <property name="Email"/>
    <set name="Horses" table="HorsesBelongToIndividuals">
        <key column="Company"/>
        <many-to-many class="Horse" column="Horse"/>
    </set>
</class>

<class name="Consortium">
    <id column="Id"  type="System.Int32">
        <generator class="hilo"/>
    </id>
    <property name="Name"/>
    <set name="Horses" table="HorsesBelongToIndividuals">
        <key column="Company"/>
        <many-to-many class="Horse" column="Horse"/>
    </set>
    <set name="Owners" table="ConsortiumOwners">
        <key column="Consortium"/>
        <many-to-any id-type="System.Int32" meta-type="System.String">
            <meta-value class="Individual" value="Individual"/>
            <meta-value class="Company" value="Company"/>
            <meta-value class="Consortium" value="Consortium"/>
            <column name="OwnerType"/>
            <column name="OwnerId"/>
        </many-to-any>
    </set>
</class>

The database schema is:

image

You might want to pay some attention to the association between Consortium and its owner, using <many-to-any/>.

Trying to execute the aforementioned query will give us an error, NHibernate is not aware of any persistent class called Owner and HQL queries are not polymorphic over unknown types. Criteria API, however, are. And we can execute the following query successfully:

s.CreateCriteria<Owner>()
    .Add(Restrictions.Eq("Name", "ayende"))
    .List<Owner>();

Which result in:

image

image

image

Let us look at another scenario, which would keep the same data model, but let NHibernate know about the inheritance association between the classes. NHibernate call this type of association union subclasses. Here are the mapping:

<class name="Owner" abstract="true">
    <id column="Id"  type="System.Int32">
        <generator class="hilo"/>
    </id>
    <set name="Horses" table="OwnerHorses">
        <key column="Owner"/>
        <many-to-many class="Horse" column="Horse"/>
    </set>
    <property name="Name"/>
    <union-subclass name="Company">
        <property name="CompanyRegistrationId"/>
    </union-subclass>

    <union-subclass  name="Individual">
        <property name="Email"/>
    </union-subclass>

    <union-subclass name="Consortium">
        <set name="Owners" table="ConsortiumOwners">
            <key column="Consortium"/>
            <many-to-many class="Owner" column="Owner"/>
        </set>
    </union-subclass>
</class>

You might notice how simple the mapping looks like. For that matter, look at how we could simplify the mapping between horses and owners.

image

The resulting query is… interesting:

image

And now let us move to the more classic inheritance schemes, first, let us look at table per hierarchy:

<class name="Owner" abstract="true">
    <id column="Id"  type="System.Int32">
        <generator class="hilo"/>
    </id>
    <discriminator column="Type" type="System.String"/>
    <set name="Horses" table="OwnerHorses">
        <key column="Owner"/>
        <many-to-many class="Horse" column="Horse"/>
    </set>
    <property name="Name"/>
    <subclass discriminator-value="Company" name="Company">
        <property name="CompanyRegistrationId"/>
    </subclass>

    <subclass discriminator-value="Individual" name="Individual">
        <property name="Email"/>
    </subclass>

    <subclass discriminator-value="Consortium" name="Consortium">
        <set name="Owners" table="ConsortiumOwners">
            <key column="Consortium"/>
            <many-to-many class="Owner" column="Owner"/>
        </set>
    </subclass>
</class>

With the following schema:

image

In this case, Owner.Type is the discriminator for the Owner hierarchy. And the query that is generated is:

image

And finally, we have table per subclass, which we use the following mapping:

<class name="Owner" abstract="true">
    <id column="Id"  type="System.Int32">
        <generator class="hilo"/>
    </id>
    <set name="Horses" table="OwnerHorses">
        <key column="Owner"/>
        <many-to-many class="Horse" column="Horse"/>
    </set>
    <property name="Name"/>
    <joined-subclass name="Company">
        <key column="Id"/>
        <property name="CompanyRegistrationId"/>
    </joined-subclass>

    <joined-subclass name="Individual">
        <key column="Id"/>
        <property name="Email"/>
    </joined-subclass>

    <joined-subclass name="Consortium">
        <key column="Id"/>
        <set name="Owners" table="ConsortiumOwners">
            <key column="Consortium"/>
            <many-to-many class="Owner" column="Owner"/>
        </set>
    </joined-subclass>
</class>

This gives us this schema:

image

Using this approach, we query them using:

image

I would like to point out that this is just a sample of the type of things that we can do with NHibernate, and that I took the easy path doing so. There are actually more complex inheritance option available (discriminator with joined classes, for example) and it is pretty easy to change things even further.

And, of course, it is entirely possible to take things in the other direction, and have a single physical data model that can map to several domain models.

Have fun…

time to read 8 min | 1460 words

Today was the first day of my NHibernate course, and I think that it might be good to point out a few of the samples that we worked with. Those are pretty basic NHibernate queries, but they are probably going to be useful for beginners.

Let us take my usual Blog model, and see what kind of queries (and results) we can come up with:

image

Let us find a blog by its identifier:

var blog = s.Get<Blog>(1);

Which results in:

image

We can also try:

var blog = s.Load<Blog>(1);

Which would result in… absolutely no SQL queries. You can look at a more deep discussion of that here.

Now, let us try to search by a property:

var blogs = s.CreateCriteria<Blog>()
.Add(Restrictions.Eq("Title", "Ayende @ Rahien"))
.List<Blog>();

Which results in:

image

If we try to make the same with HQL, it would look:

var blogs = s.CreateQuery("from Blog b where b.Title = :title")
.SetParameter("title","Ayende @ Rahien")
.List<Blog>();

Which results in slight different SQL than using the criteria:

image 

What about trying a more complex conditional? Let us try to see comparing two properties:

var blogs = s.CreateCriteria<Blog>()
.Add(Restrictions.Eq("Title","Ayende @ Rahien"))
.Add(Restrictions.Eq("Subtitle", "Send me a patch for that"))
.List<Blog>();

Which results in:

image

Let us do that again, but using two properties using an OR:

var blogs = s.CreateCriteria<Blog>()
.Add(Restrictions.Disjunction()
.Add(Restrictions.Eq("Title", "Ayende @ Rahien"))
.Add(Restrictions.Eq("Subtitle", "Send me a patch for that")))
.List<Blog>();

Which would result in:

image

We can also execute the same SQL using the following syntax:

var blogs = s.CreateCriteria<Blog>()
.Add(
Restrictions.Eq("Title", "Ayende @ Rahien") ||
Restrictions.Eq("Subtitle", "Send me a patch for that")
)
.List<Blog>();

Doing the same using HQL would be:

var blogs = s.CreateQuery("from Blog b where b.Title = :title and b.Subtitle = :subtitle")
.SetParameter("title","Ayende @ Rahien")
.SetParameter("subtitle", "Send me a patch for that")
.List<Blog>();

Which results in:

image

And changing that to an OR is pretty self explanatory :-)

var blogs = s.CreateQuery("from Blog b where b.Title = :title or b.Subtitle = :subtitle")
.SetParameter("title","Ayende @ Rahien")
.SetParameter("subtitle", "Send me a patch for that")
.List<Blog>();

Giving us:

image

Let us try something a bit more complex, finding a blog by a post title:

var blogs = s.CreateCriteria<Blog>()
.CreateCriteria("Posts")
.Add(Restrictions.Eq("Title","NHibernate Rocks"))
.List<Blog>();

That gives us:

image

You will note that we force a load of the Posts collection. We can try something else, though:

var blogs = s.CreateCriteria<Blog>()
.Add(Subqueries.PropertyIn("id",
DetachedCriteria.For<Post>()
.Add(Restrictions.Eq("Title","NHibernate Rocks"))
.SetProjection(Projections.Property("Blog.id"))
))
.List<Blog>();

Which would give us the same result, but without loading the Posts collection:

image

This is a pretty common example of changing the way that we compute complex conditionals when we want to avoid wide result sets.

Let us do the same with HQL:

var blogs = s.CreateQuery("from Blog b join b.Posts p where p.Title = :title")
.SetParameter("title", "NHibernate Rocks")
.List<Blog>();

Which would result:

image

We have the same issue as with the first Criteria API, and we can resolve it in the same way:

var blogs = s.CreateQuery("from Blog b where b.id in (from Post p where p.Title = :title)")
.SetParameter("title", "NHibernate Rocks")
.List<Blog>();

And the same result as in the Criteria API show us:

image

And the final test, let us try to find a blog that has a post posted by a specific user:

var blogs = s.CreateCriteria<Blog>()
.Add(Subqueries.PropertyIn("id",
DetachedCriteria.For<Post>()
.SetProjection(Projections.Property("Blog.id"))
.CreateCriteria("User")
.Add(Restrictions.Eq("Username","Ayende"))
))
.List<Blog>();

And this give us:

image

The same thing with HQL will give us:

var blogs = s.CreateQuery("from Blog b where b.id in (from Post p where p.User.Username = :user)")
.SetParameter("user","Ayende")
.List<Blog>();

And that results:

image

FUTURE POSTS

No future posts left, oh my!

RECENT SERIES

  1. API Design (10):
    29 Jan 2026 - Don't try to guess
  2. Recording (20):
    05 Dec 2025 - Build AI that understands your business
  3. Webinar (8):
    16 Sep 2025 - Building AI Agents in RavenDB
  4. RavenDB 7.1 (7):
    11 Jul 2025 - The Gen AI release
  5. Production postmorterm (2):
    11 Jun 2025 - The rookie server's untimely promotion
View all series

Syndication

Main feed ... ...
Comments feed   ... ...