Created
July 13, 2014 15:41
-
-
Save StacyGay/d987cbf5940f1f758f89 to your computer and use it in GitHub Desktop.
InterfaceFactory and InterfaceExtension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum ServiceTypes { Main, Alt }; | |
public interface IConfig | |
{ | |
ServiceTypes ServiceSetting { get; set; } | |
} | |
public class Config : IConfig | |
{ | |
public ServiceTypes ServiceSetting { get; set; } | |
} | |
public abstract class InterfaceExtension : UnityContainerExtension | |
{ | |
protected override void Initialize() | |
{ | |
if (!ValidExtension()) return; | |
LoadDependencies(); | |
LoadExtensions(); | |
} | |
public abstract bool ValidExtension(); | |
public abstract void LoadExtensions(); | |
public abstract void LoadDependencies(); | |
} | |
public class RoomExtension : InterfaceExtension | |
{ | |
private readonly Config _config; | |
public IQWareExtension(Config config) | |
{ | |
_config = config; | |
} | |
public override bool ValidExtension() | |
{ | |
return _config.ServiceSetting == ServiceTypes.Main; | |
} | |
public override void LoadDependencies() | |
{ | |
Container.RegisterType<IBusinessLogicService, MainLogicService>(); | |
} | |
public override void LoadExtensions() | |
{ | |
// Further extensions can go here | |
} | |
} | |
public class InterfaceFactory | |
{ | |
protected IUnityContainer Container; | |
private IConfig _config; | |
protected InterfaceFactory(IUnityContainer container) | |
{ | |
Container = container.CreateChildContainer(); | |
if (!container.IsRegistered<IConfig>()) | |
return; | |
_config = container.Resolve<IConfig>(); | |
LoadExtensions(); | |
} | |
public InterfaceFactory GetNewFactoryInstance() | |
{ | |
return (InterfaceFactory)Activator.CreateInstance(typeof(InterfaceFactory), new object[] { Container.Parent }); | |
} | |
public InterfaceFactory GetNewFactoryInstance(IConfig config) | |
{ | |
var instance = GetNewFactoryInstance(); | |
instance.Configure(config); | |
return instance; | |
} | |
private void LoadExtensions() | |
{ | |
var extensions = Container.ResolveAll<InterfaceExtension>().ToList(); | |
extensions.ForEach(extension => Container.AddExtension(extension)); | |
} | |
public virtual void Configure(IConfig config, Action<IUnityContainer> configAction = null) | |
{ | |
_config = config; | |
var parentContainer = Container.Parent; | |
Container.Dispose(); | |
Container = parentContainer.CreateChildContainer(); | |
Container.RegisterInstance(_config, new ExternallyControlledLifetimeManager()); | |
if (configAction != null) | |
configAction.Invoke(Container); | |
LoadExtensions(); | |
} | |
public T GetService<T>() | |
{ | |
if (_config == null) | |
throw new DependencyMissingException("InterfaceFactory has not been loaded with config"); | |
try | |
{ | |
return Container.Resolve<T>(); | |
} | |
catch (Exception ex) | |
{ | |
throw new DependencyMissingException("InterfaceFactory cannot resolve type: " + typeof(T).FullName, ex); | |
} | |
} | |
public void ReleaseInstance<T>() | |
{ | |
var instance = Container.Registrations.FirstOrDefault(r => r.RegisteredType == typeof(T)); | |
if (instance != null) | |
instance.LifetimeManager.RemoveValue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment