Linq-to-entity: how to create objects (new Xyz () vs CreateXyz ())?

What is the best way to add a new object to an entity infrastructure. The designer adds all these creation methods, but it makes sense to me to name a new object. Generated CreateCustomer method, for example. could be called like this:

Customer c = context.CreateCustomer(System.Guid.NewGuid(), "Name"));
context.AddToCustomer(c);

where it would be more appropriate for me:

Customer c = new Customer {
    Id = System.Guid.NewGuid(),
    Name = "Name"
};
context.AddToCustomer(c);

The latter is much more explicit, since the properties that are set during construction are named. I assume that the designer specifically creates creation methods. Why should I use them?

+3
source share
3 answers

(), . , "", , , , . , , - , , SaveChanges. Entity Framework ; , . "", .

+5

, . , .

+2

I guess this has a lot to do with it. For me, it looks like a factory method, so a single extension point is allowed. Secondly, all this in your constructor is not the best practice, especially when you do a lot of things during initialization. Yes, your question seems reasonable, I even agree with it, however, from the point of view of the design of the object, it is more practical, like it.

Sincerely, Marius S. ( c_marius@msn.com )

0
source

Source: https://habr.com/ru/post/1702888/


All Articles