Browsing:

Tag: Eloquera

NoSQL: Eloquera and ASP.NET MVC 3 (and Ninject)

I thought today I'd give Eloquera a swing, just to do something fun and new before starting the other obligations of the day. Eloquera is another objected oriented database, like Db4o or MongoDb. The difference is that Eloquera is natively .NET and it's free whereas db4o is not. But I honestly do not prefer the one above the other. I'm just trying things out.

I wrote a quick web application as a POC.

First, download Eloquera from here. I took the x64 .NET 4.0 version and installed it..
I edited the C:\Program Files\Eloquera\Eloquera Server 3.0\Eloquera.config file so that the Eloquera files are in c:\temp\db by adding DatabasePath="c:\temp\db":

  <Server ServerPort="43962" 
            DatabasePath="c:\temp\db"
            Trace="true" 
            InMemoryAllowed="true"
            Secure="true" 
            AllowUsers="" 
            AllowGroups="Everyone" />
    <SmartRuntime Smart="true" />

Oh and don't forget to take a look in the Samples directory in the program folder.

Then I created an new ASP.NET MVC 3 application and edited the Global.asax.cs. Here is the relevant portion:

Here are the gotcha's:

  • The OpenSession() method opens the database and if it doesn't exist, a new one is created with some dummy data.
  • I use the session-per-web request pattern (the lifetime of a session is the same as a web request). You can see that here in the complete Global.asax.cs.
  • I do not have to deal with Transactions with Eloquera.
  • I have to register types explicitly, else the Views won't recognize them: db.RegisterType(typeof(iSay.Story));

And then I only have to get the current session in my Repositories with (private DB db = MvcApplication.db;) and I'm good to go just like any other app!

using Eloquera.Client;

namespace iSay.Infrastructure
{
    public class StoryRepository : IStoryRepository
    {
      private DB db = MvcApplication.db;
        public IEnumerable<Story> getStories()
        {
           return db.ExecuteQuery("SELECT Story").OfType<Story>();
        }

        public void Add(Story s)
        {
            db.Store(s);
        }

        public int getNumberOfComments(Story s)
        {
            var comments = s.Comments.Count();
            return comments;
        }
    }
}

The controller (I used Ninject again, as you can see I injected the controller with IStoryRepository):

using iSay.Infrastructure;

namespace iSay.Controllers
{
    public class HomeController : Controller
    {
        private IStoryRepository _repo;

        public HomeController(IStoryRepository repo)
        {
            _repo = repo;
        }

        public ActionResult Index()
        {
            var stories = _repo.getStories();
            return View(stories);
        }

    }
}

Hmm, that's almost embarrassing, as easy as this is.

You can view the source code here. Maybe I'll spice it up later but I have to go now.