views:

739

answers:

6

I'm working on a .NET application using Silverlight on the client side. Now I've come to the point where I want to throw out my static dummy data on the server side and add a database instead.

For the database I'd love to use one of them ORM's where I can simply tag my model classes and the database tables are built for me. I did some tests with Groovy and Grails earlier, and thought GORM did a smooth job. What's the best way to set up a database in .Net?

The first thing that strikes me is to use nHibernate. I don't really know anything about nHibernate, but I've heard numerous people mention it with enthusiasm. But then I see that ADO .Net also is an ORM, which is built into the framework.. Does nHibernate outbeat ADO? And what's the deal with LINQ? I see that's listed as an ORM too, but I though LINQ was more for the query part? Can I "define" the database through LINQ?

Any comments and recommendations are welcome. I'd also love to hear if you have opinions on what database to use. I assume MS SQL Server is the easiest choice?

+2  A: 

Have you looked at .NET RIA Services?

What is .NET RIA Services?

.NET RIA Services: From Vision to Architecture

.NET RIA Services Samples

Mitch Wheat
No I haven't.. Actually never heard about it. Will check it out - thanks!
stiank81
I might have misunderstood, but from what I can see .NET RIA Services is not an ORM and hence not something I can use instead of e.g. NHibernate. It seems to be replacing the WCF part in my project, binding the client and server side together. Correct? That being said - .NET RIA Services certainly looks like something we should look further into!
stiank81
+2  A: 

For a system without an existing database, I think NHibernate + FluentNHibernate automappings would be worth a shot:

http://wiki.fluentnhibernate.org/show/AutoMapping

I haven't used automappings before (I'm given a schema to use), they would be my first idea to investigate.

The second choice, and probably more reliable / proven is ActiveRecord:

http://www.castleproject.org/activerecord/index.html

Again, haven't used this either, but I believe you mark your entities with attributes to specify the mapping.

The key to both of them is that NHibernate can export the mapping to the database creating the tables automatically.

I also have no idea how this impacts Silverlight.


In answer to comment (easier to type here than comment):

What NHibernate supports is the ability to take the mapping files defined in the project and export them to the database. This means that you define your schema once (your application).

Now, normally those mapping files are xml. This isn't a big deal, but obviously room for improvement.

FluentNHibernate lets you (a) define mapping files in code or (b) automatically create the mapping files from your entities via convention. I use method (a) because my db is terrible and there is no convention in this world for it, but for your use I would recommend (b) which is automapping. The wiki link above gives a quick example of how it can map Product and Shelf.

Active Record works by letting you define the schema not in XML but as attributes on your entity. It is much more than that though as it also implements the ActiveRecord pattern that RoR uses (Entity.Save, etc). I have never used this, but it is widely used.

One great thing about the NHibernate schema generation is unit testing. I can create a test database in SQL Lite in a few lines of code, export the schema, populate with test data, run the test, and tear it down. It makes integration testing easy, and also helps test that queries are working properly.

I don't know if these are the best solutions as I really only use NHibernate so can't comment on Linq2SQL or EF or other ORM.

eyston
As commented on one of the other answers I don't think this choice makes any impact on Silverlight.. Thanks for mentioning ActiveRecord and Fluent - will check it out. But I though NHibernate supported automapping? No? How do you define the db-schema with NHibernate then? You need to create it yourself manually?
stiank81
edited post to answer comment ... too long.
eyston
+6  A: 

NHibernate and Silverlight:

One of the NHibernate contributors, Ayende Rahien, recently posted a blog post about NHibernate and Silverlight:

I got a few questions about NHibernate and Silverlight. That is actually a very easy thing to answer.

Don’t even try. They don’t get along. In fact, they aren’t even going to get along.

Silverlight doesn’t have System.Data.IDbConnection, and you can safely assume that that it somewhat important to NHibernate.

So, running NHibernate inside a Silverlight application, presumably in order to access a local database is out. But I don’t think that this is what most people actually had in mind when they ask about NHibernate and Silverlight. They want to know about NHibernate on the server and Silverlight on the client.

And that is easy enough to answer as well, it is going to work just like any client / server system. All the same rules apply.

So NHibernate should work as long as you don't plan to use it directly from the Silverlight client.

NHibernate:

NHibernate is a great ORM but it has a quite steep learning curve, so you should be prepared to invest some time into learning the framework if you choose NHibernate. If you make that investment you will be rewarded by the flexibility and power that NHibernate provides.

Castle ActiveRecord:

