private void RegisterType(Type serviceType, Type serviceImplementationType, object tag, RegistrationType registrationType, bool registerIfAlreadyRegistered, object originalContainer, Func<ServiceLocatorRegistration, object> createServiceFunc)
{
Argument.IsNotNull("serviceType", serviceType);
if (serviceImplementationType == null)
{
// Dynamic late-bound type
serviceImplementationType = typeof(LateBoundImplementation);
}
if (serviceImplementationType.IsInterfaceEx())
{
throw Log.ErrorAndCreateException<InvalidOperationException>("Cannot register interface type '{0}' as implementation, make sure to specify an actual class", serviceImplementationType.GetSafeFullName(false));
}
/* TODO: This code have to be here to ensure the right usage of non-generic overloads of register methods.
* TODO: If it is finally accepted then remove it from ServiceLocatorAutoRegistrationManager
if (serviceImplementationType.IsAbstractEx() || !serviceType.IsAssignableFromEx(serviceImplementationType))
{
string message = string.Format("The type '{0}' is abstract or can't be registered as '{1}'", serviceImplementationType, serviceType);
Log.Error(message);
throw new InvalidOperationException(message);
}
*/
// Outside lock scope for event
ServiceLocatorRegistration registeredTypeInfo = null;
lock (_lockObject)
{
if (!registerIfAlreadyRegistered && IsTypeRegistered(serviceType, tag))
{
//Log.Debug("Type '{0}' already registered, will not overwrite registration", serviceType.FullName);
return;
}
var serviceInfo = new ServiceInfo(serviceType, tag);
if (_registeredInstances.ContainsKey(serviceInfo))
{
_registeredInstances.Remove(serviceInfo);
}
Log.Debug("Registering type '{0}' to type '{1}'", serviceType.FullName, serviceImplementationType.FullName);
registeredTypeInfo = new ServiceLocatorRegistration(serviceType, serviceImplementationType, tag, registrationType,
x => (createServiceFunc != null) ? createServiceFunc(x) : CreateServiceInstance(x));
_registeredTypes[serviceInfo] = registeredTypeInfo;
}
TypeRegistered.SafeInvoke(this, () => new TypeRegisteredEventArgs(registeredTypeInfo.DeclaringType, registeredTypeInfo.ImplementingType, tag, registeredTypeInfo.RegistrationType));
Log.Debug("Registered type '{0}' to type '{1}'", serviceType.FullName, serviceImplementationType.FullName);
}