r/programming • • Apr 28 '17

Entity Framework 6 Extensions You Might Be Unaware Of

http://codingsight.com/entity-framework-6-extensions-you-might-be-unaware-of/
3 Upvotes

34 comments sorted by

View all comments

Show parent comments

1

u/Eirenarch Apr 29 '17

I can see how you can argue that views and denormalized objects will improve the architecture although I am skeptical. However I still don't see how that can lead to less code.

1

u/[deleted] Apr 29 '17

[removed] — view removed comment

2

u/Eirenarch Apr 29 '17

With EF you can skip the roundtrip if you really want to. I agree that updates are more tedious but how about this

DbContext.Categories.Include(c => c.Products).Include(c => c.Tags).SingleOrDefault(c => c.CategoryID == id);

The joins and materialization of the related entities will be quite annoying with manual sql and as far as I know with micro ORMs you do the mapping manually.

Also how about paging? A lot of my queries end up with something like .ToPagedResult() where this method does 2 queries one for Count and one with Skip().Take() on the query that was already defined. It seems like the paging would need to be added manually to every query with a micro ORM.

1

u/[deleted] Apr 29 '17

[removed] — view removed comment

1

u/Eirenarch Apr 29 '17

I assume you are talking about the bad effects of clauses like OFFSET and implementing paging strategies which somehow mark the last value seen (like a date) and using this to page. In my experience this is not needed in 95% of paging scenarios (indexing the order by is sufficient) and is non-trivial amount of work which even requires changing the way the UI is built. While this should be done when there are excessive amounts of data it is rarely needed and a good candidate for stored procedure anyway.

1

u/[deleted] Apr 29 '17

[removed] — view removed comment

1

u/Eirenarch Apr 29 '17

EF tends to do an union if it sees more than two joins.

1

u/Deif Apr 29 '17

Why is auditing difficult in EF? You can just intercept the upsert and add the audit information for all entities that you've marked as needing auditing.

I've never seen steps 4 and 5 be tedious either. AutoMapper was made for that reason.

1

u/[deleted] Apr 29 '17

[removed] — view removed comment

1

u/Deif Apr 29 '17

You can automatically inject the HttpContext/wherever you hold current user data into the DbContext, have your entity inherit an AuditedEntity class that implements IAuditedEntity that has your LastModifiedBy, etc columns.

Then you can override your SaveChanges method in the DbContext, check for entities in the change tracker that implement your IAuditedEntity interface, add your data to those fields and commit.

You're trading performance for ease of use and time basically.

1

u/[deleted] Apr 29 '17

[removed] — view removed comment

1

u/Deif Apr 29 '17

Well I say just pick whatever suits your needs best. With any of the MicroORMs you suggested you surely have to implement your own migration code to handle schema changes?