gerrylowry:
I do not think it is a problem. In a complex application, one could be connected to multiple databases.
I would hope that with two connections to the same database, there might be some efficiency that one gets automatically.
I'm not a database expert, regardless, this seems normal to me. In our application, we are abstracting to a logical model of our data. The physical model should be more or less irrelevant. We should be able to restructure the physical model without changing the logical model AFAIK.
Regards,
Gerry
I wanted to comment on this. I'm not a database expert, either, but I've been on all sides of them through my career (install, dba, develop, app developer, etc). I wanted to drill down a bit on the pro's and cons of the scenarios here.
Multiple Db's. Using multiple connections in and of itself isn't a problem; doing so synchronously and in series is probably a reliability and performance issue, especially if one considers that you may be taking a network hit to talk to them. Consider that in the above code, with two DBs, you are forced to wait for the first to respond before calling the second. If the entities are not related, there's no good reason for that and the whole thing can (and should) be done using asynchronous calls (though that ups the complexity of the solution). If they are related, you can still use asynch methods, you just have to use callbacks to pass the keys needed between them. In both cases, you have to consider how to notify your user if things went wrong, etc. A potentially more elegant solution would be to use a message queue of some sort (or bus that guarantees reliable messaging) so that you can respond to the user that "all is well" as soon as the objects hit the queue, since the queue's job is to make sure that the transaction happens successfully.
Multiple connections to the same DB. Less of an issue. Yes, ADO.Net (and by extension L2S and L2E) does pool connections, so that you can take less of a hit for opening new ones, but you do still *use* two connections out of that pool, and that's still a finite resource. In a high traffic site, you could still run into issues. While you can change the # of pooled connections, that changes the physical resource consumption profile of your app, and why hasten the rate at which your app uses resources needlessly?
That said, one of the things that most of the modern ORMs do pretty well (L2S, L2E, NHibernate, LLBLGENPro) is minimizing footprint on the connection pool. They grab a connection at teh very last second, and release it as soon as possible. So the danger is probably minimal for using multiple "contexts" (using the generic term) at the same time. So, multiple connections at the same time == bad. Multiple contexts, probably less so.
Finally, I would agree that in a perfect world the logical model would be completely ignorant of the persistence means, and the physical model could be changed willy-nilly. I have found that from a practical standpoint, that's not always the case. Some ORM tools are better than others at allowing that (e.g. NHhibernate is better at that than L2S or L2E), but there are always tradeoffs. If you don't want to expose your properties as virtual, and you're using NHibernate, then you either have to tell it to bypass your properties and go straight for the fields (which you may not want to do) or else you have to accept not lazyloading them. Which may or may not be a good choice for you, performannce-wise. L2S, you have to accep ttha tyou'll only be using SQL Server. The list goes on.
Help those who have helped you... remember to "Mark as Answered"