Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I've been using the SqlCommand class and writing my own T-SQL queries for a very long time. I've stuck with it because I'm comfortable with it, and it probably stemmed from me starting as a windows developer in the days before we had many other great options. I've been doing web development now for about 10 years, and still using it and writing my own query strings. I've written a dynamic class for all my database needs on the fly, all I have to do is write the T-SQL, which I can do quickly and use in any project.
Other developers have got big eyed when they see my code, and encourage me to learn LINQ which I do not like at all.
I'm starting a new project now in ASP.NET MVC and now feel like I should get out of the dinosaur days and move to Entity Framework to handle my data.
I know that the SqlCommand class is the nuts and bolts of the Entity Framework behind the scenes, so I feel like why should I go through the middle man (entity) to achieve the same results and spend time learning something new.
Can someone please explain to me why I need to move on, or if I should stick with what I know?
Well, with SqlCommand... If you're doing it right, you've either already written the equivalent of an ORM, or you are spending a lot of time writing code that does stuff like packaging up arguments, and mapping columns to objects. If you're not doing either of those, then chances are you have unsafe code and are passing around untyped DataSets and then doing lots of casting, or you're doing dangerous concatenated SQL queries that are prone to SQL Injection.
The advantage of of using EF (or any ORM) is that it handles the SQL generation, the object mapping, the parameter packaging, and gives you a very powerful query interface that lets you write multiple queries very quickly.
Why write 10 lines of code when you can write one?
Whether or not you use EF is really a personal choice... but if you're doing serious, object oriented development, you should be using an ORM of some kind (maybe even a micro ORM, like dapper or massive). Or write your own micro-orm for that matter.
It's just a huge pain to use ADO without some kind of automatic mapping technology.
The main reasons to switch to EF is
It is faster to develop with EF once you learnt it
Especially joins are simpler to write in most cases
You get compile time checking that all your columns and tables actually exists
You have the option to use EF migrations that is one of the best database migration tools out there
I switched years ago (via Linq2SQL) and have never looked back.
Go for EF code first and use migrations.
I've been working with NHibernate, EntityFramewrk, LinqToSQL and with bare SQLCommand over the years. ORMs vary in quality of generated commands and the overhead that they require but they provide simplicity and type safety to your sql commands. Which you will not have if you write sql as strings. It is good to know them, as they are great tools for rapid development of small to medium application in my opinion they fail with large and complicated environments.
Recently we have started a new project and decided to use plain old SQLCommand for it with our own Linq to SQL translator. It's doing good for simple queries and we can fall back to dedicated code for more complicated ones. Other thing is that we like to have full control over our database and in my opinion we loose that when using ORM.
I suspect I'll be alone in this opinion, but here goes.
I've thought about this a lot myself. I'm also a SqlCommand and ADO guy, and I expect I will be for at least a little while longer.
This does remind me of a discussion on LinkedIn that you might want to look at here. I'm sure there are lots of sites talking about the advantages, but that has a few developers discussing their takes on the matter.
This is what I had to say on it:
I tend to prefer writing my own database access code, so I go the ADO.NET route. I suppose some might call me a purist (and not in a good way) for that, but I just like the flexibility it gives me. I have full control and I know whether something will or will not work, which saves time looking at documentation if nothing else. Realistically, I use some wrappers I wrote myself to do something similar to Entity Framework, but simultaneously give me more control. It's still all built on ADO, though, so I can always go in and change whatever I want, whenever I want to.
Basically, in general, I don't really see much reason to switch over. I don't think that it makes code any less readable (in fact I like the option of verbosity). I like that by writing my own statements, I can be sure that what I'm doing will work, and I can copy the code directly into SSMS with complete knowledge that it will do exactly the same thing in both places.
The wrappers I've written provide access on a few levels, one where I can write T-SQL directly in (the wrapper here is that I have a string.Format-esque, params-style way of entering parameters), one that's built on that where I can build different types of statements directly with semi-strongly-typed code, and the top-level one that's based on a series of attributes and hints that acts most like EntityFramework.
There are times when I want to use each of these, and I just find it frustrating to be limited as heavily as EntityFramework and other equivalent systems seem to enforce. I want to be the one writing my code so I know what's wrong and how to fix it. It's not like it really takes me all that much time to write a SELECT out, so it wouldn't save me that much to do something else.
And on top of that, as you said, SqlCommand isn't going anywhere any time soon. So I just haven't really found any reason to leave the comfort of having full control.
Edit:
Having read the other couple responses, I guess I have something to add. I assumed you had already written an ORM similar to what I have, but if you're writing out the full SqlCommand text for every call you make and casting everything on the fly, that's very likely to cause you problems. So I'd definitely get away from that. I tend to like, again, having my own ORM to do with what I please (I guess a good example of that is, for instance, I'll automatically parse an XElement if that's what the application needs, or I'll change Sql Spatial data into something more manageable), but you should definitely switch to EF if you're choice is between that and constantly writing out every line involved in making an ADO call. That's going to do way more harm than good.
Edit 2:
I suppose another important thing to remember is that you'll have to keep your database structure tied to your object model if you write your own ORM. EF will keep everything synchronized, which can be nice, but again I still like to have more control over everything and I don't mind doing that work myself.
Related
I have a few very large queries which I need to convert it linq because we are using Entity framework and I cant use stored procedures(breaks compatibility with other data bases).
using tool like linqer didnt even help and even if I get it to work with some mods to generated linq, there is a huge performance issue.
so, what is the best option in a situation like this where EF fails?
please don't ask me to divide it into small queries cause that's not possible.
Moving this to an "answer" because what I want to say is too long for a comment.
It sounds like you're running into an inherent limitation to ORMs. You won't get perfect performance trying to do everything in code. It sounds like you're trying to use an ORM like a T-SQL interface rather than a mapping between objects and a relational instance of data.
You say you want to maintain compatibility between databases but that's already a nonstarter if you consider schema differences from database to database. If you're already implementing a schema validation step so you ensure your code doesn't break, then there should be no reason why you can't use something like views.
You can say you don't want to support these things all day long but the simple point is that these things exist because they address certain problems. If you wholesale abandon them, then you can't really expect to get rid of the problem. Some things the database simply does better.
So, I think you're expecting something out of the technology that it wasn't meant to solve. You'll need to either reevaluate your strategy or use another tool to accomplish it. I think you may even need a couple different tools.
What you've been doing may have worked when your scale was smaller. I could see such a thing working for quite a while actually. However, it does have a scale limit, and I think you're coming up against it.
I think you need to make a determination on what databases you want to support. Saying "we support all databases" is untenable. Then, compare features and use the ones in common. If it's a MS SQL vs. MySQL thing, then there's no reason why you can't use views or stored procedures.
Check out LinqKit - this is a very useful tool for building up complex large EF queries.
http://www.albahari.com/nutshell/linqkit.aspx
I need to write a specialized, embedded database for some .net Apps. The database part itself is no problem (as said, it's specialized), but the access to it from my .net code is.
I was thinking of using LINQ as the only data access (so no ADO.net, which is fine for the scenario), but I've never done that and I wonder what I actually need to implement?
Do I need IQueryable? Is there a set of Standard methods/Interfaces I need to implement? Is there a standard for writing data or do I need to create my own semantics for it?
Do I need IQueryable?
Yes, this is where LINQ hooks up.
Is there a standard for writing data
or do I need to create my own
semantics for it?
Your own. OTOH that is normally the easier part - you normally dout do a JOIN or something when you update a row.
I personally use BLToolkit quite often these days (need something fast and leightweight). They have a SqlQuery object that can manipualte classes to tables. GOod enough for most direct db manipulation (and I dont use change tracking - most of my objets never change but are versioned in the database).
LINQ providers are quite complicated. As is writing an execution engine. Sure you want to go that way?
Writing your own linq provider is very difficult - I looked into it a while back and got discouraged... may still do it though.
See the answers here - a couple of tutorials are linked to.
I would like to know what is the best method for developing a multi-user C# app using the SQL Server2005 as database. This is what I have in mind:
using nhibernate or telerik's openacces orm.
linq
using wrappers. all data from tables load into corresponding objects (at startup) and from that point only delete&update transactions affect the database.
...
I've looked at orm tools but in my opinion they generate a lot of code and i do not know if
it's necessary.
What is the best solution having in mind future changes in the application?
If i would choose the 3rd option how can i ensure that only one users modifies a row in a table(how can i lock a table row which is under modification) ?
Any suggestions or reading material will help!
Thanks!
There are hundreds of ways to solve this, but don't discount ORM. Microsoft's Entity Framework is getting better with every revision. The framework 4.0 bits are pretty good and play extremely well with LINQ.
As for generated code vs your own, try something like Entity Spaces... You have complete control over how the code gets generated and the data access layer is extremely powerful and flexible (not to mention very easy to use). It also plays nicely with LINQ.
I have written a lot of data access code over the years. In the beginning, the ORM tools were rough around the edges and left a lot to be desired. These tools have gone through many iterations since and have become indispensable in my opinion. I can't imagine writing routine after routine that does the same basic CRUD. I did that for years and spent lots of time correcting hardcoded SQL and vow to avoid it at all costs from here on out.
As for concurrency / locking issues, that's a question unto itself. There are many ways to provide locking (the major categories being optimistic and pessimistic). Each has its pros and cons.
If it's multiuser do NOT do #3. The purpose of an DBMS is to handle the multi-user aspects for you. Everything from transactions to access rights are built right in. Going down the path of mimicking that in your code will be difficult to get right. In the past some "engines" like Borland's BDE and MS Access did this. The end result is that you end up dealing with little things like data corruption and consistency errors.
Never mind that as your database grows the is going to take exponentially longer to start.
We typically stay away from ORM tools for a number of reasons, mostly feature / benefit / security concerns. Of course, we are extremely well versed in SQL and can take advantage of the specific features a given db server can offer, which most ORMs can't do. We also tend to tweak the queries based on performance metrics after product release, which would force a recompile of an app for most ORMs. By staying away from this, we can let production DBAs do their job. That may or may not be a concern of yours.
That said a lot of dev teams both like and successfully use the ones you spoke about. I would say to skip Linq-to-SQL in favor of Entity Framework if you're going that route. Linq-to-SQL has all but been replaced by EF.
Save yourself a load of effort and time and use an ORM. In terms of helping you decide which one, there is loads of information/opinion on the web (and StackOverflow!) about which one to use but that'll depend on what your application requirements are (which you haven't described).
I like Linq-to-SQL for small/mid sized apps. It's quick and easy and almost efficient. For bigger apps it'll depend on what types of data transformations and design you have in mind but Linq-to-Entities or nHibernate are probably the most appropriate.
I've been taking a look at some different products for .NET which propose to speed up development time by providing a way for business objects to map seamlessly to an automatically generated database. I've never had a problem writing a data access layer, but I'm wondering if this type of product will really save the time it claims. I also worry that I will be giving up too much control over the database and make it harder to track down any data level problems. Do these type of products make it better or worse in the already tough case that the database and business object structure must change?
For example:
Object Relation Mapping from Dev Express
In essence, is it worth it? Will I save "THAT" much time, effort, and future bugs?
I have used SubSonic and EntitySpaces. Once you get the hang of them, I beleive they can save you time, but as complexity of your app and volume of data grow, you may outgrow these tools. You start to lose time trying to figure out if something like a performance issue is related to the ORM or to your code. So, to answer your question, I think it depends. I tend to agree with Eric on this, high volume enterprise apps are not a good place for general purpose ORMs, but in standard fare smaller CRUD type apps, you might see some saved time.
I've found iBatis from the Apache group to be an excellent solution to this problem. My team is currently using iBatis to map all of our calls from Java to our MySQL backend. It's been a huge benefit as it's easy to manage all of our SQL queries and procedures because they're all located in XML files, not in our code. Separating SQL from your code, no matter what the language, is a great help.
Additionally, iBatis allows you to write your own data mappers to map data to and from your objects to the DB. We wanted this flexibility, as opposed to a Hibernate type solution that does everything for you, but also (IMO) limits your ability to perform complex queries.
There is a .NET version of iBatis as well.
I've recently set up ActiveRecord from the Castle Project for an app. It was pretty easy to get going. After creating a new app with it, I even used MyGeneration to script out class files for a legacy app that ActiveRecord could use in a pretty short time. It uses NHibernate to interact with the database, but takes away all the xml mapping that comes with NHibernate. The nice thing is though, if necessary, you already have NHibernate in your project, you can use its full power if you have some special cases. I'd suggest taking a look at it.
There are lots of choices of ORMs. Linq to Sql, nHibernate. For pure object databases there is db4o.
It depends on the application, but for a high volume enterprise application, I would not go this route. You need more control of your data.
I was discussing this with a friend over the weekend and it seems like the gains you make on ease of storage are lost if you need to be able to query the database outside of the application. My understanding is that these databases work by storing your object data in a de-normalized fashion. This makes it fast to retrieve entire sets of objects, but if you need to select data from a perspective that doesn't match your object model, the odbms might have a hard time getting at the particular data you want.
Currently I am using NetTiers to generate my data access layer and service layer. I have been using NetTiers for over 2 years and have found it to be very useful. At some point I need to look at LINQ so my questions are...
Has anyone else gone from NetTiers to LINQ To SQL?
Was this switch over a good or bad thing?
Is there anything that I should be aware of?
Would you recommend this switch?
Basically I would welcome any thoughts
.
No
See #1
You should beware of standard abstraction overhead. Also it's very SQL Server based in it's current state.
Are you using SQL Server, then maybe. If you are using LINQ for other things right now like over XML data (great), Object data, Datasets, then yes you should could switch to have a uniform data syntax for all of them. Like lagerdalek mentioned if it ain't broke don't fix it.
From the quick look at .netTiers Application Framework, I'd say if you already have an investment with that solution it seems to give you much more than a simple Data Access Layer and you should stick with it.
From my experience LINQ to SQL is a good solution for small-medium sized projects. It is an ORM which is a great way to enhance productivity. It also should give you another layer of abstraction that will allow you to change out the layer underneath for something else. The designer in Visual Studio (and I belive VS Express also) is very easy and simple to use. It gives you the common drag-drop and property-based editing of the object mappings.
# Jason Jackson - The Designer does let you add properties by hand, however you need to specify the attributes for that property, but you do this once, it might take 3 minutes longer than the initial dragging of the table into the designer, however it is only necessary once per change in the database itself. This is not too different from other ORMs, however you are correct that they could make this much easier, and find only those properties that have changed, or even implement some kind of refactoring tool for such needs.
Resources:
Why use LINQ to SQL?
Scott Guthrie on LINQ to SQL
10 Tips to Improve your LINQ to SQL Application Performance
LINQ To SQL and Visual Studio 2008 Performance Update
Performance Comparisons LINQ to SQL / ADO / C#
LINQ to SQL 5 Minute Overview
Note that Parallel LINQ is being developed to allow for much greater performance on multi-core machines.
I tried to use Linq to SQL on a small project, thinking that I wanted something I could generate quickly. I ran into a lot of problems in the designer. For example, anytime you need to add a column to a table you basically have to remove and re-add the table definition in the designer. If you have set any properties on the table then you have to re-set those properties. For me this really slowed down the development process.
LINQ to SQL itself is nice. I really like the extensibility. If they can improve the designer I might try it again. I think that the framework would benefit from a little more functionality aimed at a disconnected model like web development.
Check out Scott Guthrie's LINQ to SQL series of blog posts for some great examples of how to use it.
NetTiers is very good for generating a heavy and robust DAL, and we use it internally for core libraries and frameworks.
As I see it, LINQ (in all its incarnations, but specifically as I think you're asking to SQL) is fantastic for quick data access, and we generally use it for more agile cases.
Both technologies are quite inflexible to change without regeneration of the code or dbml layer.
That being said, used properly LINQ 2 SQL is quite a robust solution, and you might even start using it for future development due to it's ease of use, but I wouldn't throw away your current DAL for it - if it aint broke ...
My experience tells me that using by using linq you can get things done faster, however the actual actions to the database are slower.
So... if you have a small database, i'll say go for it. If not, i would wait for some improvements before changing
I'm using LINQ to SQL on fairly large project right now (about 150 tables) and it is working out very well for me. The last ORM I used was IBatis and it worked well but took alot of legwork to get your mappings done. LINQ to SQL performs very well for me and so far has proved to be very easy to use out of the box. There are definately some differences you have to overcome in transition, but I would recommend it's use.
Side note, I have never used or read about NetTiers so I won't discount it's effectiveness, but LINQ to SQL in general has proven to be an extremely viable ORM.
Our team used to use NetTiers and found it to be useful. BUT... the more we used it, the more we found headaches and pain points with it. For example, anytime you make a change to the database, you need to re-generate the DAL with CodeSmith which involved:
re-generating thousands of lines of code in 3 separate projects
re-generating hundreds of stored procedures
Maybe there are other ways of doing it, but this is what we had to do. The re-gen of the source code was ok, scary, but ok. The real issue came with the stored procedures. It didn't clean any unused stored procedures so if you removed a table from your schema and re-gened your DAL, the stored procedures for that table did not get removed. Also, this became quite a headache for database change scripts where we had to compare the old database structure to the new one and create a change script to update client installations. This script could run into the tens of thousands of lines of sql code and if there was an issue executing it, which there invariably was, it was quite a pain to resolve it.
Then the light came on, NHibernate as an ORM. It certainly has a ramp-up time to it but it is well worth it. There is a ton of support for it so if there's something you need done, more than likely it's been done before. It is extremely flexible and allows you to control every aspect of it and then some. It is also becoming easier and easier to use. Fluent Nhibernate is up and coming as a great way to get rid of the xml mapping files that are needed and NHibernate Profiler provides an excellent interface to see what's going on behind the scenes to increase efficiency and remove redundancy.
Moving from NetTiers to NHibernate has been painful, but in a good way. It has forced us to move into a better architecture and re-evaluate functional needs. NetTiers provided tons of data access code, get this entity by its id, get this other entity by its foreign key, get a tlist and vlist of this and that, but most of it was unnecessary and unused. NHibernate with a generic repository and custom repositories only where needed reduced tons of unused code and really increased readability and reliability.