Answered by:
Entity Framework vs Traditional ADO.Net

Question
-
Hello everyone,
I work for a small software company and we are planing to build a web based application using asp.net mvc and sql server. All my past experience is with traditional style ado.net/ sql queries/ sps etc for data access and manipulation.
My question is, should we look into EF as it is gaining it's popularity. Is there any advantage / disadvantage of using EF over traditional ADO.Net. Are there any proven large scale projects built using EF? What are you people using in your company for dataaccess?
I would be really very thankful for your help.
Regards,
TK Lee
Friday, October 7, 2011 4:13 PM
Answers
-
Hi TK
i have no arguments that would speak against using EF. I am building an enterprise MVC3 application thats running on windows azure and hopefully for tousands of users. I have choosen the code first approach and i dont regret anythng. Its a complete new way in designing and developing testable applications. The advantage of using EF is surely the strong typed entities with the power of linq and the easy transaction management (SaveChanges does the work for you). Together with a nice design you can build really nice architectures. I suggest for MVC projects
- use a dependency resolver like UNITY for all your services
- Implement the WorkOfUnit pattern together with a contextbuilder class
- Use a generic repository for your entities
- Inject the dbContext, unitofwork, repositores via Constructor injection in your services
- Inject the services in your MVC controllers
I made this so far, that i created a testenvironment that allows to use my complete infrastructure. I use for unit testing a SQL CE database. With code first you can let create your database automatically. I used this for creating / initializing the testdatabase for each test run. SQL CE i use because I can copy the database file for each test method.. Also it runs on TFS.
There are many other advantages like scaffolding (if you use it) and soon migration support is implemented as well in ef.
If found a codeplex projekt that looks similiar to my architecture: http://efmvc.codeplex.com/
Hope that helps a bit :)
Holger
Friday, October 7, 2011 6:45 PM -
On 10/7/2011 12:13 PM, tkl33 wrote:> Hello everyone,>> I work for a small software company and we are planing to build a web> based application using asp.net mvc and sql server. All my past> experience is with traditional style ado.net/ sql queries/ sps etc for> data access and manipulation.>> My question is, should we look into EF as it is gaining it's popularity.> Is there any advantage / disadvantage of using EF over traditional> ADO.Net. Are there any proven large scale projects built using EF? What> are you people using in your company for dataaccess?>> I would be really very thankful for your help.>I worked in two shops that were using EF. One shop used EF exclusivelyno sprocs period were used after EF started to be used in solutions. Thesecond shop allowed EF and Linq-2-Entries to be used to query, but foradd, updates and deletes only sprocs could be called by EF.Each shop was using EF in n-tier with MVP or MVC and WCF Web servicewith high volume traffic applications.Here are some links you may want to look at.Compiled views stay in static memory on ASP.NET Web application on Webserver until the ASP.NET Worker Process is recycled.Use a profiler to view the T-SQL generated to make sure you are gettingoptimal performance -- by just changing the Linq query to generate adifferent set of T-SQL can give you better performance.Be aware of using the T-SQL Nolock when querying....I have seen MVC used in applications, but I have never implemented it. Ithink MVC is not flexible.MVP is flexible, it's a derivative of MVC and there are several ways toimplement it.One way is to use events.The second way is to use dependency injection to inject the view and theservice layer into the presenter.The first link will show you how to use the service layer. But you cantake the instance of the service layer and inject it into the presenteralong with the view. You do it in the Page's class constructor and noton the Page_Load() in the example.Public void ClassHelp{public ClassHelp{_presenter = new UserDetailsPresenter(this, servicelayer instance);}}Your Page_Load() would call _presnter.PageLoad();On the IView, you can create a control.public instance IView{public Textbox tbName {get; set;)}ClassHelp would implement IView.public Textbox tbName{get{return TBName; //TBName would be the name of the textbox HTMLcontrol}set{TBName = value;}}In the presenter you can do this.var name = mView.tbName.Text;ormView.tbName.Text = "Larry";You can do any control that way with results being no code in the codebehind dealing with controls.You have a control wires up and event on the codebehind, then pass theobject and event args to the _presenter.EventMethod(object sender, eventargs)Friday, October 7, 2011 7:52 PM
All replies
-
Hi TK
i have no arguments that would speak against using EF. I am building an enterprise MVC3 application thats running on windows azure and hopefully for tousands of users. I have choosen the code first approach and i dont regret anythng. Its a complete new way in designing and developing testable applications. The advantage of using EF is surely the strong typed entities with the power of linq and the easy transaction management (SaveChanges does the work for you). Together with a nice design you can build really nice architectures. I suggest for MVC projects
- use a dependency resolver like UNITY for all your services
- Implement the WorkOfUnit pattern together with a contextbuilder class
- Use a generic repository for your entities
- Inject the dbContext, unitofwork, repositores via Constructor injection in your services
- Inject the services in your MVC controllers
I made this so far, that i created a testenvironment that allows to use my complete infrastructure. I use for unit testing a SQL CE database. With code first you can let create your database automatically. I used this for creating / initializing the testdatabase for each test run. SQL CE i use because I can copy the database file for each test method.. Also it runs on TFS.
There are many other advantages like scaffolding (if you use it) and soon migration support is implemented as well in ef.
If found a codeplex projekt that looks similiar to my architecture: http://efmvc.codeplex.com/
Hope that helps a bit :)
Holger
Friday, October 7, 2011 6:45 PM -
On 10/7/2011 12:13 PM, tkl33 wrote:> Hello everyone,>> I work for a small software company and we are planing to build a web> based application using asp.net mvc and sql server. All my past> experience is with traditional style ado.net/ sql queries/ sps etc for> data access and manipulation.>> My question is, should we look into EF as it is gaining it's popularity.> Is there any advantage / disadvantage of using EF over traditional> ADO.Net. Are there any proven large scale projects built using EF? What> are you people using in your company for dataaccess?>> I would be really very thankful for your help.>I worked in two shops that were using EF. One shop used EF exclusivelyno sprocs period were used after EF started to be used in solutions. Thesecond shop allowed EF and Linq-2-Entries to be used to query, but foradd, updates and deletes only sprocs could be called by EF.Each shop was using EF in n-tier with MVP or MVC and WCF Web servicewith high volume traffic applications.Here are some links you may want to look at.Compiled views stay in static memory on ASP.NET Web application on Webserver until the ASP.NET Worker Process is recycled.Use a profiler to view the T-SQL generated to make sure you are gettingoptimal performance -- by just changing the Linq query to generate adifferent set of T-SQL can give you better performance.Be aware of using the T-SQL Nolock when querying....I have seen MVC used in applications, but I have never implemented it. Ithink MVC is not flexible.MVP is flexible, it's a derivative of MVC and there are several ways toimplement it.One way is to use events.The second way is to use dependency injection to inject the view and theservice layer into the presenter.The first link will show you how to use the service layer. But you cantake the instance of the service layer and inject it into the presenteralong with the view. You do it in the Page's class constructor and noton the Page_Load() in the example.Public void ClassHelp{public ClassHelp{_presenter = new UserDetailsPresenter(this, servicelayer instance);}}Your Page_Load() would call _presnter.PageLoad();On the IView, you can create a control.public instance IView{public Textbox tbName {get; set;)}ClassHelp would implement IView.public Textbox tbName{get{return TBName; //TBName would be the name of the textbox HTMLcontrol}set{TBName = value;}}In the presenter you can do this.var name = mView.tbName.Text;ormView.tbName.Text = "Larry";You can do any control that way with results being no code in the codebehind dealing with controls.You have a control wires up and event on the codebehind, then pass theobject and event args to the _presenter.EventMethod(object sender, eventargs)Friday, October 7, 2011 7:52 PM
-
Hello Holger K,
Thanks for your reply. Actually I'm very new to EF and our team is about to start a Web-based enterprise application aswell. I'm bit confused whether should we go with EF or write our own DAL. Have u found any difficulties implementing EF in your project? any limitations compared to traditional ADO.net data access? Any major advantage of using EF over traditional data access?
Regards,
TK Lee
Saturday, October 8, 2011 2:59 AM -
Hi TK,
no i didnt find any disadvantages. The only "disadvantage" was the missing migration support for code first. But thats in work..
People say "performance" is a kind of disadvantage. But like darnold suggested aswell, i use sql profiler to see what sql is generated by EF and i optimize the sql execution tree's by creating indexes for the most important queries. Thats why I inherit a entity repository from the generic repository to implement each dbset query in one single repository. This is a nice design, because you see which queries you may have to optimize and you dont need to search for them somewhere in a DAL.
feel free to ask here about any concerns or questions..
greetings
Holger
Saturday, October 8, 2011 7:56 AM -
On 10/7/2011 10:59 PM, tkl33 wrote:> Hello Holger K,>> Thanks for your reply. Actually I'm very new to EF and our team is about> to start a Web-based enterprise application aswell. I'm bit confused> whether should we go with EF or write our own DAL. Have u found any> difficulties implementing EF in your project? any limitations compared> to traditional ADO.net data access? Any major advantage of using EF over> traditional data access?Your other option is to use nHibernate a mature and robust ORM solution,as opposed to EF which is still in its infancy stag.Your DAL would use either nHibernate or EF with methods in the DALworking with the ORM solution for CRUD operations on the model. Also onedoesn't expose the entities from nHibernate or EF at the UI of a Webapplication.An ORM solution doesn't replace the DAL. The DAL works with the ORMsolution just as if you were using ADO.NET and SQL command objects withT-SQL and sprocs for CRUD operations in the DAL.Each one of the Dofactory for E-commerce solutions show how to use theDAL with Link-2-SQL or EF. As a matter of fact, each one of theE-commerce solution are using the same back-end of BLL and DAL which isinterchangeable between the E-commerce solutions.EF is using this to execute T-SQL with all the advantages that using asproc has like caching, compiled sprocs and the whole nine yards.The major advantage of using an ORM solution like EF over the straightADO.NET with SQL Command objects is this.The above is what an ORM like EF brings to the table when developingn-tier solutions.The only disadvantage of using any ORM solution is the developer'sinability to use the ORM effectively.Saturday, October 8, 2011 10:13 PM
-
On 10/7/2011 10:59 PM, tkl33 wrote:> Hello Holger K,>> Thanks for your reply. Actually I'm very new to EF and our team is about> to start a Web-based enterprise application aswell. I'm bit confused> whether should we go with EF or write our own DAL. Have u found any> difficulties implementing EF in your project? any limitations compared> to traditional ADO.net data access? Any major advantage of using EF over> traditional data access?Your other option is to use nHibernate a mature and robust ORM solution,as opposed to EF which is still in its infancy stag.Your DAL would use either nHibernate or EF with methods in the DALworking with the ORM solution for CRUD operations on the model. Also onedoesn't expose the entities from nHibernate or EF at the UI of a Webapplication.An ORM solution doesn't replace the DAL. The DAL works with the ORMsolution just as if you were using ADO.NET and SQL command objects withT-SQL and sprocs for CRUD operations in the DAL.Each one of the Dofactory for E-commerce solutions show how to use theDAL with Link-2-SQL or EF. As a matter of fact, each one of theE-commerce solution are using the same back-end of BLL and DAL which isinterchangeable between the E-commerce solutions.EF is using this to execute T-SQL with all the advantages that using asproc has like caching, compiled sprocs and the whole nine yards.The major advantage of using an ORM solution like EF over the straightADO.NET with SQL Command objects is this.The above is what an ORM like EF brings to the table when developingn-tier solutions.The only disadvantage of using any ORM solution is the developer'sinability to use the ORM effectively.Thanks very much guys, it really helped. I'm still trying to convince my team lead to go with EF instead of manually coding own DAL from scratch. He has a concern about versioning and migration of database. Lets say if we manually create a trigger on database or if we write a LINQ query and let EF generate SQL query. Now if we optimize that SQL query for performance. When we would deploy our product for some other client, we are going to loose our changes no? because EF would regenerate it's own query. In that case we would have to maintain our own sql scripts for triggers, hand written optimized sql queries etc.. no? or is there any automated way in EF to sync those things?Tuesday, October 11, 2011 5:08 PM
-
Hi,
Code First Migration is here: http://blogs.msdn.com/b/adonet/archive/2011/09/21/code-first-migrations-alpha-3-released.aspx
Have nice day.
Alan Chen[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Saturday, October 15, 2011 7:55 AM