Ask a questionAsk a question
 

Answerdifferent view of request object

  • Tuesday, November 03, 2009 1:38 PMUSCHunter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is it possible to have a request object and vary the requirements of the contract based on the context of the request?  for example, I have a data contract that defines what members are required.  however, the required members vary based on the method requested.  Let's say there is a securityrequest that has username, password, emailaddress, address, city, state, zip.  And when adding a user all may be required but when retrieving details we only require a username.  I am checking the values as they are passed in but one of our clients brought up a good point, if there required or not I should be letting them know on the interface.

    Thanks

Answers

  • Tuesday, November 03, 2009 2:46 PMAllan Rees Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    How about having a few data contracts, as the context in which you are using the entity has changed, thus so has the contract, create a new one.

    Data contracts can also be composed and inherited, so it is possible to build new contracts out of smaller ones.
    e.g.

    [DataContract]
    User

    [DataContract]
    SecurityContext

    nothing stops you from creating a say a SecureUser that is an aggregate for User and SecurityContext.

    [DataContract]
    SecureUser
        User
        SecurityContext

    Your use cases will dictate the contents and granularity.
  • Tuesday, November 03, 2009 4:18 PMHaripraghash Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    I am not sure if i am stating the obvious or if i dont understand your question right, but you can just pass only the only the required data you require for that particular operation. And in that operation read only that data that iusrequired.
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
  • Tuesday, November 03, 2009 9:02 PMPrometheusMS Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Then you must create different DataContracts for each operation.
    I don't now for possibility to change data contracts for look different for different operation.

All Replies

  • Tuesday, November 03, 2009 2:46 PMAllan Rees Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    How about having a few data contracts, as the context in which you are using the entity has changed, thus so has the contract, create a new one.

    Data contracts can also be composed and inherited, so it is possible to build new contracts out of smaller ones.
    e.g.

    [DataContract]
    User

    [DataContract]
    SecurityContext

    nothing stops you from creating a say a SecureUser that is an aggregate for User and SecurityContext.

    [DataContract]
    SecureUser
        User
        SecurityContext

    Your use cases will dictate the contents and granularity.
  • Tuesday, November 03, 2009 3:15 PMHaripraghash Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Datacontract inheritance and using [KnownType] is definitely an option. When you use datacontract inheritance,the type of the object you return can be controlled as long as you dont break the hierarchy.Also, when you define a datacontract and expose it in an operation,you wont be able to add or remove its members in runtime.
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
  • Tuesday, November 03, 2009 4:05 PMUSCHunter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thanks but we currently use an inheritance hierarchy the problem is we can't create a data contract for every scenario. here's an example:

    [DataContract]
    SecurityRequest
                   Username
                   password
                   emailaddress
                   foo
                   bar

    [

    OperationContract]
    Foo(SecurityRequest request)
    // this operation requires username but could use xyz

    [

    OperationContract]
    Bar(SecurityRequest request)
    // this operation requires username and password and nothing else

    [

    OperationContract]
    Baz(SecurityRequest request)
    // this operation uses the entire request but not username

    So you can see that all 3 operations use different parts but each have different data that is required and optional.



    The problem is they are either required or optional and cannot be changed - that I can see

  • Tuesday, November 03, 2009 4:18 PMHaripraghash Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Hi

    I am not sure if i am stating the obvious or if i dont understand your question right, but you can just pass only the only the required data you require for that particular operation. And in that operation read only that data that iusrequired.
    Please mark the response as answers if it solves your question or vote as helpful if you find it helpful. http://thoughtorientedarchitecture.blogspot.com/
  • Tuesday, November 03, 2009 5:36 PMUSCHunter Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    That is what we currently do.  however a few of our clients do not like that everything is always optional whether they are actually required or not.  For example, what is required on one operation may not be required on another.  So, the data on the data contract has to become optional. 

    So, we have an operation that looks like this(matching the same contract as above)

    // this guy requires that Username must be supplied however it is optional on the data contract becauase elsewhere that contract is used doesn't require it
    Foo(SecurityRequest request)
    {
          if(string.IsNullOrEmpty(request.UserName)
         {
                    throw;
         }
    }

    Our clients say wait a minute, your contract says this is optional....even though our documentatation states the is is required for this operation...but the client wants to have it as required for this particular operation.

    Hope that makes more sense
  • Tuesday, November 03, 2009 9:02 PMPrometheusMS Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Then you must create different DataContracts for each operation.
    I don't now for possibility to change data contracts for look different for different operation.