I'm working with visual studio. The solution contains the 4 following projetcs :
DAL
Works with Entity framework + Repository pattern. It contains de DB classes + Repositorys.
BLL
It contains the DTO classes + Services.
Services map the DTO classes with the DB classes. It reference the DAL project.
IMessageRepository messageRepo;
public MessageService()
{
this.messageRepo = new MessageRepository();
AutoMapper.Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperProfile>());
}
public MessageService(IMessageRepository repo)
{
this.messageRepo = repo;
AutoMapper.Mapper.Initialize(cfg => cfg.AddProfile<AutoMapperProfile>());
}
public List<DTO.Message> GetAll()
{
List<DTO.Message> messages = ((System.Linq.IQueryable<DAL.Message>)messageRepo.GetAll()).ProjectTo<DTO.Message>().ToList();
return messages;
}
Presentation
It references the BLL project and EntityFramework.SqlServer.dll.
MessageService ms = new MessageService();
List<Message> messages = ms.GetAll();
Bonus : Unit tests
It references the BLL project and the DAL project (It mocks the repositorys)
[TestClass]
public class MessageServiceTest
{
Mock<IMessageRepository> messageRepo;
MessageService messageService;
[TestInitialize]
public void SetUp()
{
// Create a new mock of the repository
messageRepo = new Mock<IMessageRepository>();
// Set up the mock for the repository
messageRepo.Setup(x => x.GetAll()).Returns((new List<DAL.Message>
{
new DAL.Message { Id = 1, Content = "Hello world!", Date = DateTime.Now },
new DAL.Message { Id = 2, Content = "I <3 U!", Date = DateTime.Now }
}).AsQueryable());
// Create the service and inject the repository into the service
messageService = new MessageService(messageRepo.Object);
}
[TestMethod]
public void TestGetAll()
{
// Act
var messages = messageService.GetAll();
// Assert
Assert.AreEqual(2, messages.Count, "The messages count is not correct");
}
}
I'm new to software architecturing. What are your thoughts on this architecture? Any improvements?