I'm somewhat new to the onion architecture. I created a service layer that calls repositories in the DAL by manually passing them into the constructor. But now looking at my method I'm using an object from the core domain and I'm wondering if I should have passed in an interface to access it as well? Should all objects be accessed via an interface being passed in via parameter, constructor, property, etc?? I know this would make it less coupled, but in terms of best practices where does this stand?
Here in my code I'm wondering if
DiaryEvent diaryEvent = new DiaryEvent();
should be used as an interface instead of a concrete object. P.S. DiaryEvent is coming from the layer (core) below.
public class DiaryEventService : IDiaryEventService
{
private readonly IYogaSpaceEventRepository yogaSpaceEventRepository;
public DiaryEventService() : this(new YogaSpaceEventRepository())
{
}
public DiaryEventService(IYogaSpaceEventRepository yogaSpaceEventRepository)
{
this.yogaSpaceEventRepository = yogaSpaceEventRepository;
}
public List<DiaryEvent> LoadAllAppointmentsInDateRange(double start, double end)
{
var fromDate = ConvertFromUnixTimestamp(start);
var toDate = ConvertFromUnixTimestamp(end);
var yogaSpaceEvents = yogaSpaceEventRepository.Find(fromDate, toDate);
List<DiaryEvent> result = new List<DiaryEvent>();
foreach (var item in yogaSpaceEvents)
{
DiaryEvent diaryEvent = new DiaryEvent();
diaryEvent.ID = item.YogaSpaceEventId;
//diaryEvent.SomeImportantKeyID = item.SomeImportantKey;
diaryEvent.StartDateString = item.DateTimeScheduled.ToString("s");
// "s" is a preset format that outputs as: "2009-02-27T12:12:22"
diaryEvent.EndDateString = item.DateTimeScheduled.AddMinutes(item.AppointmentLength).ToString("s");
// field AppointmentLength is in minutes
diaryEvent.Title = item.Title + " - " + item.AppointmentLength.ToString() + " mins";
diaryEvent.StatusString = Enums.GetName<AppointmentStatus>((AppointmentStatus)item.StatusEnum);
diaryEvent.StatusColor = Enums.GetEnumDescription<AppointmentStatus>(diaryEvent.StatusString);
string ColorCode = diaryEvent.StatusColor.Substring(0, diaryEvent.StatusColor.IndexOf(":"));
diaryEvent.ClassName = diaryEvent.StatusColor.Substring(diaryEvent.StatusColor.IndexOf(":") + 1,
diaryEvent.StatusColor.Length - ColorCode.Length - 1);
diaryEvent.StatusColor = ColorCode;
result.Add(diaryEvent);
}
return result;
}
private static DateTime ConvertFromUnixTimestamp(double timestamp)
{
var origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
return origin.AddSeconds(timestamp);
}
}