Stylet/Stylet/StyletIoC/Internal/RegistrationCollections/RegistrationCollection.cs

43 lines
1.4 KiB
C#

using StyletIoC.Creation;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace StyletIoC.Internal.RegistrationCollections
{
internal class RegistrationCollection : IRegistrationCollection
{
private readonly object registrationsLock = new object();
private readonly List<IRegistration> registrations;
public RegistrationCollection(List<IRegistration> registrations)
{
this.registrations = registrations;
}
public IRegistration GetSingle()
{
throw new StyletIoCRegistrationException("Multiple registrations found.");
}
public List<IRegistration> GetAll()
{
List<IRegistration> registrationsCopy;
lock (this.registrationsLock) { registrationsCopy = this.registrations.ToList(); }
return registrationsCopy;
}
public IRegistrationCollection AddRegistration(IRegistration registration)
{
// Need to lock the list, as someone might be fetching from it while we do this
lock (this.registrationsLock)
{
// Should have been caught by SingleRegistration.AddRegistration
Debug.Assert(this.registrations.All(x => x.Type != registration.Type));
this.registrations.Add(registration);
return this;
}
}
}
}