using AutoMapper using AutomatedLibrarySyst em Data Entities using Aut

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using AutoMapper;
using AutomatedLibrarySystem.Data.Entities;
using AutomatedLibrarySystem.Data.EntityFramework.Interfaces;
using AutomatedLibrarySystem.Data.Infrastructure.Exceptions;
using AutomatedLibrarySystem.Domain.DTO;
using AutomatedLibrarySystem.Domain.Infrastructure.Exceptions;
using AutomatedLibrarySystem.Domain.Infrastructure.Mapping;
using System;
namespace AutomatedLibrarySystem.Domain.Services {
public class BookService : IDisposable {
private bool isDisposed = false;
private IMapper mapper;
private ILibraryUnitOfWork unitOfWork;
public BookService(ILibraryUnitOfWork unitOfWork) {
this.unitOfWork = unitOfWork;
mapper = AutoMapperDomainConfiguration.Configure().CreateMapper();
}
#region Registration
public void RegisterBook(BookDTO book) {
var transBook = mapper.Map<BookDTO, Book>(book);
unitOfWork.Books.Create(transBook);
unitOfWork.BookExtraData.Create(new BookExtraData(transBook, 0, 0));
unitOfWork.Commit();
}
public void RegisterBook(BookDTO book, BookExtraDataDTO extraData) {
unitOfWork.Books.Create(mapper.Map<BookDTO, Book>(book));
try {
var transExtraData = mapper.Map<BookExtraDataDTO, BookExtraData>(extraData);
unitOfWork.BookExtraData.Create(transExtraData);
} catch (AmountOutOfRangeException e) {
throw new AmountOutOfRangeException(e.Message, e);
} finally {
unitOfWork.Commit();
}
}
public void UpdateBook(BookDTO book) {
try {
unitOfWork.Books.Update(mapper.Map<BookDTO, Book>(book));
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
}
}
public void DeleteBook(BookDTO book) {
try {
var transBook = mapper.Map<BookDTO, Book>(book);
unitOfWork.Books.Delete(transBook);
unitOfWork.BookExtraData.DeleteSingleBy(x =>
x.Book.Id == transBook.Id
);
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
}
}
#endregion
public void Dispose() {
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (!isDisposed && disposing) {
unitOfWork.Dispose();
isDisposed = true;
}
}
}
}