Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy-loading proxies package #10720

Merged
merged 1 commit into from
Jan 19, 2018
Merged

Lazy-loading proxies package #10720

merged 1 commit into from
Jan 19, 2018

Conversation

ajcvickers
Copy link
Contributor

Part of #10509, #3797.

This change adds a new package--Microsoft.EntityFrameworkCore.Proxies--that contains a lazy-loading proxy implementation making use of the EF lazy-loading infrastructure and the Castle.Core proxies package. Current plan is for this to ship on NuGet as an optional package for use with EF.

To use in a normal application, just add a call to UseLazyLoadingProxies() on the DbContext options builder.

There are also context.CreateProxy() methods for creating stand-alone proxy instances if needed.

Entity types must be public and navigation properties must be virtual. Also, the entity type and constructor must be "visible" to the castle proxies assembly, which usually means public, but could mean internal if "InternalsVisibleTo" is used. Exceptions are thrown if these requirements are not met--you don't just not get a proxy like in EF6.

Note that this is an optional package for EF that we chose to create because the infrastructure in place made it easy to do so. It does not preclude a Roslyn-based rewriting solution in the future.

@anpete
Copy link
Contributor

anpete commented Jan 18, 2018

Should this be called "CastleProxies"? Could it support different proxy impls? Or would they be separate packages?

@ajcvickers
Copy link
Contributor Author

@anpete It can and probably will support different types of proxies. Theoretically those could use something other than Castle, but that's not the current plan. I'm hesitant to put a third-party reference in the package name.

@divega Thoughts?

=> Expression.Call(
EntityMaterializerSource.TryReadValueMethod.MakeGenericMethod(ConsumedProperty.ClrType),
{
var property = ConsumedProperties.First();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Save alloc with [0]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

/// This API supports the Entity Framework Core infrastructure and is not intended to be used
/// directly from your code. This API may change or be removed in future releases.
/// </summary>
public static readonly ProxyFactory Instance = new ProxyFactory();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D.I. enabled the factory

/// </summary>
public static readonly ProxyFactory Instance = new ProxyFactory();

private readonly ProxyGenerator _generator = new ProxyGenerator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DI?

Part of #10509, #3797.

This change adds a new package--Microsoft.EntityFrameworkCore.Proxies--that contains a lazy-loading proxy implementation making use of the EF lazy-loading infrastructure and the Castle.Core proxies package. Current plan is for this to ship on NuGet as an optional package for use with EF.

To use in a normal application, just add a call to `UseLazyLoadingProxies()` on the DbContext options builder.

There are also `context.CreateProxy()` methods for creating stand-alone proxy instances if needed.

Entity types must be public and navigation properties must be virtual. Also, the entity type and constructor must be "visible" to the castle proxies assembly, which usually means public, but could mean internal if "InternalsVisibleTo" is used. Exceptions are thrown if these requirements are not met--you don't just not get a proxy like in EF6.

Note that this is an optional package for EF that we chose to create because the infrastructure in place made it easy to do so. It does not preclude a Roslyn-based rewriting solution in the future.
@ralmsdeveloper
Copy link
Contributor

ralmsdeveloper commented Jan 25, 2018

@ajcvickers ,
I have a simple question.
Does using AsNotracking really override LazyLoad?

I ask why you when using: UseLazyLoadingProxies();

and use the AsNoTracking Ex: db.Developers.AsNoTracking().ToList();

LazyLoad is simply ignored.
Can you understand my question?

@ajcvickers
Copy link
Contributor Author

@ralmsdeveloper Loading of related entities (with .Load) does not work for entities that are not being tracked: see #10042. Since lazy loading is just a way of calling .Load it also doesn't support this. It's not even clear that this should work--I believe for 2.1 we will just make sure that it throws. In the future it could be made to work, but I doubt it will make it into 2.1.

@ralmsdeveloper
Copy link
Contributor

Thanks @ajcvickers ,
I was breaking my head here, until i realized it.
I think there will be many people who need this, but great work for now!

Congratulations, this will eliminate some bad reviews!

@smitpatel smitpatel deleted the ThisKikiIsMarvelous0111 branch January 26, 2018 00:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants