I'd like to take advantage of .NET's data frameworks, but I'm a bit confused about how they work, and I'm not sure if what I want is possible. (I've done much of this with PHP frameworks, so I should hope .NET can do the same!) In short, I'd like my business logic to be database-agnostic, and in some sense, agnostic of the fact that it's a database at all.
Queries like this irritate me:
var productNames = from p in context.Products where seller.SellerID == mySeller.SellerID select p.Name;
Instead, I'd like to do:
var productNames = from p in context.Products where seller == mySeller select p.Name;
In essence, I want my business logic to not servicios sociales about the ID mappings. Partly, this is because in a few tables, IDs have real meaning. (Some of the tables I'm working with are imported from another application domain, and I need to be able to work with those IDs directly, but for my own types the IDs are incidental.)
It's a minor point in the example, but in general, I want to be able to work entirely in the world of objects and more or less forget about the realities of the underlying database. Is this just a matter of defining equivalence operators for my classes?
In summary: What .NET data framework gives me the cleanest database abstraction to the world of objects?
preguntado el 03 de mayo de 12 a las 17:05
If there is a relationship between Products and Sellers (I have no idea what seller is in your code examples), then EF let's you do p.Seller (or p.Sellers if there can be multiple sellers per product).
But that doesn't seem to help what you want to do.
seller == mySeller is not at all clear. What determines equality? Normally, you have a primary key to establish equality. And so comparing IDs still makes sense. In addition, it is generally also much more efficient.
Otra opción es sobrecargar el
== operator. This allows you to write a method that compares IDs, which is not visible to the user of the operator. However, this may or not work in your queries because it can't be converted to SQL.