Reputation: 6158
Several of our apps use multiple databases from the example below - each in their own separate DBML file. The problem is, MVC by convention puts them all in namespace AppName.Models
causing class name conflicts.
Which of the two options is the better fix and why:
1.) Putting them in separate namespaces. To keep stylecop/resharper happy, they would go in their own subfolder:
**but now in code, all uses of them have to be Live.Customer
and Crm.Customer
to differentiate between objects.
Models
folder. On top of that, in order to keep the same naming for Helper
file code reuse - even apps that only use one database would need a subfolder in Models
, which I certainly never see people doing in MVC2.) Prepending all object names in one or both DBML designers with a prefix. This is my current approach. The Live database has Customer
and Order
objects, while the Crm database has CrmCustomer
and CrmOrder
. They all stay in the same namespace and /Models
folder. This, however has two main drawbacks:
CrmCustomer.CrmOrders.First().CrmOrderType
Marsha Marsha MarshaHelper
files we have to do a lot of find/replace. This is particularly evident in Helper
files that get added to every app like error/activity logging.So I'd like to hear from other experts which of the two strategies they use, or something else entirely. It seems like a pretty common occurrence to have at least some name conflicts between databases. Thanks
Example table names:
Live Database:
Intranet Database:
CRM Tool Database:
Upvotes: 0
Views: 822
Reputation: 738
If I understand your problem correctly, you have two additional possible solutions:
You can modify the namespace of the entities generated by the DBML. Assuming you use T4 templates to generate them, you can right-click on the *.tt file, and go to properties. There is a namespace property that you can set to your own custom, and therefore unique, namespace:
A second, similar option would be to have each dbml and the generated classes be contained in their own project, and their own namespace associated with it. So in this case you would have three new projects:
You would then add a reference to the projects when you want to consume them.
The benefit to this, in my opinion, is that it is very likely that tomorrow you could have a project that needs to reference Live, CRM, and a brand new database. In this case you would simply take a dependency on the projects you've already created (binary, or code -- my preference is binary, but YMMV), and that part of this 2nd project is complete.
In my opinion, do not decorate you classes (your option 2). That will be very difficult to maintain. There is nothing inherently wrong with your option 1, and I have done that as well, but for most of my current solutions I create projects for the reusability factor.
Upvotes: 1
Reputation: 19857
So, I question the design that leads you to needing two identical databases in code. That aside, I think option 1 is better. Here is my reasoning:
using
level, and not in every single reference to the code. In classes that need both, you aren't avoiding the prefixses in either case. So option #1 wins out in the only case that matters.Upvotes: 0