I am having a problem using ValueInjecter to create deep EntityFramework POCO clones in similar DTO classes.
In cases where I inject several related entities / children from a complex POCO object with navigation properties into a slightly simpler DTO, the ValueInjecter seems to still touch on multiple property values ββand result in lazy loading of this data from the database.
I believe that a ValueInjecter gets the value of each property in a specific source object, as it prepares to enter values ββinto the specified target.
My actual project is quite complex, but as an example, I took the NerdDinner example and replicated the problem much easier. (NerdDinner is the first developer code example with EF4 ( ScottGu NerdDinner example ).
So, I have two model classes.
public class Dinner
{
public int DinnerId { get; set; }
public string Title { get; set; }
public DateTime EventDate { get; set; }
public string Address { get; set; }
public string HostedBy { get; set; }
public virtual ICollection<RSVP> Rsvps { get; set; }
}
and
public class RSVP
{
public int RsvpID { get; set; }
public int DinnerID { get; set; }
public string AttendeeEmail { get; set; }
public virtual Dinner Dinner { get; set; }
}
I also created a DTO class:
public class DinnerDTO
{
public int DinnerId { get; set; }
public string Title { get; set; }
public DateTime EventDate { get; set; }
public string Address { get; set; }
public string HostedBy { get; set; }
}
Note that I do not have the Rsvps collection found Dinnerin mine DinnerDTO.
Also important, I use the CloneInjection convention to deeply clone an object. This code is offered both here on SO and in many other sites as an approach for performing deep clone injection. This code can be found here: CloneInjection Code
Now, to emphasize the lazy load, I went and inserted 10,000 RSVP for Dinner with Id = 1.
Then I execute the following code:
var dinner = nerdDinners.Dinners.Where(x => x.DinnerId == 1).FirstOrDefault();
DinnerDTO dinnerDTO = new DinnerDTO();
dinnerDTO.InjectFrom<CloneInjection>(dinner);
InjectFrom , , 10 000 RSVP . CloneInjection Match, SetValue, , . , - ValueInjecter, RSVPs.
, : ( Include )
var dinner = nerdDinners.Dinners.Where(x => x.DinnerId == 1).Include("RSVPs").FirstOrDefault();
DinnerDTO dinnerDTO = new DinnerDTO();
dinnerDTO.InjectFrom<CloneInjection>(dinner);
"Eager Load" RSVP, , , , InjectFrom , .
StackOverflow, , LazyLoading datacontext. , , .
( NHIBernate POCO DTO ) , , , NHibernate , - - . EF4.
, , , Rsvps DTO, .
. , ValueInjecter , .
- , ValueInjecter? - , , , , SetValue ConventionInjection? , , , DTO .
, , ValueInjecter , - lazy load , , , null . , .
- EF, ? .
, ValueInjecter?
* *
, , , .