<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CSF.Screenplay.Scenarios
{
  /// <summary>
  /// Concrete implementation of <see cref="IServiceRegistryBuilder"/>.
  /// </summary>
  public class ServiceRegistryBuilder : IServiceRegistryBuilder
  {
    readonly ICollection<IServiceRegistration> registrations;

    /// <summary>
    /// Registers a service which will be used across all scenarios within a test run.
    /// </summary>
    /// <param name="instance">Instance.</param>
    /// <param name="name">Name.</param>
    /// <typeparam name="TService">The 1st type parameter.</typeparam>
    public void RegisterSingleton<TService>(TService instance, string name = null) where TService : class
    {
      var meta = GetMetadata<TService>(name, ServiceLifetime.Singleton);
      var reg = new SingletonRegistration(meta, instance);
      registrations.Add(reg);
    }

    /// <summary>
    /// Registers a service which will be used across all scenarios within a test run.
    /// </summary>
    /// <param name="initialiser">Initialiser.</param>
    /// <param name="name">Name.</param>
    /// <typeparam name="TService">The 1st type parameter.</typeparam>
    public void RegisterSingleton<TService>(Func<TService> initialiser, string name = null) where TService : class
    {
      var meta = GetMetadata<TService>(name, ServiceLifetime.Singleton);
      var fac = GetNonGenericFactory(initialiser);
      var reg = new LazySingletonRegistration(meta, fac);
      registrations.Add(reg);
    }

    /// <summary>
    /// Registers a service which will be constructed afresh (using the given factory function) for
    /// each scenario within a test run.
    /// </summary>
    /// <param name="factory">Factory.</param>
    /// <param name="name">Name.</param>
    /// <typeparam name="TService">The 1st type parameter.</typeparam>
    public void RegisterPerScenario<TService>(Func<IServiceResolver,TService> factory, string name = null) where TService : class
    {
      var meta = GetMetadata<TService>(name, ServiceLifetime.PerScenario);
      var fac = GetNonGenericFactory(factory);
      var reg = new PerScenarioRegistration(meta, fac);
      registrations.Add(reg);
    }

    /// <summary>
    /// Builds and returns a service registry.
    /// </summary>
    /// <returns>The registry.</returns>
    public ServiceRegistry BuildRegistry()
    {
      var builtRegistrations = new ReadOnlyCollection<IServiceRegistration>(registrations.ToList());
      return new ServiceRegistry(builtRegistrations);
    }

    ServiceMetadata GetMetadata<TService>(string name, ServiceLifetime lifetime)
    {
      return new ServiceMetadata(typeof(TService), name, lifetime);
    }

    Func<IServiceResolver,object> GetNonGenericFactory<TService>(Func<IServiceResolver,TService> genericFactory)
    {
      return scenario => genericFactory(scenario);
    }
<commit_msg>Resolve #84 - Add logic for ownership of registration metadata<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace CSF.Screenplay.Scenarios
{
  /// <summary>
  /// Concrete implementation of <see cref="IServiceRegistryBuilder"/>.
  /// </summary>
  public class ServiceRegistryBuilder : IServiceRegistryBuilder
  {
    readonly ICollection<IServiceRegistration> registrations;

    /// <summary>
    /// Registers a service which will be used across all scenarios within a test run.
    /// </summary>
    /// <param name="instance">Instance.</param>
    /// <param name="name">Name.</param>
    /// <typeparam name="TService">The 1st type parameter.</typeparam>
    public void RegisterSingleton<TService>(TService instance, string name = null) where TService : class
    {
      var meta = GetMetadata<TService>(name, ServiceLifetime.Singleton, false);
      var reg = new SingletonRegistration(meta, instance);
      registrations.Add(reg);
    }

    /// <summary>
    /// Registers a service which will be used across all scenarios within a test run.
    /// </summary>
    /// <param name="initialiser">Initialiser.</param>
    /// <param name="name">Name.</param>
    /// <typeparam name="TService">The 1st type parameter.</typeparam>
    public void RegisterSingleton<TService>(Func<TService> initialiser, string name = null) where TService : class
    {
      var meta = GetMetadata<TService>(name, ServiceLifetime.Singleton);
      var fac = GetNonGenericFactory(initialiser);
      var reg = new LazySingletonRegistration(meta, fac);
      registrations.Add(reg);
    }

    /// <summary>
    /// Registers a service which will be constructed afresh (using the given factory function) for
    /// each scenario within a test run.
    /// </summary>
    /// <param name="factory">Factory.</param>
    /// <param name="name">Name.</param>
    /// <typeparam name="TService">The 1st type parameter.</typeparam>
    public void RegisterPerScenario<TService>(Func<IServiceResolver,TService> factory, string name = null) where TService : class
    {
      var meta = GetMetadata<TService>(name, ServiceLifetime.PerScenario);
      var fac = GetNonGenericFactory(factory);
      var reg = new PerScenarioRegistration(meta, fac);
      registrations.Add(reg);
    }

    /// <summary>
    /// Builds and returns a service registry.
    /// </summary>
    /// <returns>The registry.</returns>
    public ServiceRegistry BuildRegistry()
    {
      var builtRegistrations = new ReadOnlyCollection<IServiceRegistration>(registrations.ToList());
      return new ServiceRegistry(builtRegistrations);
    }

    ServiceMetadata GetMetadata<TService>(string name, ServiceLifetime lifetime, bool isResolverOwned = true)
    {
      return new ServiceMetadata(typeof(TService), name, lifetime, isResolverOwned);
    }

    Func<IServiceResolver,object> GetNonGenericFactory<TService>(Func<IServiceResolver,TService> genericFactory)
    {
      return scenario => genericFactory(scenario);
    }

{'fraction_non_alphanumeric': 0.12098110705999338, 'fraction_numerical': 0.001325820351342393, 'mean_word_length': 3.500372856077554, 'pattern_counts': {'":': 0, '<': 93, '<?xml version=': 0, '>': 95, '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': '408965', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1604, 'n_words': 427}