Fregas pretty much nailed it. However, this is less of a Fluent NHibernate specific issue, and more a general architectural one. You would be better served by asking this either at the nhusers mailing list<http://groups.google.com/group/nhusers>, or even on somewhere like the altdotnet list<http://tech.groups.yahoo.com/group/altdotnet/>. There's more people frequent those lists than here.
On Tue, Aug 4, 2009 at 6:03 PM, Fregas <[email protected]> wrote: > > Hi Brian, > > You are indeed confused. Let me see if I can help. > > "1. The model is nothing but entity mappings; a class modeling one > table should not involve any other tables. That sort of thing belongs > in the repository classes. " > > Your model isn't just "entity mappings". Your model are the > business / domain objects that hold data and behavior (methods) and as > such, they will often hold references to each other, and you can use > NHibernate's lazy-loading or pre-fetching to get those related > objects. As such, there is nothing wrong with doing exactly what you > were doing before, where user1.AddFriend(user2) method creates a > relationship object and adds it perhaps to a relationship collection > if i'm understanding you correctly. In fluent, you would use HasMany > in your user mapping class so that a user has many relationships, and > each relationship has a reference to both users and a property for the > RelationshipType. This would not go in repository classes but in > Fluent mapping classes. > > "For the sake of "dependency injection", I should not have my User > class create a Relationship object; I should create this Relationship > object separately and then pass it to the User class. " > > Again, not so. Its okay for the User (IMHO) to instantiate another > domain object directly without using DI. So the user can instantiate > relationship objects thru its methods. Where you want to use D.I. is > between layers. So the presentation/ui layer such as web forms or MVC > controllers would not instantiate repositories, they would use DI to > hand them repositories thru their contsructors which would only spell > out the interface. I even have instantiate domain objects such as > users in my presentation layer because domain objects are fairly > technology neutral and not having references to things like nhibernate > or other similar services that are likely to change. However my > domain objects do not have any references to my repositories typically > or to nhibernate or anything but pure .NET code. > > "But now this has me completely confused, because instead of making a > method to allow the controller to do "user1.AddFriend(user2)", the > controller instead has to know about the Relationship class, create a > new Relationship object, then call a "userRepository.AddRelationship > (user1, newRelationship)" method I'll write. This seems more > complicated. " > > I'm not sure if you are talking about an MVC controller or some other > controller. But typically the Repository just does loads and saves of > some aggregate root object and you manipulate the domain objects to > handle the relationships. > > So an asp.net MVC controller might do something like: > > public ActionResult AddFriend(int userID, int friendID) > { > var user1 = this.userRepository.FindByID(userID); > var friend = this.userRepository.FindByID(friendID); > > user1.AddFriend(friend); //AddFriend might create a relationship > object, give it a "friend" relationship type, add it to a collection, > etc. > > this.userRepository.Save(user1); //saves user1, friend, and > relationships depending on cascade options in your fluent mappings > } > > So repositories don't do mappings. They just have methods to find, > delete and save objects. The fluent mapping classes determine how > those objects get saved or loaded to the db schema. So in a fluent > mappign you can tell it when you save a user, save its relationships, > etc. > > > Hope this helps. > > Fregas > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en -~----------~----~----~----~------~----~------~--~---
