Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 399
Nigiri Avatar asked Jan 16 '23 17:01

Nigiri


1 Answers

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.

like image 90
ccellar Avatar answered Apr 06 '23 01:04

ccellar