PageRenderTime 27ms CodeModel.GetById 10ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Source/Bifrost/Sagas/SagaLibrarian.cs

#
C# | 111 lines | 72 code | 12 blank | 27 comment | 8 complexity | 171e098e2a9bf55be772cf2cc20eaf37 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
 22
 23using System;
 24using System.Collections.Generic;
 25using System.Linq;
 26using Bifrost.Entities;
 27
 28namespace Bifrost.Sagas
 29{
 30    /// <summary>
 31    /// Represents a <see cref="ISagaLibrarian"/>
 32    /// </summary>
 33	public class SagaLibrarian : ISagaLibrarian
 34	{
 35		readonly IEntityContext<SagaHolder> _entityContext;
 36		readonly ISagaConverter _sagaConverter;
 37
 38        /// <summary>
 39        /// Initializes a new instance of <see cref="SagaLibrarian"/>
 40        /// </summary>
 41        /// <param name="entityContext">A <see cref="IEntityContext{SagaHolder}"/> to use for working with persisting and resuming <see cref="ISaga">Sagas</see></param>
 42        /// <param name="sagaConverter">A <see cref="ISagaConverter"/> for converting a <see cref="ISaga"/> to a <see cref="SagaHolder"/> and back</param>
 43		public SagaLibrarian(IEntityContext<SagaHolder> entityContext, ISagaConverter sagaConverter)
 44		{
 45			_entityContext = entityContext;
 46			_sagaConverter = sagaConverter;
 47		}
 48
 49#pragma warning disable 1591 // Xml Comments
 50        public void Close(ISaga saga)
 51		{
 52			var sagaHolder = _sagaConverter.ToSagaHolder(saga);
 53			_entityContext.Delete(sagaHolder);
 54			_entityContext.Commit();
 55		}
 56
 57		public void Catalogue(ISaga saga)
 58		{
 59			var sagaHolder = GetExistingIfAny(saga.Id);
 60			if (sagaHolder == null)
 61			{
 62				sagaHolder = _sagaConverter.ToSagaHolder(saga);
 63				_entityContext.Insert(sagaHolder);
 64			}
 65			else
 66			{
 67				_sagaConverter.Populate(sagaHolder, saga);
 68				_entityContext.Update(sagaHolder);
 69			}
 70			_entityContext.Commit();
 71		}
 72
 73		public ISaga Get(Guid id)
 74		{
 75			var sagaHolder = (from s in _entityContext.Entities
 76								  where s.Id == id
 77								  select s).SingleOrDefault();
 78			var saga = _sagaConverter.ToSaga(sagaHolder);
 79			return saga;
 80		}
 81
 82
 83		public ISaga Get(string partition, string key)
 84		{
 85			var query = from s in _entityContext.Entities
 86			            where s.Partition == partition && s.Key == key
 87			            select _sagaConverter.ToSaga(s);
 88
 89			return query.SingleOrDefault();
 90		}
 91
 92		public IEnumerable<ISaga> GetForPartition(string partition)
 93		{
 94			var sagaHolders = (from s in _entityContext.Entities
 95			                      where s.Partition == partition
 96			                      select s).ToArray();
 97			var sagas = sagaHolders.Select(_sagaConverter.ToSaga);
 98			return sagas;
 99		}
100#pragma warning restore 1591 // Xml Comments
101
102
103        SagaHolder GetExistingIfAny(Guid id)
104		{
105			var sagaHolder = (from s in _entityContext.Entities
106								  where s.Id == id
107								  select s).SingleOrDefault();
108			return sagaHolder;
109		}
110	}
111}