locked
How to implement IOrganizationService RRS feed

  • Question

  • Are there any proper guidelines / examples that illustrate implementing the IOrganizationService interface?  I am looking for a mock implementation for testability of business logic. In particular, how would 'RetrieveMultiple(QueryBase)'  be implemented for passing in QueryExpressions?

    -Abhijeet

    Wednesday, May 1, 2013 4:54 AM

All replies

  • Many of the samples in the CRM SDK use the IOrganizationService interface, including several that pass a QueryExpression to the RetrieveMultiple method.

    Maybe it would be best for you to post a specific example of code that you'd like assistance with


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    • Proposed as answer by Minal Dahiya Wednesday, May 1, 2013 8:07 AM
    • Marked as answer by HIMBAPModerator Wednesday, May 1, 2013 10:15 AM
    • Unmarked as answer by Abhijeet P Thursday, May 2, 2013 3:55 AM
    Wednesday, May 1, 2013 7:46 AM
    Moderator
  • Hi Abhijeet,

        Here is an example from SDK.

    // Connect to the Organization service. 
                    // The using statement assures that the service proxy will be properly disposed.
                    using (_serviceProxy = ServerConnection.GetOrganizationProxy(serverConfig))
                    {
                        // This statement is required to enable early-bound type support.
                        _serviceProxy.EnableProxyTypes();
                        _service = (IOrganizationService)_serviceProxy;
    
                        // Call the method to create any data that this sample requires.
                        CreateRequiredRecords();
    
                        // *****************************************************************************************************************
                        //                FetchXML      estimatedvalue_avg    Aggregate 1
                        // *****************************************************************************************************************
                        // Fetch the average of estimatedvalue for all opportunities.  This is the equivalent of 
                        // SELECT AVG(estimatedvalue) AS estimatedvalue_avg ... in SQL.
                        System.Console.WriteLine("===============================");
                        string estimatedvalue_avg = @" 
                        <fetch distinct='false' mapping='logical' aggregate='true'> 
                            <entity name='opportunity'> 
                               <attribute name='estimatedvalue' alias='estimatedvalue_avg' aggregate='avg' /> 
                            </entity> 
                        </fetch>";
    
                        EntityCollection estimatedvalue_avg_result = _serviceProxy.RetrieveMultiple(new FetchExpression(estimatedvalue_avg));
    
                        foreach (var c in estimatedvalue_avg_result.Entities)
                        {
                            decimal aggregate1 = ((Money)((AliasedValue)c["estimatedvalue_avg"]).Value).Value;
                            System.Console.WriteLine("Average estimated value: " + aggregate1);
    
                        }
    

    Hope this helps.
     
    -----------------------------------------------------------------------
     Minal Dahiya
     
    If this post answers your question, please click "Mark As Answer" on the post and "Vote as Helpful"
    Wednesday, May 1, 2013 8:06 AM
  • Many of the samples in the CRM SDK use the IOrganizationService interface, including several that pass a QueryExpression to the RetrieveMultiple method.

    Maybe it would be best for you to post a specific example of code that you'd like assistance with


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk


    Maybe I wasn't specific enough, the question is not about how to use IOrganizationService( I am well aware of this) ,but rather how to create a mock implementation of this interface so that the calls don't actually go over the wire to CRM but are serviced by an in memory implementation, this is mainly for testability of code that uses IOrganizationService

    -Abhijeet


    • Edited by Abhijeet P Thursday, May 2, 2013 4:00 AM edits
    Thursday, May 2, 2013 3:58 AM
  • Hi Minal,

    I am looking for a mock implementation of this interface. I am well versed with the SDK samples.

    Thanks.

    Abhijeet


    -Abhijeet


    • Edited by Abhijeet P Thursday, May 2, 2013 4:08 AM edits
    Thursday, May 2, 2013 3:59 AM
  • I expect you could build your own class that implements IOrganizationService, and has code that generates dummy output data, but I would have thought it would be a lot easier to use an actual CRM instance, and this would be a better test as you'd only get a successful response if the QueryExpression were valid for the schema in the CRM organisation


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Thursday, May 2, 2013 3:46 PM
    Moderator
  • I expect you could build your own class that implements IOrganizationService, and has code that generates dummy output data, but I would have thought it would be a lot easier to use an actual CRM instance, and this would be a better test as you'd only get a successful response if the QueryExpression were valid for the schema in the CRM organisation


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk


    That is precisely the question, are there any examples/guidance from MSFT for building a custom implementation for IOrganizationService for testability. In particular how would one implement a RetrieveMultiple(QueryBase). The reason why you wouldn't want to actually couple to a physical CRM instance is for the same reasons why you would not want to couple your business logic to a database/data access code (separation of concerns) and to get accurate performance stats of your business logic code. If you are coupled to an actual CRM instance network latency can skew performance test numbers.

    -Abhijeet

    Friday, May 3, 2013 5:50 AM
  • I'm pretty sure there would be no example from MSFT for building a custom implementation for IOrganizationService.

    The overall design behind CRM (both as an application, and whn developing extensions) does not lend itself to separating business logic from the data access.

    Re performance testing, I personally don't see a major benefit of testing the performance of the business logic without including the impact of the connection to CRM; the performance that the end customer will care about will include the connection to CRM. If you do want to compensate for the connection, it shouldn't be hard to determine the time just before, and just after, the call to IOrganizationService, and subtract it from your overall numbers - this would probably take a lot less effort than building your own implementation of IOrganizationService


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Friday, May 3, 2013 9:06 AM
    Moderator