Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Entity Framework Use Reflection and Hurt Performance?

I ultimately have two areas with a few questions each about Entity Framework, but let me give a little background so you know what context I am asking for this information in.

At my place of work my team is planning a complete re-write of our application structure so we can adhere to more modern standards. This re-write includes a completely new data layer project. In this project most of the team wants to use Entity Framework. I too would like to use it because I am very familiar with it from my using it in personal projects. However, one team member is opposed to this vehemently, stating that Entity Framework uses reflection and kills performance. His other argument is that EF uses generated SQL that is far less efficient than stored procedures. I'm not so familiar with the inner-workings of EF and my searches haven't turned up anything extremely useful.

Here are my questions. I've tried to make them as specific as possible. If you need some clarification please ask.

Issue 1 Questions - Reflection

  1. Is this true about EF using reflection and hurting performance?
  2. Where does EF use reflection if it does?
  3. Are there any resources that compare performance? Something that I could use to objectively compare technologies for data access in .NET and then present it to my team?

Issue 2 Questions - SQL

  1. What are the implications of this?
  2. Is it possible to use stored procedures to populate EF entities?
  3. Again are there some resources that compare the generated queries with stored procedures, and what the implications of using stored procedures to populate entities (if you can) would be?

I did some searching on my own but didn't come up with too much about EF under the hood.

like image 528
Chev Avatar asked Mar 22 '11 18:03

Chev


2 Answers

Yes, it does like many other ORMs (NHibernate) and useful frameworks (DI tools). For example WPF cannot work without Reflection.

While the performance implications of using Reflection has not changed much over the course of the last 10 years since .NET 1.0 (although there has been improvements), with the faster hardware and general trend towards readability, it is becoming less of a concern now.

Remember that main performance hit is at the time of reflecting aka binding which is reading the type metadata into xxxInfo (such as MethodInfo) and this happens at the application startup.

Calling reflected method is definitely slower but is not considered much of an issue.


UPDATE

I have used Reflector to look at the source code of EF and I can confirm it heavily uses Reflection.

like image 63
Aliostad Avatar answered Sep 28 '22 11:09

Aliostad


Answer for Issue 1:

You can take a look at exactly what is output by EF by examining the Foo.Designer.cs file that is generated. You will see that the resulting container does not use reflection, but does make heavy use of generics.

Here are the places that Entity Framework certainly does use reflection:

  1. The Expression<T> interface is used in creating the SQL statements. The extension methods in System.Linq are based around the idea of Expression Trees which use the types in System.Reflection to represent function calls and types, etc.
  2. When you use a stored procedure like this: db.ExecuteStoreQuery<TEntity>("GetWorkOrderList @p0, @p1", ...), Entity Framework has to populate the entity, and at very least has to check that the TEntity type provided is tracked.

Answer for Issue 2:

It is true that the queries are often strange-looking but that does not indicate that it is less efficient. You would be hard pressed to come up with a query whose acutal query plan is worse.

On top of that, you certainly can use Stored Procedures, or even Inline SQL with entity framework, for querying and for Creating, Updating and Deleting.


Aside:

Even if it used reflection everywhere, and didn't let you use stored procedures, why would that be a reason not to use it? I think that you need to have your coworker prove it.

like image 33
John Gietzen Avatar answered Sep 28 '22 10:09

John Gietzen