0

So I am sending entities over WCF. What I would like to do is to have an opportunity to alter the entities in any way I want before they go over. I do not have to do this in the WCF but I don't see another place to do it, as I want one single place to do it.

Open to suggestions though if anyone has another way to do it :)

I have the usual layers of DAL, Domain and Service. They all share a Common too.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Shumii
  • 4,529
  • 5
  • 32
  • 41

3 Answers3

2

I think you are talking about Message Inspectors

A message inspector is an extensibility object that can be used in the service model's client runtime and dispatch runtime programmatically or through configuration and that can inspect and alter messages after they are received or before they are sent.

This sample implements a basic client and service message validation mechanism that validates incoming messages against a set of configurable XML Schema documents. Note that this sample does not validate messages for each operation. This is an intentional simplification.

Also take a look into IParameterInspector, here you can handle all input/output parameters in client and server side

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • yes this does look good... but I am clueless about how to get the objects I am sending over. Would I have to look at the message contents and deserialize? I did look at behaviours too but got stumped with the same question. – Shumii Nov 30 '12 at 12:06
  • 1
    yes, you would have to deserialize messages inside, what you want to do with this objects? – Arsen Mkrtchyan Nov 30 '12 at 12:08
  • I will want to turn it back into an object so I can use reflection to loop though the properties to look at properties with a specific attribute. Now if it has said attribute I would append some extra information. The entities extend from a base class which has the property for inserting the extra information. – Shumii Nov 30 '12 at 12:11
  • 1
    Also try to take a look into How to: Validate Input Parameters, here http://msdn.microsoft.com/en-us/library/ff650173.aspx. I believe you can get parameters and just convert to types you want – Arsen Mkrtchyan Nov 30 '12 at 12:23
  • Many thanks for your help... will take a look in a little while and update this post. Thanks again – Shumii Nov 30 '12 at 12:37
  • I am going to mark this answer as answered as it addressed my question perfectly although I have not chosen to implement it. Reason why is because of the additional overhead and then the dealbreaker was the fact that my extra customizations would only be available to clients of the service layer and not 'behind' the service layer itself. I am now looking to implement this somehow in Entity Framework, so the customizations are attached when data is fetched and entity created. – Shumii Dec 03 '12 at 10:08
2

I am not sure if I understood correctly what you are asking, but I'll give it a try.

I think that you might be looking for Data Transfer Objects (DTO). You can put all the needed data of your entities into a DTO before sending it to the service. The service then does the processing of the contained data and sends another DTO back to your client.

This way, you have full control over how the DTO is structured and what kind of data your service and client really need.

Jens H
  • 4,590
  • 2
  • 25
  • 35
  • You are kind of accurate. But I want it to happen automatically for any entity. – Shumii Nov 30 '12 at 12:02
  • @shumii then make your DTOs implement an interface and loop through the results and invoke the interface before returning them – cordialgerm Nov 30 '12 at 12:40
  • @pickles can you be more specific? Where would you do the loop and what exactly do you mean by "invoking" the interface? Thanks though for comment - I'm just not sure you understand what i am trying to achieve. – Shumii Nov 30 '12 at 13:43
1

Expanding on Jen H's answer:

Instead of inspecting messages and dealing with the overhead of de-serializing the
already serialized results you could just perform your customization logic after you retrieve the results from your database and before you return them to the client. One way of doing that is shown below

public interface IDataTransferObject
{
        void CustomizeMeSomehow();
}

[DataContract]
public class MyDataTransferObject : IDataTransferObject
{
    public void CustomizeMeSomehow()
    {
          //Your custom logic here..
    }
}

public class MyService
{
      public List<MyDataTransferObject> GetObjects()
      {
          List<MyDataTransferObjects> items = Repository.RetrieveResults();

          foreach (var item in items)
               item.CustomizeMeSomehow();

          return items;
      }  
}
cordialgerm
  • 8,403
  • 5
  • 31
  • 47
  • I see that this would work for a particular service returning a particular object. But what about any service returning any object? – Shumii Nov 30 '12 at 15:16
  • @Shumii -- you'll have to provide more information about what you're trying to do, but you should be able to put this type of code into utility classes – cordialgerm Nov 30 '12 at 15:54
  • essentially I want my developers to get the add ons without writing any extra code. I don't want them to do anything consciously to get the add ons (add ons being my customizations). They would code as normal and get what they needed from a service call and automagically they will get extra fields. Read the comments above in my discussion with ArsenMkrt. I am still looking into that solution but the deserialization, serialization and reflection overhead is a concern. – Shumii Nov 30 '12 at 17:34