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
80
81
82
83
84
85
86
87
88
89
90
91
92
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 RegistrationService : IDisposable {
private bool isDisposed = false;
private IMapper mapper;
private ILibraryUnitOfWork unitOfWork;
public RegistrationService(ILibraryUnitOfWork unitOfWork) {
this.unitOfWork = unitOfWork;
mapper = AutoMapperDomainConfiguration.Configure().CreateMapper();
}
#region EmployeeCard
public void RegisterEmployeeCard(EmployeeCardDTO employeeCard) {
unitOfWork.EmployeeCards.Create(
mapper.Map<EmployeeCardDTO, EmployeeCard>(employeeCard));
unitOfWork.Commit();
}
public void UpdateEmployeeCard(EmployeeCardDTO employeeCard) {
try {
unitOfWork.EmployeeCards.Update(
mapper.Map<EmployeeCardDTO, EmployeeCard>(employeeCard));
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new EmployeeCardNotFoundException(e.Message, e);
}
}
public void DeleteEmployeeCard(EmployeeCardDTO employeeCard) {
try {
unitOfWork.EmployeeCards.Delete(
mapper.Map<EmployeeCardDTO, EmployeeCard>(employeeCard));
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new EmployeeCardNotFoundException(e.Message, e);
}
}
#endregion
#region UserCard
public void RegisterUserCard(UserCardDTO userCard) {
unitOfWork.UserCards.Create(mapper.Map<UserCardDTO, UserCard>(userCard));
unitOfWork.Commit();
}
public void UpdateUserCard(UserCardDTO userCard) {
try {
unitOfWork.UserCards.Update(mapper.Map<UserCardDTO, UserCard>(userCard));
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new UserCardNotFoundException(e.Message, e);
}
}
public void DeleteUserCard(UserCardDTO userCard) {
try {
unitOfWork.UserCards.Delete(
mapper.Map<UserCardDTO, UserCard>(userCard));
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new UserCardNotFoundException(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;
}
}
}
}