Entity Framework 6 performance impact with IList - entity-framework

Is there any performance impact of IList instead ICollection (or IEnumerable) in EF entities?

It really depends on how you retrieve the data from the database context. Entity framework uses DbSet in order to expose an abstraction from linq to the database tables. This exposure comes in the form of an IQueryable. IQueryable implements the IEnumerable interface and allows for queried results to the database to be enumerated.
Enumeration forces the expression tree associated with an IQueryable object to be executed.
-MSDN: http://msdn.microsoft.com/en-us/library/vstudio/bb351562(v=vs.100).aspx
So depending on if you simply use a ToList() and grab the entire set, use a .Take(), or a .Skip(), or if you use a projection such as .Select() -- all of these factors weigh into how performant the entity framework will be when getting data from the database.
What you choose as far as an interface to represent the data you have fetched will not affect performance at all. This is because the interfaces only expose methods to use, but will not change the underlying set of data.
Basically the methods will range from a base of IEnumerable, all the way up to IList which has a larger selection available. In general, you can use IEnumerable for a set of data which needs to be iterated and used, ICollection for a set of data which is going to be iterated and modified, and IList for a set of data which is going to be iterated, modified, and sorted.

Related

When is IEnumerable preferred over IQueryable in Entity Framework?

I understand how IEnumerable and IQueryable works. I just cannot imagine a situation where IEnumerable would be needed in entity framework when working with SQL database. I wonder if I can just discard IEnumerable in EF. Can you provide any useful example that shows IEnumerable could be more useful than IQueryable?
There are three situations that come to mind.
When EF cannot convert your query into a correct SQL statement - so you need to bring the results into memory to complete the computation.
When you need to perform operations that involve operators that do not convert to SQL.
When SQL server is slower at producing the computation than an in-memory calculation. Many times I have found that pulling all the data into memory is quicker than letting SQL to do it.
Provided a data source (as IQueryable) can be queried, then yes, use IQueryable - though you shouldn't be creating IQueryable instances or implementing it yourself, that's what EF is for. You will still need to use IEnumerable as method parameters or return types if you're using EF with external data-sources or other data that isn't queryable itself, such as JOINing an EF table with non-EF data.
For example, you'd have a return type as IEnumerable<T> if the data you're returning isn't queryable because you called AsEnumerable or ToList but you don't want to reveal implementation details - but I'd then prefer IReadOnlyList<T> in that case.

Can I utilise JPA 2.1 #Converter with DB entities?

Maybe, I'm a bit wrong, however, I'm trying to refactor my code right now via making use of #Converter annotation from JPA 2.1 to out-source the attribute-to-dbdata converting from the POJO class to a separate class. I'm mainly utilising a custom transformation for storing a kind of JSON blob into a database column. I have several cases, where I need to rely on the order of child entities, i.e., I store the set of utilised child entities in a many-to-many table to keep the relationship between the items and, furthermore, the order in a JSON array that just keeps the child entity identifiers (to keep the order). Then I have a resolving mechanism that keeps both sides always up-to-date, i.e., the db-data (string) will be converted to a (ordered) list of child entities (that are also stored in the DB and available via the set of child entities (many-to-many relationship).
So right now I'm wondering, whether I can handle this with a #Converter (AttributeConverter) implementation, since I'll require the set of child entities to resolve the db-data (string) to a (ordered) list of child entities (i.e. the "convertToEntityAttribute" method implementation)? Or whether I need to rely on my (a bit cumbersome) mechanism in the POJO class to convert between both sides?
AttributeConverter is for simple types only, not collections/maps, and as such provides a mapping between a java type and a database column. Some JPA implementations may allow mapping to multiple columns (I know the JPA implementation I use does, DataNucleus JPA, and some others may also allow it), but I doubt you'll get one that allows mapping to some other table entirely.
Better to look at your entity mappings and consider creating a dummy entity for this information somehow

EF 4.1, POCO: Lazy-loading for complex type (2 types mapping the same table)

Is it possible to set up Address (complex type) being lazy loaded for Customer in this example: Entity Framework 4.1 – Component mapping ?
I needed it to optimize my SQL queries, so that sometimes I do not need complex type being loaded, sometimes I do. I do not want to deal with LINQ's Select() with DTOs.
Thanks
It is not possible. EF doesn't support lazy loading of complex types (properties of complex type cannot be null). You must either refactor your application and use address as separate table or use table splitting. In both cases your address becomes separate entity with navigation properties and you will be able to control its loading.

Returning IQueryable vs. ObjectQuery when using LINQ to Entities

I have been reading when using LINQ to entites a query is of type IQueryable before it is processed but when the query has been processed, it's no longer an IQueryable, but an ObjectQuery.
In that case, is it correct to write methods from my layer (repository layer) to return IQueryable?
Do I need to cast?
Why would I want to return an ObjectQuery?
I'm coming from a LINQ to SQL background where things were always IQueryable but EF seems to have changed this.
Any help really appreciated.
My repositories always returns IQueryable. The reason for this is that IQueryable is not dependent on EF whereas ObjectQuery is. So if I want my other layers to be persistance ignorant I don't want to introduce dependency on ObjectQuery.
ObjectQuery is implementation of IQueryable with several additional features. First feature you will quickly discover is Include function which is need for eager loading of navigation properties (related entities). Second feature is EQL - entity query language. EQL is second way how you can query your conceptual model. It has similar syntax as SQL. You can write simple query as string, pass it to ObjectQuery constructor and execute query or use it in Linq-To-Entities.

Entity to DTO conversion with JPA

I'm using DataNucleus as a JPA implementation to store my classes in my web application. I use a set of converters which all have toDTO() and fromDTO().
My issue is, that I want to avoid the whole DB being sent over the wire:
If I lazy load, the converter will try to access ALL the fields, and load then (resulting in very eager loading).
If I don't lazy load, I'll get a huge part of the DB, since user contains groups, and groups contains users, and so on.
Is there a way to explicitly load some fields and leave the others as NULL in my loaded class?
I've tried the DataNucleus docs with no luck.
Your DTOs are probably too fine-grained. i.e. dont plan to have a DTO per JPA entity. If you have to use DTOs then make them more coarse grained and construct them manually.
Recently we have had the whole "to DTO or not to DTO, that is the question" discussion AGAIN. The requirement for them (especially in the context of a JPA app) is often no longer there, but one of the arguments FOR DTOs tends to be that the view has coarser data requirements.
To only load the data you really require, you would need to use a custom select clause containing only these elements that you are about to use for your DTOs. I know how painful this is, especially when it involves joins, which is why I created Blaze-Persistence Entity Views which will take care of making the query efficient.
You define your DTO as an interface with mappings to the entity, using the attribute name as default mapping, this looks very simple and a lot like a subset of an entity, though it doesn't have to. You can use any JPQL expression as mapping for your DTO attributes.