Castle ActiveRecord is a framework that is build on top of NHibernate, and hence is quite similar to NHibernate. It reduces the learning curve a bit, since it adds some additional abstractions. As the name implies it is build for use with the ActiveRecord pattern, and includes a base class that gives you quite a lot of functionality if you don't mind using their ActiveRecord base class.

LINQ to SQL:

LINQ to SQL and the ADO.NET Entity Framework are two ORM:s that are included in the .NET Framework. LINQ to SQL is a smaller and simpler framework than Entity Framework, but it has some nice features, and is quite easy to get started with.

Entity Framework:

Entity Framework is quite easy to get started with as well, but it has a few quite big problems in the current version, since it is still in version 1. However, the next version of Entity Framework will improve and fix many of the current shortcomings.

LINQ and Schema Generation for the Frameworks:

All of these frameworks have support for using LINQ as the querying language. LINQ to SQL and NHibernate can generate a schema for you based on your domain classes and mapping. Entity Framework can not generate a schema in v1, but v2 will add that functionality.

Erik Öjebo
"Now I've come to the point where I want to throw out my static dummy data on the server side and add a database instead." -- this to me sounds like his Silverlight application won't need to be changed at all and he just has a server side returning POCO. I guess that could be clarified because I'm not really sure how Silverlight would impact the decision of the server side ORM (it may or may not?).
eyston
Yes, I agree, but it might be worth mentioning for completeness when talking about NHibernate and Silverlight.
Erik Öjebo
agreed, and nice answer.
eyston
Yes, let me clarify.. Mentioning Silverlight was probably irrelevant in this question. My system will be like you mention. Silverlight on the client side - C# behind the silverlight-code - communicating with the server side through WCF - typically returning POCOs. The database and ORM will be on the server side only, and hence not being touched by the Silverlight stuff. So; no, Silverlight shouldn't affect my choice of ORM.
stiank81
From your answer it sounds like I'd like to use nHibernate. I'm a little frighten by the mentioned learning curve though.. Would like to use LINQ to do the queries towards the DB, but if I've understood correctly nHibernate and LINQ go well together. So; I can tag the code with some nHibernate stuff and it will create my database schema automagically? But what about DB? Should I use MS SQL Server, or does nHibernate (and LINQ?) support other databases out of the box? E.g. DB2?
stiank81
NHibernate supports a whole bunch of databases like DB2, MS SQL Server, Oracle, MySQL, SQLite, PostgreSQL etc. NHibernates Linq implementation is now at version 1.0, so it should be quite stable.About schema generation, yep you create your classes and then some metadata to tell NHibernate how those classes should be stored in the DB and NHibernate can then create the DB for you.
Erik Öjebo
I think the NHibernate learning curve is greatly reduced by having a clean database schema to work from. I know for me the hardest part was trying to figure out how to map some screwed up db's that I had no control over. The other tricky part with NHibernate is just relaxing and letting it do as much of the work for you as possible. A lot of times the first attempt is to abstract away NH or use it as little as possible, but once you just let it do its thing it is magic (imo).
eyston
So; can't say this is what we'll end up with, but will give NHibernate a shot, with some LINQ for the queries. Thanks for clearing up a few things!
stiank81
A: 

If you have used CodeSmith before and would like to auto-generate a lot of your LINQ to SQL code you might want to take a look at PLINQO.

Secondly, there is also a tool called SubSonic that can also create a LINQ to SQL data access layer with minimal trouble.

Creating a data acess layer with either of these tools should give you assemblies that can be accessed by your Silverlight user interface.

Oh note, CodeSmith itself does cost money while the PLINQO template and SubSonic generator are free to use.

Good luck with your project.

Chris
A: 

SubSonic has already been mentioned, but with apologies to Chris, I think it's worth pointing out that SubSonic can generate a database from your model classes for you. Since you specifically asked about that feature.

Joel Mueller
A: 

I would absolutely use ECO. It is free up to 12 classes and have a lot of useful features to offer. It is centered around the model and use OCL to navigate in the model. OCL is a lot easier to use than SQL or LINQ in my opinion.

for example this constructed expression in OCL:

Person.allinstances.address->select(hasGeoPoint).streetname

In this example there are 2 classes, Person and Address. A Person can have an address and an Address can have a geoPoint (in this case this is indicated by a boolean flag) and a streetname. The expression above filter all streetnames that have a geopoint from all persons addresses.

Another important feature in ECO is derived attributes that let you calculated values from other attributes (that in turn may be derived). The value is then cached from the first query taht saves CPU time. ECO can also be used from an existing database.

Roland Bengtsson