views:

37

answers:

1

The code that gets generated by Entity Framework 4--mainly the AddTo and Create methods--should I be using those directly? I'm trying to understand the Create method. The CreateDinner method (in NerdDinner) for example is this:

public static Dinner CreateDinner(global::System.Int32 dinnerID, global::System.String title, global::System.DateTime eventDate, global::System.String description, global::System.String hostedBy, global::System.String contactPhone, global::System.String address, global::System.String country, global::System.Double latitude, global::System.Double longitude)
    {
        Dinner dinner = new Dinner();
        dinner.DinnerID = dinnerID;
        dinner.Title = title;
        dinner.EventDate = eventDate;
        dinner.Description = description;
        dinner.HostedBy = hostedBy;
        dinner.ContactPhone = contactPhone;
        dinner.Address = address;
        dinner.Country = country;
        dinner.Latitude = latitude;
        dinner.Longitude = longitude;
        return dinner;
    }

There is no SubmitChanges() and when I view references, this method isn't being called anywhere. What's the purpose of this method?

+1  A: 

Entity Framework default code generation template creates a Factory Method for each entity object in your Model. This static method lets you quickly create a new entity and the parameter list for it consists of all of the non-nullable properties in your class (and not all of them).
Therefore, it's not meant to Save or Submit anything to the DB.

Morteza Manavi
I guess I'm trying to understand how the Factory pattern fits in with this Entity Framework code. Could you help me understand why it's quicker to create my entity using this factory method rather than just using the entity's constructor: Entity e = new Entity()
Prabhu
Since like I said its parameter list consists of only the non-nullable properties in your class so that you can quickly come up with the minimum requirement for this object to be saved in the database. Of course you can directly use the object initializer syntax or class constructor but then you might forget to initialize all the non-nullable fields and as a result your Save will fails at runtime. Other than that, it's not faster or quicker than constructor by any means since as you can see even this factory method is merely an object initializer implementation.
Morteza Manavi