I’m getting started with domain driven design in asp.net mvc with c# and have a small sample project I am creating. I currently have a project for my domain layer, service layer, web, and also a console application. I am also using mongodb as my database.
Currently my project has groups with people belonging to those groups which I have a class called Group and Person in my domain layer something like below (simplified). Group and Person also represent what the collection Group looks like in my mongodb database.
public class Group
{
public string Name { get; set; }
public List<Person> People { get; set; }
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
The way I understand it is my website and my console application should be communicating to my services layer which acts as a sort of facade and should never see the domain layer?
The thing I am unsure of is in my console application or website if I wanted to ask the service layer for all the groups normally I would expect that I would be asking for something like
var groupService = new GroupService();
List<Group> groups = groupService.GetGroups();
Except in this instance as my group, and person classes are in the domain layer which my console app and website have no idea about I can’t ask for a list of Group as my application has no idea what a Group is.
Should my service layer be outputting the result as something else such as Json where it is up to my website and console app or say if I had an api that others could access to work out what to do with this data? or have I got something mixed up here?
EDIT:
In the future I plan to use my service and domain in an android app. I am thinking of using a restful service. I don’t plan (at this stage) to have this as a public API.
Your service layer will be communicating with data transfer objects, not domain objects. The difference between a domain entity and a data transfer object is that domain entities have behavior and data transfer objects (DTO) are just data.
So, to answer your question: Your service layer will take commands and queries from your console application or your website using data transfer objects and translate them into domain entities. It will get results from the domain entities and translate the results back into DTOs.
It might be very possible that a single domain entity doesn’t translate into a DTO at all, or that a single domain entity translates into 10 DTOs. These are 2 very different apis used for 2 very different purposes. My suggestion is to develop your domain layer and then build your service layer on top to enable communication based on the needs of the clients (console and web app).