Sanjay Sharma

Sanjay Sharma

  • 1.3k
  • 413
  • 27.3k

How to avoid Multiple calls to private common function

Feb 7 2018 11:17 PM
I have class that has many public method which perfrom differnet service operations.
Inorder to do service operation it need to create service Instance.
 
How do I avoid calling private method from each function?
code snippet.
public class EmployeeModel
{
    private IOrganizationService _service = CRMConnect.CrmService();
     
   private void CreateCrmServiceInstance()
   {
      
         if (_service == null){
            _service = CRMConnect.CrmService();
      }
   }
   
public List<EmployeeModel> RetrieveAllContacts()
{
    CreateCrmServiceInstance();
    // do something using  _service
 
public List<ContactModel> UpdateContactStatus()
{
CreateCrmServiceInstance();
// do something using _service
}
 
 
 

Answers (2)