PageRenderTime 18ms CodeModel.GetById 12ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Domain/AggregatedRootRepository.cs

#
C# | 81 lines | 48 code | 6 blank | 27 comment | 3 complexity | e6ed237e0a466daba021f2aa9e0157bc MD5 | raw file
 1#region License
 2//
 3// Copyright (c) 2008-2012, DoLittle Studios and Komplett ASA
 4//
 5// Licensed under the Microsoft Permissive License (Ms-PL), Version 1.1 (the "License")
 6// With one exception :
 7//   Commercial libraries that is based partly or fully on Bifrost and is sold commercially, 
 8//   must obtain a commercial license.
 9//
10// You may not use this file except in compliance with the License.
11// You may obtain a copy of the license at 
12//
13//   http://bifrost.codeplex.com/license
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21#endregion
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using Bifrost.Commands;
26using Bifrost.Events;
27
28namespace Bifrost.Domain
29{
30	/// <summary>
31	/// Defines a concrete implementation of <see cref="IAggregatedRootRepository{T}">IAggregatedRootRepository</see>
32	/// </summary>
33	/// <typeparam name="T">Type the repository is for</typeparam>
34	public class AggregatedRootRepository<T> : IAggregatedRootRepository<T>
35		where T : AggregatedRoot
36	{
37		readonly ICommandContextManager _commandContextManager;
38
39		/// <summary>
40		/// Initializes a new instance of <see cref="AggregatedRootRepository{T}">AggregatedRootRepository</see>
41		/// </summary>
42		/// <param name="commandContextManager"> <see cref="ICommandContextManager"/> to use for tracking </param>
43		public AggregatedRootRepository(ICommandContextManager commandContextManager)
44		{
45			_commandContextManager = commandContextManager;
46		}
47
48#pragma warning disable 1591 // Xml Comments
49		public T Get(Guid id)
50		{
51			var commandContext = _commandContextManager.GetCurrent();
52			var type = typeof (T);
53			var aggregatedRoot = Activator.CreateInstance(type, id) as T;
54			if (null != aggregatedRoot)
55			{
56                if(!aggregatedRoot.IsStateless())
57                {
58                    foreach (var stream in commandContext.EventStores.Select(eventStore => eventStore.Load(type, id)).Where(stream => stream.HasEvents))
59                    {
60                        aggregatedRoot.ReApply(stream);
61                    }  
62                }
63                else
64                {
65                    var versions = commandContext.EventStores.Select(eventStore => eventStore.GetLastCommittedVersion(type, id)).ToList();
66
67                    aggregatedRoot.FastForward(versions.Max());
68                }
69			}
70            commandContext.RegisterForTracking(aggregatedRoot);
71
72			return aggregatedRoot;
73		}
74
75        object IAggregatedRootRepository.Get(Guid id)
76        {
77            return Get(id);
78        }
79#pragma warning restore 1591 // Xml Comments
80	}
81}