开发者

Lifetime of an OrganizationServiceContext - CRM Dynamics 2011

This is more of a non-tech question.

We intend to use OrganizationServiceContext with Linq as opposed to calling OrganizationServiceProxy.

My question is: what shou开发者_开发问答ld the lifetime of the context be? Should it instantiated once per method or can you keep it around for the life of the web application using a singleton approach?

What would the pros/cons be? Any advice?

Thanks in advance


You should never keep a datacontext around for the life of a web application. The application lifecycle is managed outside of your code.

There is also a world of pain around saving changes when other users are saving at the same time. Datacontexts should always be managed only for the life of the request and running save changes should never save bits and pieces from other people's request as they are processing.

If you want to reduce reads, then use caching. If you want to manage concurrency use transactions with a unit of work.


Just to expand a little on Gats' answer, which is entirely correct, we create new context objects pretty much for each separate method we have. Even for Silverlight, where we know we're running for one user at a time, managing what is in the context at any time is just too painful just to avoid creating a new context object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