F# Type Providers and WebAPI

by dotnetnerd 4. April 2012 10:35

imagesLater this month on the 25th I will be speaking along with two other speakers about webframeworks at an ANUG meeting. The frameworks covered are ServiceStack, Nancy and WebAPI where I will cover the latter.

While I have been preparing my eye cought a new feature in F#, which I think is one of the first really good stories for F# that makes sence outside akademia and science. Type Providers give us a way to access data sources that provide its own metadata, and this fits nicely with building WebAPI services.

More...

MongoDB – getting better for .NET

by DotNetNerd 6. March 2010 16:24

Since my last post Rob Connery has joined the team working on MongoDB, and he recently blogged about the latest additions he has made on github. I couldn’t resist taking a look at the latest changes, and even though there is still some way to go it is nice to see the improvements to the drivers for .NET.

My last sample is already depricated, so Ill shamelessly steal his sample, and modify it slightly just to see the new bits running with entities that I worked with last. So Ill recommend looking at the sample from the blogpost, since I’ll be using his session class.

First thing to notice from Robs sample is that I don’t have to think much about mapping classes to documents any longer. One thing that is already depricated even from Robs own sample is that it is now a requitement that the class has an identifier. So going with the simplest thing possible I added an ID integer.

using (var session=new Session()){
   session.Add(new Actor { ID = 1, Name = "Hans", Age = 40, Gender = Gender.Male });
   session.Add(new Actor { ID = 2, Name = "Eva", Age = 22, Gender = Gender.Female });
}

So comparing to Db4o, we are now closer to a similar experience. One of the things I’ll wait to look at, but I see as fairly importait is how either will handle Lazy<T> properties and other kinds of more advanced scenarios.

With the above objects saved the data can be accessed using a Linq query like so:

using (var session=new Session())
{
   var actors = session.Actors;
   foreach (var actor in actors.Where(a => a.Gender == Gender.Male))
   {
       Console.Write(actor.Name);
   }
}

The LINQ provider is still not too far along so I’ll leave it alone for now, but it is also one of the places where it will be interesting to compare e.g. db4o and MongoDB in the future.

Tags: ,

Db4o – first look

by DotNetNerd 21. February 2010 13:12

As the next step in my recent adventures into NoSQL country I decided to take a look at db4o. Shortly put I hope it will give me more than the document based databases, like Mongo that I have also been taking out for a spin.

My interest in NoSQL has been kindled further than it already was because of blog posts from Rob Connery, who looked into practical approaches and posts on CodeBetter provokingly entitled Using an ORM is like kissing your sister. Basically what they are all saying is that it is very limiting that we always start out with a relational database without even considering this very very important architectural decision.

Object vs Document

Similar to the document based database, the big win is that there is no schema, and no need to map between objects and a relational model. Actually with document based databases you do map to key/value pairs, so it’s not the whole truth, but still simpler than mapping to a relational model.

Mapping can be expensive in two ways – because it takes time to write the code, and it does warrant a certain overhead. On top of that db4o as an object oriented database has a concept of relations between objects. This means that rich querying capabilities are build in and that it performs really well.

When comparing to MongoDB the big thing missing is that it basically works on a flat file out of the box. So you need something to handle concurrency and stuff like that. Rob Connery posted a nice sample of how this could be done, and its really quite straight forward, because db4o does provide all you need to do it.

The basic stuff

In the interest of simplicity I’ll just sample how to work with db4o directly. Also this will illustrate just how straight forward it really is. The first thing that brought a smile to my face was how little I had to do to save and query a bunch of objects.

using (IObjectContainer context = Db4oEmbedded.OpenFile(Server.MapPath("myDatabase.yap")))
{
    var empolyeeToSave = new Employee() { Name = "John", Card = new TaxCard() { Number = "12345678" } };
    context.Store(empolyeeToSave);

    IList<Employee> retrievedEmployees =
        context.Query<Employee>(employee => employee.Card.Number == "12345678");
}

 

As you can see it’s very straightforward stuff. Besides this type fo querying which takes a predicate, there is also a QueryByExample, which works as one would expect. Another nice thing is that all calls to the Store method make up a transaction, so there is a Rollback method available. When the context is disposed the convention is that Commit is called for you, so when you don’t need rollbacks you don’t have to think about it.

