views:

34

answers:

2

I've got a simple domain object, Movie, with the following constructor:

public Movie(string title, int year = 0, Genre genre = Genre.None, int length = 0, IEnumerable<string> actors = null) { ... }

There is no ID parameter, as there couldn't be a way to know up front what the ID would be. Movie does have an int Id property.

This object would be sent to my MovieRepository, which uses NHibernate. The repository has a method for adding movies: void AddMovie(Movie movie) { ... }.

Suppose I want to interact with the object after it is inserted, perhaps to display the data or change properties. I would need the object with the Id property set, or would need to know the ID generated by the database.

Should I change my AddMovie method to return the same movie with the ID set, or should I return the resulting ID value? Or perhaps something else?

A: 

I would recommend change AddMovie to return the newly created Movie with its Id assigned.

Since the Session.SaveOrUpdate() method already returns the object with its Id, that is quite straightforward.

Pedro
+5  A: 

After Session.Save() or Session.SaveOrUpdate() is called, the id will be returned to you for future queries.

See Also this question.

0A0D
Thanks for the advice. I'm able to receive the ID, then get and return the newly saved object.
Grant Palin
@Grant: I'd also recommend you get this book.. it helped me alot: http://www.amazon.com/NHibernate-Beginners-Guide-Aaron-Cure/dp/1847198902
0A0D