DDD object creation design - wcf

I'm developing a server side application based on DDD.
My application service (wcf layer) has a method which received an XML from the client.
This XML needs to be processed and finally transformed into an object.
In such a case, where's the best place to put the data transformation logic?
Inside the domain model?
Example:
void OnRequestArrived(string xml)
{
ItemRequest request = ItemRequest.New(xml);
}
or in a separate domain service?
void OnRequestArrived(string xml)
{
ItemRequest request = _mappingService.Map(xml);
}
The ItemRequest object is then the main domain model for the business flow..
Thanks

This responsibility belongs to the WCF service. In DDD terms, this is the anti-corruption layer - it maps external models to the domain model at hand. The domain model should be protected from external services as much as possible.
Sometimes it can be convenient to break this single WCF service into two - an application service and a WCF-specific adapter which would adapt the application service to the WCF framework. This is based on the Hexagonal architecture. The application service knows nothing about WCF or serialization formats. The WCF adapter service handles this and other serialization concerns. This can be useful for two reasons. First it keeps your application service clean and focused on its responsibility - the orchestration of domain objects, repositories and other services to implement domain use cases. Second, it allows the same application service to be called from different adapters.

Related

MVC Controllers WCF Service

I am using ASP.net MVC3 for my presentation layer and my data access and business logic are exposed through a WCF Service. Should my controllers call the WCF service or should there be a further level of abstraction such as a repository which calls the WCF service.
Repository which calls the service
public ProductController(IProductRepository productRepository)
{
_productRepository = productRepository;
}
public ProductRepository(ProductServiceClient client)
{
_client = client;
}
Service directly in the controllers
public ProductController(ProductServiceClient client)
{
_client = client;
}
The repository classes do nothing apart from call the methods exposed via the service.
Sorry I am well confused about your question, but I am sorry if I have misunderstood. Hope my pointers will clear this up.
Repositories related to persistence and define a way to handle the
infrastructure layer i.e. dealing with data (in memory repository,
sql repository or generic one)
Services if you like use these repositories to perform the
contractual operations such as getting the client in your case.
Services are called by clients or someone that requests services and
service in turn calls the repository which in turn calls the data
operations.
So you may need to change your wcf to work with repositories and let your controller call services..hope that helps
I would start by doing the exact oposite - the WCF should call methods inside the repository.
The data layer should be universal, and should be able to be accessed through any means (wcf should be one, mvc website should be another, etc).
That way you can also unit test your projects, and it's easier to keep track of it. Wcf should be considered as an extra api to your program in this case.
I am more concerned of where the business rules should be stored, but I would vote for mvc controllers for business logic, and wcf services invoking those internally.

WCF Service and Business Logic

I am unsure where to place my business logic. I have a WCF service which exposes its methods to my client.
Should my business logic go in the service method
public User GetUser(int id)
{
//Retrieve the user from a repository and perform business logic
return user;
}
or should it be in a separate class where each WCF service method will in turn call the business layer methods.
public User GetUser(int id)
{
return _userLogic.GetUser(id);
}
My personal preference is to have WCF as a very thin layer on top of a separate business layer. The WCF layer does nothing more than make calls to the business layer, similar to what you have shown in option 2. This gives you some flexibility in the event that you want to have your business layer consumed by something other than WCF clients (for example, a WPF application calling your business layer directly rather than via WCF).
WCF services are already, by default, designed for reuse. I see no reason not to have some logic in your services, though keep in mind things like the Single Responsibility Principle so you don't end up with a service that does a dozen things.
Even then, if you end up parceling out your functionality into smaller classes, it's not a bad idea at all to host those classes as WCF services. You can then use them in-proc (via pipes) when needed or across machine boundaries (tcp) or even as web services. Create facades as needed to provide access to the functionality of your other, smaller services.
There's no real need to avoid putting any logic in WCF service classes.
I think that the decision depends on your business needs. WCF is a mechanism to transport data (objects) between server and client. If you like your businsess logic runs on server, you should let WCF exposes the object after running your business logic.
It should go in a separate set of classes. Your WCF layer should only contain logic that directly pertains to how the product of the service is delivered.
In your case, I see that you have a WCF method that returns a User (I assume this is a custom class) why have a separate method to return the UserID instead of populating that property as part of returning the User object?
For reuse/testability/maintenance/readability you should always put you BL in a separate layer.

WCF SOA naming conventions

I have an class library called ServiceLayer which acts as a repository for a ASP.NET MVC application This service layer has a references to a WCF Service called ProfileService which contains Profile methods to perform CRUD operations on a database etc.
I now need to allow mobile devices to communicate with my application so I have created another WCF Service called ProfileService. This service has a reference to the ServiceLayer class library and makes calls to it to undertake Profile operations.
Now this is quite confusing as I now have 2 ProfileServices. The first communicating with my database etc and exposing itself to my service layer. The second communicating with my service layer and exposing itself to mobile devices.
What is the best way to name your services in a SOA environment to avoid confusion of which type is which? especially when mapping between types.
I may also want to create another service which acts as an API to users of the system. What would I name this service ProfileAPI?? I know each ProfileService is in its own namespace but this doesnt help with readability when creating AutoMapperSettings or performing manual mapping.
So if anybody out there knows of a good way to name services in this environment it would be much appreciated.
You are looking for a Service Facade
You would end up with a Facade, which is just a specialized interface into your real service. You would define the different services as needed (mobile, users, database)

WCF Service with existing Service/Repository Layer and EF 4.1 Code First

I have an existing set of Services and Repositories I use in an MVC application that leverage the Entity Framework 4.1 Code First.
I want to create a couple of WCF Services that use the existing architecture, but it seems to have a hard time serializing the object graphs.
I realize that there are some circular references to deal with, but I really don't want to litter the Domain Objects with WCF attributes, so should I just create View Models like my MVC app uses? And if so, should I create the View Models to be able to be used in both?
Any other ideas? - Thanks!!
I prefer keeping my domain model and the WCF data contract separate by defining Data Transfer Object classes as the data contact of the WCF server. They are tailored specifically to carry the right data across the wire. A good DTO design will keep the number of WCF service call roundtrips from the client down. It will also separate your internal domain model from the contract with the client.

Can I use a rich domain model with WCF?

Is it possible to use DDD and a rich domain model if your application is like:
windows client (WPF)
windows service
And communication happens with WCF?
I'm used to have DTO's with only data state, and have business rules inside the Service layer, but everyone keeps telling me that I should have a rich domain model where data state and rules/methods are all in the objects themselves.
I'm just not sure if this rich domain model would apply to a system that has a UI and communicates via WCF to a service (like I presented above). In my case is it better to continue using an anemic domain model because of WCF? If not, could you please give an example on how to architecture it using a rich domain model, considering WCF, proxy, etc?
Thanks!
Generally speaking you serialize your domain objects for transmission across WCF as some simplified DTO anyway, and it's these that are consumed by your client application.
You can serialize user defined types and deserialize them in the client but for most applications this is unnecessary. As long as you don't need the 'rich' behaviour of your objects in your client (which you shouldn't with a good DDD anyway), it sounds to me like you are fine to use a rich design in your service layer and send simple DTOs across the wire.