Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing an Interface on Client and Server Sides
    primarykey
    data
    text
    <p>I have a client (Android app), and a server (WebApi REST web-service).</p> <p>The app has a service layer it calls to communicate with the web-service for reading and writing records. Return values are sent back from the web-service's controllers (ie, when a new record is written to the DB and the associated record-id is generated).</p> <p>I have many different controllers, each having multiple methods that all return a different number and type of values (they mostly return int array's, but occasionally I return an object array if there non-integer values mixed in).</p> <p>The Service layer on the app side does a few checks to validate the parameters, such a checking the argument count (array elements) and checking for pre-defined invalid/uninitialized values (like -9999).</p> <p>I wan't to formalize these parameter operations by defining an interface so that things like the count and type of the parameters used on both sides are explicitly specified in one location (so I'm not "winging it" on both sides).</p> <p>However, the dilemma I'm running into is deciding on whether to define one interface that specifies both the client and server related methods, or to define two separate interfaces, each specifying methods specific that side of communication.</p> <p>If I have a single interface, like so:</p> <pre><code>public interface IServiceParams { object[] CreateParams(int recordId, string recordNumber); bool ParseParams(object[] paramz, out int recordId, out string recordNumber); } </code></pre> <p>then since CreateParams is only used on the WS side, and ParseParams on the App side, I would have to declare empty implementations on either side</p> <p>Web Service Side:</p> <pre><code>public class RecordController : ApiController, IServiceParams { object[] IServiceParams.CreateParams(int recordId, string recordNumber) { return new[] { newRecordId, newRecordNumber }; } bool IServiceParams.ParseParams(object[] paramz, out int recordId, out string recordNumber) { throw new NotImpementedException(); } //////////////////////////////////////////////////////////////////////////////////////////// public object[] Put( Record record ) { if( !DbLayer.Update(record) ) { return null; } return CreateParams(record.RecordId, record.RecordNumber); } } </code></pre> <p>App Side:</p> <pre><code>public class RecordService : IServiceParams { object[] IServiceParams.CreateParams(int recordId, string recordNumber) { throw new NotImpementedException(); } bool IServiceParams.ParseParams(object[] paramz, out string recordNumber) { if( paramz == null || paramz.Count != 4 ) { return false; } if(!ParseInt(paramz[0].ToString(), out recordId) || recordIdNew == DEFAULT_RECORD_ID) { return false; } // ... return true; } //////////////////////////////////////////////////////////////////////////////////////////// public bool UpdateRecord( Record record ) { object[] paramz = WebService.Put( record ); int recordId; string recordNumber; if(!ParseParams(paramz, out recordId, out recordNumber)) { return false; } record.RecordId = recordId; record.RecordNumber = recordNumber; return true; } } </code></pre> <hr> <p>Or, should I define two interfaces, and implement each on their respective sides, thus eliminating the need to have empty implementations:</p> <pre><code>public interface IServiceParamsClient { bool ParseParams(object[] paramz, out int recordId, out string recordNumber ); } public interface IServiceParamsServer { object[] CreateParams(int recordId, string recordNumber); } </code></pre> <p>Thanks a lot for the help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload