I have an application in Domain Driven Design that links user profiles from different social networks. For example, let’s say it’s Facebook and Twitter. APIs return a User objects. These User objects are required for the custom RegisteredUser, that is, in some methods, both Twitter.User and Facebook.User are used at once.
I use the Repository-Service pattern and don’t use EF. It was decided to create a TwitterUser and FacebookUser in Domain. Also, TwitterUserDto and FacebookUserDto were created to communicate with the database, because DTOs have a slightly different structure for correct work and maintaining the database structure.
For example:
public class TwitterUser
{
public long Id { get; set; }
public string FirstName { get; set; }
public string Username { get; set; }
}
public class TwitterUserDto
{
public required long Id { get; set; }
public required string FirstName { get; set; }
public required string Username { get; set; }
public required DateTime AddedAt { get; set; }
}
The problem is that the library methods need their own User (Facebook.User, Twitter.User), it is necessary to convert objects. Services use objects defined in Domain (TwitterUser, FacebookUser), and Repositories use DTOs (TwitterUserDto, FacebookUserDto). As a result, even simple code turns into a mess.
Is it necessary to use DTOs at all? How to properly organize the models in the project and the interaction of the various layers of the application?
2
Answers
Firstly, you can try to use Automapper to separate mapping in another class.
Secondly, Jimmy Bogard said "Put the classes close to where they’re actually used.". And it complies with this great answer about DDD:
Using DTOs is really important in the DDD method. Just map Facebook and Twitter accounts to their DTO classes:
Then inject the mapper and use it in your service inside your domain project.
Let me know if it was helpful