Reputation: 23
This is as much a design as a technical question. I'm not sure I am doing this right...
I have a WCF API which communicates with a DB and passes back a Person object (which is defined in a separate .dll) This has both methods and attributes. The object is being sent from the WCF to the calling client.
I want to call the Person's methods on the client. I understand that these cannot be sent downstream from the API. However, if I reference the same .dll that the WCF uses should I be able to cast the API Person to a .dll Person then run the methods?
I hope it is clear what I am trying to achieve.
Upvotes: 2
Views: 8621
Reputation: 872
WCF works through data contracts. These are models of the data to return, like a Person
object with its properties. Don't worry about methods during transportation of the object from Service to Client. If a data contract is used, and you will reference WCF, the proxy class will generate a Person object.
If your logic is more complex I suppose it depends on the situation you have. Let me describe in a few words:
If you can't change the source of the class and you want to call a public method, it is better to use reflection. So, you receive an object from WCF, set the Person
object's properties with the values returned, and then call the method.
If you can change sources of the class, you can create a base interface IPerson
, implement this interface with properties of the Person object in the class and return an IPerson
object. In this case, you will be able to perform the cast.
More details: Ok, let me provide you with more details:
As a best practice, I recommend creating separate classes with interfaces. This should be a data contract interface which will describe your object. Something like this:
[DataContract]
public interface IPerson
{
[DataMember]
public int Identifier { get; set; }
[DataMember]
public string First { get; set; }
[DataMember]
public string Last { get; set; }
public string GetSomething();
}
Method on WCF you are implementing should return IPerson type.
IPerson
for your Person
object.IPerson
, so you can use your Person object from the shared library, and use all its methods.Upvotes: 2
Reputation: 61579
WCF supports the ability to re-use references that are already included in the project. In this sense, you can create a contracts assembly (an assembly that contains your thin domain models (e.g. Person
etc) which you can add your own logic to.
You can then add assembly to both your WCF service, and the calling client projects, and instruct WCF to re-use any existing references. This way, what is pulled back from your service is deserialised into local copy of a Person
, but not a Person
that would generated as a proxy, you actually get a full instance, on which you can perform method calls.
Don't forget through, you are marshalling by value in this case. Any changes you make to the Person
instance are local to the client only, you would need to pass it back upstream to your WCF service again (through serialisation) for the service to recognise any changes and act accordingly.
Upvotes: 1
Reputation: 166
Since you are referencing the same dll, and WCF can be strong typed, you should be able to call the methods on the Person response object without casting. Make sure when you define the Person class that you are using the DataContract attribute.
In the following example, the Person class will be serialized by WCF with the three data members on the server side. The client side WCF will deserialize response... creating the class Person. So on the client side, you can call FullName() and it will work without casting.
[DataContract]
public class Person
{
[DataMember]
public int Identifier { get; set; }
[DataMember]
public string First { get; set; }
[DataMember]
public string Last { get; set; }
public string FullName()
{
return First + " " + Last;
}
}
Upvotes: 1