Sunday, May 15, 2016

What is the difference between service.Create and orgContext.AddObject?

http://stackoverflow.com/questions/10528216/what-is-the-difference-between-service-create-and-orgcontext-addobject?rq=1


I find that there are two ways at least to create a record in an entity like the following.

Common Part
var record = new someEntity()
{
    attribute1="test1",
    attribute2="test2" 
};

var service = new OrganizationService("CrmConnectionString");
Part A
service.Create(record);
Part B
var orgContext = new OrganizationServiceContext(service);
orgContext.AddObject(record);
orgContext.SaveChanges();
What is the difference?And which is better?


8down voteaccepted
Part A uses the raw create method of the organization service proxy. This operation directly creates the record.
Part B makes use of the OrganizationServiceContext, which implements the Unit of Work pattern. Your operations are not transmitted to the server until you call SaveChanges()
Which is better? It depends on your requirements. If you only want to create a record on the go -> use the service. If you do multiple things which form a logical unit take version B.

No comments:

Post a Comment