I build an asp.net core web api with clean architecture, my layers are:
- Web API
- Service layer
- Domain layer
- Data layer
In my Web API I'm loading data from an external Exchange Webservices. i would like to provide in my own Web API for my clients.
For this I implemented a repository at my data layer which gets the data from the external webservice as objects from Class Appointment
.
At the service layer I mapped the objects to my own Class EventDTO
with AutoMapper.
Now I don't know what's the best practice to get access to this data from the web api controller.
In my opinion, when following clean architecture principles, I have to map the EventDTO
to EventEntity
. But when I done this, I have two 100% identically Objects, because there is no logic on the EventEntity
and I do the same mapping two times. That does not make sense?!?
But when I pass the EventDTO
direct to the controller, it will break the principle of clean architecture, or not?
EventController.cs
namespace Example.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EventController : ControllerBase
{
private readonly IEventService eventService;
public EventController (IEventService eventService)
{
this.eventService = eventService ?? throw new ArgumentNullException(nameof(eventService));
}
[HttpGet]
public async Task<IActionResult> LoadEventsAsync()
{
IEnumerable<EventDTO> items = await eventService.GetEvents();
return Ok(items);
}
}
}
EventService.cs
namespace Example.Core.Application.Services
{
public class EventService: IEventService
{
private IEventRepository eventRepository;
private IMapper mapper;
public EventService(IEventRepository eventRepository, IMapper mapper)
{
this.eventRepository= eventRepository;
this.mapper = mapper;
}
public async Task<IEnumerable<EventDTO>> GetEvents()
{
var appointments = await eventRepository.GetAppointments();
return mapper.Map<IEnumerable<EventDTO>>(appointments);
}
}
}
EventRepository.cs
namespace Example.Infrastructure.Repository
{
public class EventRepository : IEventRepository
{
private ExchangeService _exchangeService;
public EventRepository()
{
_exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
_exchangeService.Credentials = new WebCredentials("user", "passwort", "domain");
_exchangeService.Url = new Uri("https://example.de/Exchange.asmx");
}
async Task<IEnumerable<Appointment>> IEventRepository.GetAppointments()
{
CalendarFolder calendar = await CalendarFolder.Bind(
_exchangeService,
new FolderId
(
WellKnownFolderName.Calendar,
"user@example.de"
)
);
return await calendar.FindAppointments(
new CalendarView()
);
}
}
}
EventDTO.cs
namespace Example.Core.Application.DTO
{
public class EventDTO
{
public string Subject { get; set; }
public bool Cancelled { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
}
EventEntity.cs
namespace Example.Core.Entities
{
public class EventEntity
{
public string Subject { get; set; }
public bool Cancelled { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
}
Maybe someone has implemented a similar project. And can give me an approach.