Refactor BL foorum service to use a factory for DTOS

This commit is contained in:
Arti Zirk 2017-06-12 14:28:07 +03:00
parent 4aadaf08fe
commit 475f0d539c
3 changed files with 55 additions and 29 deletions

View File

@ -42,6 +42,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="DTOs\FoorumDTO.cs" /> <Compile Include="DTOs\FoorumDTO.cs" />
<Compile Include="DTOs\PostDTO.cs" /> <Compile Include="DTOs\PostDTO.cs" />
<Compile Include="Factories\FoorumFactory.cs" />
<Compile Include="Interfaces\IFoorumService.cs" /> <Compile Include="Interfaces\IFoorumService.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\FoorumService.cs" /> <Compile Include="Services\FoorumService.cs" />

View File

@ -0,0 +1,46 @@

using System;
using System.Collections.Generic;
using BL.DTOs;
using DAL.Domain;
namespace BL.Factories
{
public class FoorumFactory
{
public FoorumDTO Create(Foorum f, bool withPosts = false)
{
var dto = new FoorumDTO()
{
FoorumId = f.FoorumId,
Title = f.Title,
Author = f.Author,
Body = f.Body,
CreationTime = f.CreationTime,
Description = f.Description,
Visible = f.Visible
};
if (withPosts)
{
throw new NotImplementedException();
//dto.Posts = new List<PostDTO>();
//f.Posts.ForEach(p => {dto.Posts.Add();});
}
return dto;
}
public Foorum Create(FoorumDTO f)
{
return new Foorum()
{
Author = f.Author,
Body = f.Body,
CreationTime = f.CreationTime,
Description = f.Description,
Title = f.Title
};
}
}
}

View File

@ -5,6 +5,7 @@ using System.Security.Cryptography;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using BL.DTOs; using BL.DTOs;
using BL.Factories;
using BL.Interfaces; using BL.Interfaces;
using DAL.Domain; using DAL.Domain;
using DAL.Interfaces; using DAL.Interfaces;
@ -13,32 +14,19 @@ namespace BL.Services
{ {
public class FoorumService : IFoorumService public class FoorumService : IFoorumService
{ {
private IFoorumRepository _foorumRepository; private readonly IFoorumRepository _foorumRepository;
private readonly FoorumFactory _factory;
public FoorumService(IFoorumRepository foorumRepository) public FoorumService(IFoorumRepository foorumRepository)
{ {
_foorumRepository = foorumRepository; _foorumRepository = foorumRepository;
_factory = new FoorumFactory();
} }
public FoorumDTO Add(FoorumDTO foorum) public FoorumDTO Add(FoorumDTO foorum)
{ {
var foorumDomain = new Foorum() var foorumDomain = _factory.Create(foorum);
{ foorumDomain.CreationTime = DateTime.Now;
Author = foorum.Author,
Body = foorum.Body,
CreationTime = DateTime.Now,
Description = foorum.Description,
Title = foorum.Title
};
var f = _foorumRepository.Add(foorumDomain); var f = _foorumRepository.Add(foorumDomain);
return new FoorumDTO() return _factory.Create(f);
{
FoorumId = f.FoorumId,
Title = f.Title,
Author = f.Author,
Body = f.Body,
CreationTime = f.CreationTime,
Description = f.Description,
Visible = f.Visible
};
} }
public void AddPost(int id, PostDTO post) public void AddPost(int id, PostDTO post)
@ -60,16 +48,7 @@ namespace BL.Services
{ {
foreach (var f in _foorumRepository.GetAll()) foreach (var f in _foorumRepository.GetAll())
{ {
yield return new FoorumDTO() yield return _factory.Create(f);
{
FoorumId = f.FoorumId,
Title = f.Title,
Author = f.Author,
Body = f.Body,
CreationTime = f.CreationTime,
Description = f.Description,
Visible = f.Visible
};
} }
} }