6 ms·
When you're directly interacting with the database tight coupling is always going to happen, and trying to hide that only adds needless complexity. The nice pa
by Someone1234 9y ago
When you're directly interacting with the database tight coupling is always going to happen, and trying to hide that only adds needless complexity.
The nice part about using Linq and IQueryable instead of e.g. query strings, is that the compiler (and intellisense) can check for some errors at compile-time instead of run-time. Just have to make sure your database model is 1:1 with the actual database.
After you have the data you can throw it into a non-database DTO to re-couple a bit and pass it between layers.
- ianamartin 9y agoI disagree. You can and often should decouple from your database by calling only parameterized stored procedures. Your code only needs to know the name of the proc and the paeans to pass. In non-trivial applications this is where you get the best performance bang for your buck. ORMs are convenient dev tools, but I don’t see them as real options for deploying real apps of any meaningful scale.
- zip1234 9y agoPlenty of non-trivial applications use ORMs. StackOverflow uses an ORM (Dapper).
- kenoyer130 9y agoThere is a world of difference between a Micro-ORM such as Dapper and a bloated mess of an ORM like Entity Framework. They are not even close to the same thing.
- scarface74 9y agoStored Procedures -- oh $diety no. You can't unit test them, they are harder to rollback than code, more difficult to deploy.
- bunderbunder 9y agoYou can, and you should. http://tsqlt.org http://tsqlt.org Being able to write decent unit test suites is one of the biggest things that moved me from inline SQL back toward sprocs.
- scarface74 9y agoI don't do inline Sql. I use Linq and Moq out the EFContext with List<T> https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).aspx https://msdn.microsoft.com/en-us/library/dn314429(v=vs.113).... It's faster, in memory and multiple people can test simultaneously without contention issues. I've done the whole wrap sql tests in a transaction thing, it was slow and wrought with database locks. The added advantage is that if you do it right. You not only can switch between different RDMSs you can switch to anything that has a Linq provider. I have a mix of SQL Server and Mongo and the clients don't know the difference they just pass in Expression like (e => e.Id && e.salary > 50000) and it gets translated at runtime to either MongoQuery or Sql. I've switch between the two and they never knew the difference - just update a nuget package.
- bunderbunder 9y agoIf you're looking for compile time type checking, I'd say you should go one further and just write your data access layer in F#. The SQL type provider gives you much more thorough compile-time error checking than anything I've seen in an ORM. There are a couple different ways you can set it up to use the actual database to do this, thereby eliminating the extra work of having to maintain two different copies of your schema in two different languages, and making sure they're kept in sync.
- i_s 9y agoPeople tend to drop type providers in F# for non-trivial programs, because the benefits need to be extreme in order to justify adding flakiness to the build. Type providers also have the problem of yielding errors to people who may not be in the best position to fix them. In a bigger team, it may be better to have one part of the program be yield errors at runtime, but still compile, so the team can keep on working until it is fixed. (The alternative of course being code generation, which is triggered intentionally.)
- akra 9y agoDisagree with this; because it very much depends on the program being written. From what I've seen they've become more popular with ironically the exception of the SQL ones for the reason you describe. As long as you can get the schema/example file locally on the source tree without the type provider needing to fetch it remotely it saves having to write up a lot of classes by hand. E.g with web UI calls just get the swagger Json and download it into your solution, for rest/json APIs just drop all the example documents into a folder and have the type provider reference that. Once the source data is local the flakiness mostly goes away and it just becomes a much quicker/less error prone way of expressing schema from a wide variety of sources. It's personally saved me a lot of time in development especially when interacting with many data sources at the same time - that's a lot of Dtos I would have to craft myself and I would in this case probably moved to a dynamic language a while ago for the work I do. A type provider that allows local schema would deal with your issue and then you would get type checked SQL queries at compile time without the direct db dependency and connection latency.