8.11. Deferred/Lazy Loading

Lazy loading is a coding technique that minimizes resource usage and database access by not loading an entity until it is actually accessed. In EF4, lazy loading is switched on by default. If you want to turn lazy loading off, then set the ContextOptions.LazyLoadingEnabled option to false in the context class:

ctx.ContextOptions.LazyLoadingEnabled = false;

8.11.1. Eager Loading

While we are looking at lazy loading, it's worth mentioning its evil twin, eager loading. Eager loading is the exact opposite of lazy loading, and means preloading an entity before you need it. This can increase the responsiveness of your application if the initialization is spread out over your application's lifetime. Julia Lerman has a great post that describes a number of ways to implement eager loading and measures the effect it has on performance: http://thedatafarm.com/blog/data-access/the-cost-of-eager-loading-in-entity-framework.

8.11.2. Complex Type Designer Support

If your entities contain many fields, then it can be useful to subdivide them into types. For example, a Patient entity in a medical system may have a huge amount of properties, but using complex types, we could divide this information up like so:

Patient.Demographic.FirstName
Patient.Demographic.Age
Patient.Demographic.LastName

Patient.Clinical.BloodType

Patient.Financial.InsurerName

Previously, if you wanted to accomplish this, it was necessary to manually edit the CSDL, but as of EF4 you can accomplish this in the designer. Let's see how to work with this feature with our Film entity.

  1. Select the Film entity.

  2. Hold down the Ctrl key and select the Description and Length properties (Figure 8-11).

  3. Right-click and select the Refactor into New Complex Type option on the context menu.

    Figure 8.11. Refactoring description and length into a complex type
  4. Visual Studio will create a new property called ComplexProperty; rename this property Detail.

  5. If you open Program.cs, you will now be able to access these properties using code similar to the following:

    Film Film = new Film();
    Film.Detail.Description = "New film";
    Film.Detail.Length = 200;

To undo this change, remove the Film table from the model designer and then add it in again by right-clicking and selecting Update Model from Database.


8.11.3. Complex Types from Stored Procedures

The Function Import wizard will now create complex types from stored procedures. For example, let's imagine we wanted to add a method to our Film entity to return information about some of the crew, which is retrieved using the following stored procedure (mocked up for ease of use):

CREATE PROCEDURE FilmGetCrewInfo

@filmID int

AS

SELECT
'James Cameron' as Director,
'Arnold Schwarzenegger' as LeadActor1,
'Linda Hamilton' as LeadActor2

  1. Go to the Model Browser window (the tab next to Solution Explorer).

  2. Right-click the Complex Types folder and add a new complex type called FilmCrew.

  3. Right-click the newly created complex type and add three new string scalar properties called Director, LeadActor1, and LeadActor2 (Figure 8-12).

    Figure 8.12. Creating a new complex type
  4. Open Chapter8.Model.edmx, and on the designer surface, right-click and select the Update Model from Database option.

  5. Under the Stored Procedures node, select the FilmGetCrewInfo stored procedure and click Finish.

  6. Right-click the designer surface and select Add Function Import to bring up the screen shown in Figure 8-13. (I also clicked the Get Column Information button when I completed the other information to populate the stored procedure column information section).

    Figure 8.13. Add Function Import screen
  7. Enter the function import name GetCrewInfo.

  8. Select the stored procedure name FilmGetCrewInfo.

  9. Select Complex in the Returns a Collection Of radio button options, and then FilmCrew on the drop-down (notice how you have the option to create a complex type from the results of the stored procedure).

  10. Click OK. The EF designer will have added this function to the context where it can be accessed as follows (note that you could then move this into your entity using partial classes):

    var crew = ctx.GetCrewInfo(1);

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.56