/// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Returns a new AppContext instance with the specificed services injected.
        /// </summary>
        /// <param name="options">A collection of custom options that can be used by AppService UoW classes for application specific purposes.</param>
        /// <returns></returns>
        public TAppContext NewAppContext(AppOptions options)
        {
            TAppContext result = new TAppContext();

            result._errorLogger      = _errorLogger;
            result._auditLogger      = _auditLogger;
            result._backgroundAction = _backgroundAction;

            if (options != null)
            {
                result._options = options;
            }
            else
            {
                result._options = _options;
            }

            foreach (var appService in _appServices)
            {
                AppServicesBase appServiceInst = (AppServicesBase)Activator.CreateInstance(appService.Value);

                appServiceInst.InitializeAppService(result);

                result._reusableAppServices.Add(appService.Key, appServiceInst);
            }

            return(result);
        }