<commit_before>﻿using Orleans.Sagas;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Orleans.Sagas
{
    public class SagaBuilder : ISagaBuilder
    {
        private readonly List<ActivityDefinition> activities;
        private readonly IGrainFactory grainFactory;

        public Guid Id { get; private set; }

        public SagaBuilder(IGrainFactory grainFactory) : this(grainFactory, Guid.NewGuid())
        {
        }

        public SagaBuilder(IGrainFactory grainFactory, Guid id)
        {
            this.grainFactory = grainFactory;
            Id = id;
            activities = new List<ActivityDefinition>();
        }

        public ISagaBuilder AddActivity<TActivity>() where TActivity : IActivity
        {
            activities.Add(new ActivityDefinition(typeof(TActivity)));
            return this;
        }

        public ISagaBuilder AddActivity<TActivity, TConfig>(TConfig config) where TActivity : IActivity<TConfig>
        {
            activities.Add(new ActivityDefinition<TConfig>(typeof(TActivity), config));
            return this;
        }

        public ISagaBuilder AddActivity<TActivity, TConfig>(Action<TConfig> configDelegate) where TActivity : IActivity<TConfig>
        {
            var config = Activator.CreateInstance<TConfig>();
            configDelegate.Invoke(config);
            AddActivity<TActivity, TConfig>(config);
            return this;
        }

        public async Task<ISagaGrain> ExecuteSagaAsync()
        {
            if (activities.Count == 0)
            {
                throw new IndexOutOfRangeException("At least one activity must be present.");
            }

            var sagaGrain = grainFactory.GetGrain<ISagaGrain>(Id);

            await sagaGrain.Execute(activities);

            return sagaGrain;
        }
    }
}<commit_msg>Added todo.<commit_after>﻿using Orleans.Sagas;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Orleans.Sagas
{
    public class SagaBuilder : ISagaBuilder
    {
        private readonly List<ActivityDefinition> activities;
        private readonly IGrainFactory grainFactory;

        public Guid Id { get; private set; }

        public SagaBuilder(IGrainFactory grainFactory) : this(grainFactory, Guid.NewGuid())
        {
        }

        public SagaBuilder(IGrainFactory grainFactory, Guid id)
        {
            this.grainFactory = grainFactory;
            Id = id;
            activities = new List<ActivityDefinition>();
        }

        public ISagaBuilder AddActivity<TActivity>() where TActivity : IActivity
        {
            activities.Add(new ActivityDefinition(typeof(TActivity)));
            return this;
        }

        public ISagaBuilder AddActivity<TActivity, TConfig>(TConfig config) where TActivity : IActivity<TConfig>
        {
            // todo: serialize dynamic activity config safely.
            activities.Add(new ActivityDefinition<TConfig>(typeof(TActivity), config));
            return this;
        }

        public ISagaBuilder AddActivity<TActivity, TConfig>(Action<TConfig> configDelegate) where TActivity : IActivity<TConfig>
        {
            var config = Activator.CreateInstance<TConfig>();
            configDelegate.Invoke(config);
            AddActivity<TActivity, TConfig>(config);
            return this;
        }

        public async Task<ISagaGrain> ExecuteSagaAsync()
        {
            if (activities.Count == 0)
            {
                throw new IndexOutOfRangeException("At least one activity must be present.");
            }

            var sagaGrain = grainFactory.GetGrain<ISagaGrain>(Id);

            await sagaGrain.Execute(activities);

            return sagaGrain;
        }
    }
}
{'fraction_non_alphanumeric': 0.08225337212377677, 'fraction_numerical': 0.0005289605924358636, 'mean_word_length': 2.280138768430182, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1048564', 'n_tokens_mistral': 976, 'n_tokens_neox': 942, 'n_words': 221}