views:

228

answers:

7

Hi,

I'm wanting to use an ORM tool for an asp.net web app I'm working on. I've put together all my classes and just want to have the data persisted. I downloaded SubSonic 3.0 and began using the simple Repository...which was brilliant. I'm new to ORM tools as my first experience commercially was the DataSets/Stored Procs world for a few years which I'm trying to move on from (even just academically)...anyway, the simple repository was so easy to use...except I'm stuck when it comes to data relationships. I was hoping to have a list property that would be persisted as two tables with a foreign key relationship etc...

I've seen that I could manually add the extra foreign key Id's to objects but am now wondering whether I should go for a different ORM tool, as there seems to be a lot...any help much appreciated!

Note: I've heard that nhibernate will do pretty much anything, but looking at some "start guides" it seemed a world of extra learning and time compared to the SubSonic 3.0 Simple Repository...But if that simplicity isn't available for what I'm after, I'll learn!

edit
Thanks for all your responses and suggestions...I tried out most of the suggestions hoping to avoid nhibernate but wasn't able to...I've bought NHibernate in Action and am taking the time to learn it which I can already see as being the right decision.

+2  A: 

You shouldn't exclude Entity Framework (or LINQ to SQL for that matter).

The reason I said Entity Framework over LINQ to SQL is that the Entity Framework provides the ability to create the database from an Entity diagram (which should be easy to generate from your classes).

Both of those are built in to .NET so they're immediately available with minimal hassle.

Justin Niessner
My impression is that EF v1 does not support model driven development. Are you talking about EF v4 or am I mistaken on EF v1's capabilities?
Michael Maddox
I think you might be mistaken on EF v1's capabilities (but I must admit, I've used EF mainly in VS2010). EF definitely allows you to specify your model (or domain objects)...and then generate your database schema from that model rather than forcing you to create the database schema and then use objects that map to your tables.
Justin Niessner
Justin, is this wrong then? http://stackoverflow.com/questions/293631/building-db-schema-from-entity-framework-modelMaybe you can provide a link for how to do this in EF v1 (I can't easily find anything)?
Michael Maddox
It looks like I was mistaken and what I thought was in EF v1 is actually EF v2 functionality in Visual Studio 2010 Beta.
Justin Niessner
+2  A: 

I really suggest NHibernate, it's not that complex at all once you get into it, and will provide you with a huge range of features instead of locking you into one mindset. I haven't looked back since I started using it, and have experienced some other ORM's but have never been amazingly impressed.

Jay
+4  A: 

Mindscape's LightSpeed is generally pretty simple (it takes a convention over configuration approach), and automatically handles the list scenario you describe. Although it requires foreign key IDs under the surface, it has a designer which will take care of adding those for you (you can drag tables on, or create associations using the designer and the designer will automatically implement the foreign keys). See the getting started screencast for an example.

itowlson
This looks really really really good...but a price tag (for more than 8 objects) might rule it out when plenty of the others are free...
davidsleeps
Hi David, the price for one standard license is less than one hour of most dev's charge out rates so it represents a substantial saving over the time lost to the up-skilling required with other frameworks. If you're looking at higher editions then get in touch - I'm sure we can sort something out that works for both of us :-) [If it's not obvious, I work for Mindscape]
traskjd
I'm trying out the Express edition and wish I was able to drag my classes into the Lightspeed designer...but otherwise am impressed so far
davidsleeps
David: try posting a suggestion in the Mindscape forums -- a lot of the features come from customer suggestions like this.
itowlson
A: 

I faced your exact problem recently. SimpleRepository is fantastic if you're using single entities that arent related. If you are set on the model-first-database-second paradigm (which I was) then I would suggest Fluent NHibernate with automappings. The getting started stuff on the website will have you up and running quickly. The only gotcha is that you have to be a little careful about creating foreign relationships. Make sure both your primary entity and your reference entity both maintain a mapping to each other, otherwise NHibernate will not automatically cascade save a new reference entity you create.

Ie:

(note: list of recipes is public for sake of brevity, you probably want to create an internal field with a custom get/set properties to expose this collection in real life)

public class Recipe {

    public virtual RecipeBook RecipeBook {get; set;}
    public virtual string RecipeInstructions {get; set;}
}


public class RecipeBook {

    public virtual IList<Recipe> Recipes { get; set; }
    public virtual string BookTitle {get; set;}

    public void AddRecipe(Recipe recipe) {

         recipe.RecipeBook = this;
         Recipes.Add(recipe);
    }

}
Alex
+4  A: 

Try DataObjects.Net as an alternative. It is completely different to Subsonic. Composite foreign keys, unified key & version adapters (Key and VersionInfo types + Entity.Key and Entity.GetVersion(...)), truly transparent references (Subsonic does not support references at all) and good LINQ support are just few examples of what's there.

Good starting point is introduction part (start from the tail, if you prefer to look up an example first) from its Manual.

Alex Yakunin
No disclosure that this is your company and your product? No disclosure that your product costs money and Subsonic is free? Wow.
Michael Maddox
This information is open - it's enough to look into my profile.
Alex Yakunin
Btw, Michael, do you seriously think any tool author, employee or affiliate must write such replies in this fashion: "try *my* ToolX"?
Alex Yakunin
@Alex: That is your call as it is strictly your risk to bare. How do you want to be perceived?
Michael Maddox
So you're not answering a direct question, but expecting this from my side? Btw, I just noticed you used misguiding description: "costs money" isn't the same as "either GPL or commercial". In any case, I think such stuff goes far away from this topic, so if you'd like to add anything, please use e-mail.
Alex Yakunin
I think disclosure of commercial interests should be provided in every answer if its relevant. Most people plugging their own product do that. I dont want to look into the profile of every answer to see whether the poster has some commercial interest in me choosing their product.
Alex
+2  A: 

NHibernate rocks. I was productive fairly quickly with it. Check out www.summerofnhibernate.com for great free training videos. They're for an older version but you should be able to figure out the differences as you go.

Mike C.
+1 Summer Of NHibernate is an awesome set of tutorials, they really set me up for more complex NHibernate concepts. A definite starting point for anyone who wants to learn NHibernate
Jay
I have the book NHibernate In Action on its way. I look forward to seeing examples in the latest version and to see more advanced concepts/practices.
Mike C.
A: 

If you are using SQL server as your backend database, I would suggest using Linq to Sql and using the SQLMetal tool that ships with VS2008. You can do just about everything that NHibernate does and you dont have to lift a finger (not to mention no pesky xmls), just run a command on the VS2k8 Command line prompt. The best resource to start would be Scott Gu's introduction available here and then some SQLMetal documentation.

Ritesh M Nayak
Wow, really? LinqToSql is not even close to NHibernate feature wise. SQLMetal is basically not flexible. While SQLMetal has significant advantages over the VS O/R Designer, it also has significant disadvantages. LinqToSql gets very complicated to use in certain scenarios like disconnecting objects from the context and reattaching/updating them later so your "dont have to lift a finger" statement is far fetched at best. Last I checked DBML files are XML and that is a good thing.
Michael Maddox
Are the NHibernate XML mapping files REALLY that complicated? After you complete one, you basically just copy and paste.
Mike C.
I must say I have to agree with both of you. But LinqtoSql lets you use all the lambda style goodness of C# which you will have to do otherwise on a collections object returned from NHibernate. There are some advantages I found using LinqToSql which I didnt with NHibernate. But it could just be my ignorance about using NHibernate effectively.
Ritesh M Nayak
@Ritesh: Are you not familiar with Linq for NHibernate? http://www.google.com/search?q=NHibernate+Linq
Michael Maddox