Line data Source code
1 : #region Copyright
2 : // -----------------------------------------------------------------------
3 : // <copyright company="Chinchilla Software Limited">
4 : // Copyright Chinchilla Software Limited. All rights reserved.
5 : // </copyright>
6 : // -----------------------------------------------------------------------
7 : #endregion
8 :
9 : using Cqrs.Domain;
10 :
11 : namespace Cqrs.Services
12 : {
13 : /// <summary>
14 : /// Provides a basic container to control how the <see cref="IUnitOfWork{TAuthenticationToken}"/> is accessed.
15 : /// </summary>
16 : public class UnitOfWorkService<TAuthenticationToken> : IUnitOfWorkService
17 1 : {
18 : /// <summary>
19 : /// Gets or set the <see cref="IUnitOfWork{TAuthenticationToken}"/>.
20 : /// </summary>
21 : protected IUnitOfWork<TAuthenticationToken> UnitOfWork { get; private set; }
22 :
23 : /// <summary>
24 : /// Gets or set the object that wants to control the <see cref="UnitOfWork"/>.
25 : /// </summary>
26 : protected object Committer { get; private set; }
27 :
28 : /// <summary>
29 : /// Instantiate a new instance of <see cref="UnitOfWorkService{TAuthenticationToken}"/>.
30 : /// </summary>
31 : /// <param name="unitOfWork"></param>
32 1 : public UnitOfWorkService(IUnitOfWork<TAuthenticationToken> unitOfWork)
33 : {
34 : UnitOfWork = unitOfWork;
35 : }
36 :
37 : /// <summary>
38 : /// Informs the service of the object that will be committing the <see cref="UnitOfWork"/>.
39 : /// </summary>
40 : /// <returns>
41 : /// true if the provided <paramref name="commiter"/> is accepted as the committer, false otherwise.
42 : /// </returns>
43 1 : public bool SetCommitter(object commiter)
44 : {
45 : if (Committer != null)
46 : return false;
47 :
48 : Committer = commiter;
49 : return true;
50 : }
51 :
52 : /// <summary>
53 : /// Commits the <see cref="UnitOfWork"/> if the provided <paramref name="commiter"/> is the <see cref="Committer"/>.
54 : /// </summary>
55 : /// <returns>
56 : /// true if the provided <paramref name="commiter"/> is the <see cref="Committer"/>, false otherwise.
57 : /// </returns>
58 1 : public bool Commit(object commiter)
59 : {
60 : if (Committer != commiter)
61 : return false;
62 :
63 : UnitOfWork.Commit();
64 : return true;
65 : }
66 : }
67 : }
|