/// <summary> /// Create a new UoW app service in this context. /// </summary> public AppServicesBase GetAppService(Type appServiceType) { if (this.AllowedAppServiceTypes != null && !this.AllowedAppServiceTypes.Contains(appServiceType)) { throw new InvalidOperationException("App service of this type is not allowed in this app context."); } AppServicesBase result = null; if (appServiceType.IsInterface) { result = _reusableAppServices[appServiceType]; } else { if (appServiceType.IsAssignableFrom(typeof(AppServicesBase))) { ReusableInContextAppServiceAttribute reusableInContextAttribute = Attribute.GetCustomAttribute(appServiceType, typeof(ReusableInContextAppServiceAttribute)) as ReusableInContextAppServiceAttribute; if (reusableInContextAttribute != null) { result = _reusableAppServices[appServiceType]; if (result == null) { lock (_reusableAppServicesSyncRoot) { result = _reusableAppServices[appServiceType]; if (result == null) { result = (AppServicesBase)Activator.CreateInstance(appServiceType); _reusableAppServices.Add(appServiceType, result); result.InitializeAppService(this); } } } } else { result = (AppServicesBase)Activator.CreateInstance(appServiceType); result.InitializeAppService(this); } } else { throw new InvalidOperationException("The requested type is not an interface or an AppServiceBase derived type."); } } if (result == null) { throw new KeyNotFoundException("The requested app service does not exist."); } return(result); }
/// <summary> /// Create a new UoW app service in this context. /// </summary> public T GetAppService <T>() where T : AppServicesBase, new() { Type appServiceType = typeof(T); if (this.AllowedAppServiceTypes != null && !this.AllowedAppServiceTypes.Contains(appServiceType)) { throw new InvalidOperationException("App service of this type is not allowed in this app context."); } T result = null; ReusableInContextAppServiceAttribute reusableInContextAttribute = Attribute.GetCustomAttribute(appServiceType, typeof(ReusableInContextAppServiceAttribute)) as ReusableInContextAppServiceAttribute; if (reusableInContextAttribute != null) { result = _reusableAppServices[appServiceType] as T; if (result == null) { lock (_reusableAppServicesSyncRoot) { result = _reusableAppServices[appServiceType] as T; if (result == null) { result = new T(); _reusableAppServices.Add(appServiceType, result); result.InitializeAppService(this); } } } } else { result = new T(); result.InitializeAppService(this); } return(result); }