Service Composition
Quick rundown on n/3 tier architectures
What is a Service then?
Service then?public class UserService : IUserService
{
// Assume under the hood its a UserData generic
public IUserRepository UserRepository {get;}
// Lets assume this has some helper methods for sending an email
public IEmailProvider EmailProvider {get;}
// ASSUME CONSTRUCTOR INJECTING ABOVE IN
public async Task<User> GetUser(Guid id)
{
var user = await UserRepository.Get(id);
if(user == null) { throw new UserNotFoundException(id); }
// Lets pretend we have some auto mapper style extensions which convert between data/service layer models
return user.TransformTo<User>();
}
public async Task<User> RegisterUser(UserDetails userDetails)
{
// Eject if the email address exists
var isEmailAddressTaken = await UserRepository.Query<HasUserWithEmail>(userDetails.Email);
if(isEmailAddressTaken) { throw new EmailTakenException(userDetails.Email); } // Notice its not a HTTP specific exception or anything
// Create the user in the DB getting us our unique ID
var userData = await UserRepository.Create(UserDetails.Email, UserDetails.Name);
// Send the user an email containing their name, and the id for a clickable link to their user page in the content
EmailProvider.SendNewUserEmail(userData.Email, userData.Name, userData.Id);
// Transform and return the user data into a user service layer model
return userData.TransformTo<User>();
}
// Other methods for deleting users, updating them etc
}Why not share models between layers?
Why not just return HTTP/Consumer specific exceptions from service layer?
So why was that Service example bad?
Service example bad?Pretending to test the service
WILL YOU GET TO THE POINT AND TELL US WHAT YOU WANT US TO KNOW?!?
Some other info worth thinking about
Last updated