Indexing is also possible with db4o, and it is also very simple to do, so if for instance I would like to apply an index for the name property on my Employee I could do this.

Db4oFactory.Configure().ObjectClass(typeof(Employee)).ObjectField("Name").Indexed(true);

Moving along

Some of the first things that I have heard people think about when I say object/document based database is how to handle versioning and querying. With querying in this case I mean how to enable a scenario similar to opening SQL Manager and writing some queries to take a look at the actual data.

The versioning part may take a little work, but there are methods to help with e.g. renaming directly baked in - and besides, it takes work no matter what kind of database your using. On top of that my thought is that both of these issues can be addressed with a dynamic language. My plan is to take a look at this at some point, and that it will fit nicely because my copy of IronRuby Unleashed should be on it’s way :)

b_DBA

Tags: ,

MongoDB – how to get started

by DotNetNerd 3. January 2010 11:17

Even though I have been pretty disconnected during the holydays I couldn’t help read that Rob Conery wrote quite a bit on his blog about his thoughts on Ayendes “EF vs NHibernate” discussion. Rob Conery argues that he is tired of these discussions and that we need to look at the NOSQL movement and instead focus on alternatives to relational databases.

I found this pretty interesting and decided that it was finally time for me to take a look at one of the document based databases out there. As Rob mentioned MongoDB I thought this was a good a place to start as any. Based on my experiences I then decided to do this post on what I learned about getting MongoDB up and running. So first of all, the steps to get a MongoDB server running are:

  1. Download and unpack from: http://www.mongodb.org/display/DOCS/Downloads
  2. Create folder C:\data\db and make sure the server has read and write access
  3. Run the server from cmd. Go to dir where you unpacked (eg: "c:\MongoDB\bin") and run "mongod"

You can now work with the database from the commandline, but to start writing some code you need to get a driver that suits you. In my case I went with a driver so I can write my code in C#. This driver along with a early edition of a LINQ library can be downloaded from http://github.com/samus/mongodb-csharp/downloads 

This is all you need to get going, so just create a new project and start coding!

A good sample can be found here http://blog.dynamicprogrammer.com/2009/11/10/UsingMongoDBFromC.aspx and if your into F# this might be interesting http://gist.github.com/218388

The first sample focuses on two ways to handle converting objects into documents.

  1. Serialize object(hierarchy) into JSon using JSonConverter - which was the sample I focused on.
  2. Make entities implement the IMongoEntity interface – which basically just makes your entity expose a Document property which then contains the state for the entity.

Of course I wont cheat you from seeing the bit of code I started out with, which is heavily inspired by the before mentioned blog posts.

//Connect to the database
var mongo = new Mongo();
mongo.Connect();
var db = mongo.getDB("movieReviews");

//Access the collection that you wish to work with
var movies = db.GetCollection("movies");

//Add a document
var movie = new Document();
movie["title"] = "Star Wars";
movie["leadActor"] = JsonConvert.SerializeObject(
 new Actor() {Name = "Mark Hamill", Age = 26, Gender = Gender.Male});
movie["releaseDate"] = new DateTime(1977, 5, 25);
movies.Insert(movie);

//Do a query
var result = from mov in movies.AsQueryable()
             where (string)mov["title"] == "Star Wars"
             select mov;

//Write out info from document
foreach (var document in result)
 foreach (string key in document.Keys) Console.WriteLine(document[key]);

//Deserialize and write name
Console.WriteLine(JsonConvert.DeserializeObject<Actor>((string)result.First()["leadActor"]).Name);

//Disconnect from database
mongo.Disconnect();

 

You'll also need this simple class for the sample:

public class Actor
{
 public string Name { get; set; }
 public int Age { get; set; }
 public Gender Gender { get; set; }
}

public enum Gender
{
 None = 0,
 Male = 1,
 Female = 2
}

Tags: ,

Who am I?

My name is Christian Holm Diget, and I work as an independent consultant, in Denmark, where I write code, give advice on architecture and help with training. On the side I get to do a bit of speaking and help with miscellaneous community events.

Some of my primary focus areas are code quality, programming languages and using new technologies to provide value.

Microsoft Certified Professional Developer

Microsoft Most Valuable Professional

Month List

bedava tv izle