public bool TryBuildAutoFactory( Type typeOfAutoFactory, TUnityContainer container, Func <bool> isRegisteredInContainer, out Delegate autoFactory) { autoFactory = null; // Likely, an overwhelming majority of types // that are being resolved, are not parameterized func-s. // So we check for that first to get out of the way // as fast as possible. if (!typeOfAutoFactory.IsParameterizedFunc()) { return(false); } // If a type has been explicitly registered in the container, // the user expects that registration to be in effect, // so we must leave this type alone. if (isRegisteredInContainer()) { return(false); } autoFactory = ParameterizedAutoFactoryFactoryFactory.BuildAutoFactory < TUnityContainer, TResolverOverride, TParameterByTypeOverride>(typeOfAutoFactory, container); return(true); }
public bool TryGetOrBuildAutoFactoryCreator( Type typeOfAutoFactory, Func <bool> isRegisteredInContainer, out Func <TUnityContainer, Delegate> createAutoFactory) { createAutoFactory = null; // Likely, an overwhelming majority of types // that are being resolved, are not parameterized func-s. // So we check for that first to get out of the way // as fast as possible. if (!typeOfAutoFactory.IsParameterizedFunc()) { return(false); } // See if we can find an existing auto-factory before wasting time on locking. // If we find an existing auto-factory, that means we have decided to handle // the inspected type previously, and so we'll handle it now too. if (_existingAutoFactoryCreators.TryGetValue(typeOfAutoFactory, out createAutoFactory)) { return(true); } // If a type has been explicitly registered in the container, // the user expects that registration to be in effect, // so we must leave this type alone. if (isRegisteredInContainer()) { return(false); } lock (_existingAutoFactoriesLock) { // See if another thread created the auto-factory we are looking for, // while we were waiting to lock. if (_existingAutoFactoryCreators.TryGetValue(typeOfAutoFactory, out createAutoFactory)) { return(true); } createAutoFactory = ParameterizedAutoFactoryFactoryFactory.BuildAutoFactoryCreator < TUnityContainer, TResolverOverride, TParameterByTypeOverride>(typeOfAutoFactory); _existingAutoFactoryCreators.AddOrReplace(typeOfAutoFactory, createAutoFactory); return(true); } }