6
6
7
7
namespace Services
8
8
{
9
+ /// <summary>
10
+ /// A service that is responsible for managing portfolios and storing them to a persistent repository.
11
+ /// </summary>
9
12
public interface IPortfolioService
10
13
{
14
+ /// <summary>
15
+ /// Creates a new portfolio and stores it to a repository
16
+ /// </summary>
17
+ /// <param name="name">Name of the portfolio</param>
18
+ /// <param name="description">Description of the portfolio</param>
19
+ /// <param name="currency">Currency to be used within the portfolio</param>
20
+ /// <returns>A created instance of the `Portfolio` class</returns>
11
21
Portfolio CreatePortfolio ( string name , string description , Currency currency ) ;
22
+
23
+ /// <summary>
24
+ /// Deletes the given portfolio from the repository
25
+ /// </summary>
26
+ /// <param name="portfolio">Portfolio to be deleted from the repository</param>
27
+ /// <returns>A flag indicating whether the portfolio was deleted</returns>
12
28
bool DeletePortfolio ( Portfolio portfolio ) ;
29
+
30
+ /// <summary>
31
+ /// Updates the given portfolio in the repository. The portfolio with the same ID in the repository is replaced with the one.
32
+ /// </summary>
33
+ /// <param name="portfolio">Updated portfolio to be stored in the repository</param>
34
+ /// <returns>A flag indicating whether the portfolio was updated</returns>
13
35
bool UpdatePortfolio ( Portfolio portfolio ) ;
36
+
37
+ /// <summary>
38
+ /// Loads and returns a portfolio specified by the given ID from the repository.
39
+ /// </summary>
40
+ /// <param name="id">ID of the portfolio to be loaded from the repository</param>
41
+ /// <returns>An instance of the `Portfolio` class that was loaded from the repository</returns>
14
42
Portfolio GetPortfolio ( int id ) ;
43
+
44
+ /// <summary>
45
+ /// Returns a list of all portfolios present in the repository
46
+ /// </summary>
47
+ /// <returns>List of all portfolios present in the repository</returns>
15
48
List < Portfolio > GetPortfolios ( ) ;
16
49
}
17
50
18
51
public class PortfolioServiceImpl : IPortfolioService
19
52
{
20
- private IPortfolioRepository _portfolioRepository ;
21
- private IPortfolioEntryService _portfolioEntryService ;
53
+ // dependency on the portfolio repository
54
+ private readonly IPortfolioRepository _portfolioRepository ;
55
+
56
+ // dependency on the portfolio entry service (in order to be able to delete entries when deleting a portfolio)
57
+ private readonly IPortfolioEntryService _portfolioEntryService ;
22
58
23
- public PortfolioServiceImpl ( IPortfolioRepository portfolioRepository , IPortfolioEntryService portfolioEntryService )
59
+ public PortfolioServiceImpl ( IPortfolioRepository portfolioRepository ,
60
+ IPortfolioEntryService portfolioEntryService )
24
61
{
25
- this . _portfolioRepository = portfolioRepository ;
26
- this . _portfolioEntryService = portfolioEntryService ;
62
+ _portfolioRepository = portfolioRepository ;
63
+ _portfolioEntryService = portfolioEntryService ;
27
64
}
28
65
29
66
public Portfolio CreatePortfolio ( string name , string description , Currency currency )
30
67
{
31
- var potfolio = new Portfolio ( name , description , currency ) ;
32
- var id = _portfolioRepository . Add ( potfolio ) ;
33
- return potfolio with
68
+ // create a new ` Portfolio` class instance
69
+ var portfolio = new Portfolio ( name , description , currency ) ;
70
+ return portfolio with
34
71
{
35
- Id = id
72
+ Id = _portfolioRepository . Add ( portfolio )
36
73
} ;
37
74
}
38
75
39
76
public bool DeletePortfolio ( Portfolio portfolio )
40
77
{
78
+ // first, delete all portfolio entries that belong to the portfolio being deleted
41
79
_portfolioEntryService . DeletePortfolioEntries ( portfolio . Id ) ;
80
+
81
+ // then finally delete the given portfolio
42
82
return _portfolioRepository . Delete ( portfolio ) ;
43
83
}
44
84
45
- public bool UpdatePortfolio ( Portfolio portfolio )
46
- {
47
- return _portfolioRepository . Update ( portfolio ) ;
48
- }
85
+ public bool UpdatePortfolio ( Portfolio portfolio ) => _portfolioRepository . Update ( portfolio ) ;
49
86
50
- public Portfolio GetPortfolio ( int id )
51
- {
52
- return _portfolioRepository . Get ( id ) ;
53
- }
87
+ public Portfolio GetPortfolio ( int id ) => _portfolioRepository . Get ( id ) ;
54
88
55
- public List < Portfolio > GetPortfolios ( )
56
- {
57
- return _portfolioRepository . GetAll ( ) ;
58
- }
89
+ public List < Portfolio > GetPortfolios ( ) => _portfolioRepository . GetAll ( ) ;
90
+
59
91
}
60
92
}
0 commit comments