views:

45

answers:

2

Suppose my WinForms application has a business entity Order, the entity is used in multiple views, each view handles a different domain or use-case in the application. As an example, one managing orders, the other one digging into one order and displaying additional data.

If I'd use nHibernate (or any other ORM) and use one session/dataContext per view (or per db action), I'd end up getting two different instances for the same Order (let's say orderId = 1). Although functionally the same entity, they are technically two different instances. Yes, I could implement Equals/GetHashcode to make them "seem" the same.

Why would you go for a single instance per entity vs private instances per view or per use-case?

Having single instances has the advantage of sharing INotifyPropertyChanged events, and sharing additional (non-persistent) data.

Having a private instance in each view would give you the flexibility of the undo functionality on a view level. In the example above, I'd allow the user to change order details, and give them the flexibility to not save the change. Here, synchronisation between the view/use-case happens on a data persistence level.

What would your argument be?

+2  A: 

You should implement Equals/GetHashCode. This is a recommended practice when using ORMs.

In addition, you should typically stick with the "One View, One Session" mantra. Persist all of your objects when your view changes or loses focus. If you really need to share entities across views... well do it! Sometimes you must.

And once again, because when we are looking at the business objects from an entity and row type of perspective, we should not be concerned with "object" level equality.

snicker
Interesting...but why *should* you implement equals/gethashcode? in other words, why would the ORM builder not just return one single instance of an entity and let .NET handle equality? I guess what I'm aiming at is best practices based on scenario's where it is better to go for private instances, rather than just accepting that is the way it is...
taoufik
Because in most cases, the ORM *can't*. Let's say **you** create an instance of an object. Then you persist to the database. Then you pull the same object out of the DB using the ORM. How does the ORM know that you instantiated that object earlier? all it knows about is what it's got cached. And in many cases, proxies are used on the objects to assist in tracking properties that have changed. You *should* implement them because now you have a way to compare "business object" equality between objects. Do you really care if they are the same instance? If you do, keep your own cache.
snicker
A: 

I can't speak for ORM's, but I think you answered your own question - to an extent. You've provided pros and cons for both options: neither is right or wrong in absolute terms.

The options are only right or wrong depending on your situation. If sharing info makes sense use single-shared instance, but if the ability to undo is more important use multiple / private instances.

You might have other concerns which drive the decisions too: think about the NFR's (or "illities") and the context of the system. For example, if performance is a key concern and you know you're going to have large user bases then that might help suggest one option over the other, or force you to re-think it again from scratch.

Finally - you have "order", what about other entities - how are they being handled?
Or, if you don't have any, what will happen when/if you do? Would that have any imapct on your architecture?

Adrian K