2. Adapter Layer
The Business layer mentioned before contains a list of interfaces to connect to other external dependencies (external services, database storage). It doesn’t care what database system to use or what protocol the external services need. All those logic will be implemented in this Adapter layer.
An implementation may look like this
public class GetContactsByMarketingCampaignId : IGetContactsByMarketingCampaignId
{
private readonly IMapper _mapper;
public GetContactsByMarketingCampaignId(IMapper mapper)
{
_mapper = mapper;
}
public IList<Business.Contact> Execute(int marketingCampaignId)
{
// get from Redis cache and then fallback to SQL
var contacts = GetFromRedis(marketingCampaignId) ?? GetFromSql(marketingCampaignId);
// use AutoMapper to map back to Business model
return mapper.Map<IList<Business.Contact>>(contacts);
}
private IList<SqlModels.Contact> GetFromRedis(int marketingCampaignId)
{
// logic to get from redis here
...
}
private IList<SqlModels.Contact> GetFromSql(int marketingCampaignId)
{
// logic to get from sql here
...
}
}