Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing domain model with WCF service

Is it good practice to reference my web applications domain layer class library to WCF service application.

Doing that gives me easy access to the already existing classes on my domain model so that I will not need to re-define similar classes to be used by the WCF service

On the other hand, I don't like the coupling that it creates between the application and service and i am curious if it could create difficulties for me on the long run.

I also think having dedicated classes for my WCF app would be more efficient since those classes will only contain the members that will be used by the service and nothing else. If I use the classes from my domain layer there will be many fields in the classes that will not be used by the service and it will cause unnecessary data transfer.

I will appreciate if you can give me your thoughts from your experience

like image 650
kaivalya Avatar asked Sep 23 '09 21:09

kaivalya


People also ask

How do I create a WCF service application?

Create the WCF service application Right-click on the solution you created in the procedure above and click Add new project. In the New Projectscreen, select WCF, and then select WCF Service Application in the right pane as shown in Figure 2.

How do I publish a WCF service to a website?

Perform the following steps to publish the service. Publish the service to the web application Right-click the WCF service application project and then click on Publish. Select File Systemas the publish method. In the target location, specify the path to the directory which you created while creating the web application, as shown in Figure 4.

Are cross-domain authenticated WCF calls possible with default Web HTTP binding?

The impetus for this article is that cross-domain authenticated WCF calls are not possible with the default web HTTP binding. This article details the steps required to create a working end-to-end solution by using JQuery. Note You can learn more about SharePoint sandboxed solutions here: Sandboxed solutions resource center. Problem definition

How to create a WCF service application with JSONP binding?

After you have your JSONP binding project set up, you are ready to create your WCF service application. Create the WCF service application Right-click on the solution you created in the procedure above and click Add new project. In the New Projectscreen, select WCF, and then select WCF Service Application in the right pane as shown in Figure 2.


3 Answers

No it's not. Entities are all about behaviour. data contract is all about... data. Plus as you mentioned you wouldn't want to couple them together, because it will cripple you ability to react to change very soon.

like image 184
Krzysztof Kozmic Avatar answered Oct 13 '22 08:10

Krzysztof Kozmic


For those still coming across this post, like I....

Checkout this site. Its a good explanation on the topic.

Conclusion: Go through the effort of keeping the boundaries of your architecture clear and clean. You will get some credit for it some day ;)

like image 23
Mevius Avatar answered Oct 13 '22 08:10

Mevius


I personally frown on directly passing domain objects directly through WCF. As Krzysztof said, it's about a data contract not a contract about the behavior of the the thing you are passing over the wire.

I typically do this:

  • Define the data contracts in their own assembly
  • The service has a reference to both the data contracts assembly and the business entity assemblies.
  • Create extension methods in the service namespace that map the entities to their corresponding data contracts and vice versa.

Putting the conceptual purity of what a "Data Contract" is aside, If you begin to pass entities around you are setting up your shared entity to pulled in different design directions by each side of the WCF boundary. Inevitably you'll end up with behaviors that only belong to one side, or even worse - have to expose methods that conceptually do the same thing but in a different way for each side of the WCF boundary. It can potentially get very messy over the long term.

like image 32
Daniel Auger Avatar answered Oct 13 '22 10:10

Daniel Auger