/// <summary>
        /// Register Perimeter by name using service interface as key.
        /// </summary>
        /// <param name="serviceInterface">Type of interface i.e. IUsersService</param>
        /// <param name="api_name">Name of API that implements passed service interface</param>
        public static void RegisterPerimeter(this RegistrationHelper helper, Type serviceInterface, string api_name)
        {
            IPerimeter perimeter;

            helper.EndPointDict.TryGetValue(api_name, out perimeter);

            if (perimeter == null)
            {
                throw new Exception($"A Perimeter named {api_name} was not found.  Call RegisterEndPoints before calling Register.");
            }

            string existingAPIRegistration = null;

            helper.ServiceRegistrations.TryGetValue(serviceInterface.FullName, out existingAPIRegistration);

            if (existingAPIRegistration != null && existingAPIRegistration != api_name)
            {
                throw new Exception($"A service type can be registered with only one API.  Type {serviceInterface.Name} has been registered with an API named {existingAPIRegistration}.  A second attempt to register the same type with API {api_name} is being made.");
            }
            else if (existingAPIRegistration == null)
            {
                helper.ServiceRegistrations.Add(serviceInterface.FullName, api_name);
            }

            helper.Builder.RegisterInstance <IPerimeter>(perimeter).Keyed <IPerimeter>(serviceInterface);
        }
        /// <summary>
        /// Registers a service. Call RegisterEndPoints before calling this method.
        /// </summary>
        /// <typeparam name="TService">Concrete class that implements TInterface i.e. OrdersService</typeparam>
        /// <typeparam name="TInterface">Interface of service i.e. IOrdersService</typeparam>
        /// <param name="endPointType">Type of client that will access this service i.e. HTTP, InProcess, WCF</param>
        /// <param name="apiName">API_Name of EndPointConfiguration objects  TInterface</param>
        /// <param name="providerName">Similar to provider name in a connection string, describes technology provider i.e. MSSQL, MySQL, SQLNCLI, etc.</param>
        public static RegistrationHelper RegisterService <TService, TInterface>(this RegistrationHelper helper, string endPointType, string apiName, string providerName)
        {
            if (String.IsNullOrEmpty(endPointType))
            {
                throw new ArgumentNullException(nameof(endPointType));
            }
            if (string.IsNullOrEmpty(apiName))
            {
                throw new ArgumentNullException(nameof(apiName));
            }
            if (providerName == null)
            {
                providerName = string.Empty;
            }


            RegisterPerimeter(helper, typeof(TInterface), apiName);

            helper.Builder.Register <Func <string, string, TInterface> >(c => {
                ILifetimeScope cxt = c.Resolve <ILifetimeScope>();
                return((ept, pn) => new ResolutionHelper(cxt).ResolveClient <TInterface>(ept, pn));
            });
            helper.Builder.RegisterType <TService>().Keyed <TInterface>(endPointType + providerName);
            return(helper);
        }
        /// <summary>
        /// Registers a service. Call RegisterEndPoints before calling this method.
        /// </summary>
        /// <typeparam name="TService">Concrete class that implements TInterface i.e. OrdersService</typeparam>
        /// <typeparam name="TInterface">Interface of service i.e. IOrdersService</typeparam>
        /// <param name="endPoint">IEndPointConfiguration with EndPointType, API_Name, and providerName to use as keys</param>
        public static RegistrationHelper RegisterService <TService, TInterface>(this RegistrationHelper helper, IEndPointConfiguration endPoint)
        {
            if (endPoint == null)
            {
                throw new ArgumentNullException(nameof(endPoint));
            }

            RegisterService <TService, TInterface>(helper, endPoint.EndPointType, endPoint.API_Name, endPoint.ProviderName);
            return(helper);
        }
        /// <summary>
        /// Registers an implementation of IDbContextOptions keyed to the supplied providerName.
        /// </summary>
        /// <typeparam name="TOptions">Type that implements IDbContextOptions.</typeparam>
        /// <param name="helper">An instance of RegistrationHelper.</param>
        /// <param name="providerName">ProviderName typically represents some implementation of technology such as a DBMS platform.
        /// Examples might be: MSSQL, MySQL, SQLite, etc.</param>
        /// <returns>RegistrationHelper</returns>
        public static RegistrationHelper RegisterDbContextOptions <TOptions>(this RegistrationHelper helper, string providerName) where TOptions : IDbContextOptions
        {
            if (string.IsNullOrEmpty(providerName))
            {
                throw new ArgumentNullException("providerName");
            }

            helper.Builder.RegisterType <TOptions>().Keyed <IDbContextOptions>(providerName);
            return(helper);
        }
        /// <summary>
        /// Registers a validator for a given EndPointType.  A validator is used to determine if an EndPoint is alive and able to handle requests.
        /// </summary>
        /// <typeparam name="TValidator">The implementation of IEndPointValidator that will handle validation requests for the specified EndPointType</typeparam>
        /// <param name="endPointType">The type of EndPoint that will be validated by the specified implementation of IEndPointValidator</param>
        /// <returns></returns>
        public static RegistrationHelper RegisterEndPointValidator <TValidator>(this RegistrationHelper helper, string endPointType, string providerName) where TValidator : IEndPointValidator
        {
            if (String.IsNullOrEmpty(endPointType))
            {
                throw new ArgumentNullException(nameof(endPointType));
            }
            if (string.IsNullOrEmpty(providerName))
            {
                throw new ArgumentNullException(nameof(providerName));
            }

            helper.Builder.RegisterType <TValidator>().Keyed <IEndPointValidator>(endPointType + providerName);
            return(helper);
        }
        public static RegistrationHelper RegisterModule(this RegistrationHelper helper, params IAdaptiveClientModule[] modules)
        {
            if (!modules?.Any() ?? false)
            {
                return(helper);
            }

            foreach (IAdaptiveClientModule module in modules)
            {
                module.Register(helper);
            }

            return(helper);
        }
        /// <summary>
        /// Registers an implementation of IDatabaseinitializer, an object that is used to initialize or seed a database after it is created or migrations are applied.
        /// </summary>
        /// <typeparam name="TInitalizer">Type that implements IDatabaseInitializer.</typeparam>
        /// <param name="helper">An instance of RegistrationHelper.</param>
        /// <param name="apiName">An API_Name to use as a key.  Must match the API_Name of one or more IEndPointConfiguration objects.
        /// API_Name is an arbitrary name given to the collection of services exposed by an API.
        /// The name of a database or a domain name are examples of names that might also be used as an API_Name. </param>
        /// <param name="providerName">ProviderName typically represents some implementation of technology such as a DBMS platform.
        /// Examples might be: MSSQL, MySQL, SQLite, etc.</param>
        /// <returns>RegistrationHelper</returns>
        public static RegistrationHelper RegisterDatabaseInitializer <TInitalizer>(this RegistrationHelper helper, string apiName, string providerName) where TInitalizer : IDatabaseInitializer
        {
            if (string.IsNullOrEmpty(apiName))
            {
                throw new ArgumentNullException("apiName");
            }
            if (string.IsNullOrEmpty(providerName))
            {
                throw new ArgumentNullException("providerName");
            }

            helper.Builder.RegisterType <TInitalizer>().Keyed <IDatabaseInitializer>(apiName + providerName);
            return(helper);
        }
        /// <summary>
        /// Registers an implementation of IMigrationContext.  A MigrationContext is a placeholder type that allows AdaptiveClient to associate an API_Name
        /// and ProviderName with specific implementations of DbContext and DbContextOptions.
        /// </summary>
        /// <typeparam name="TContext">Type that derives from DbContext and implements IMigratinContext.</typeparam>
        /// <param name="helper">An instance of RegistrationHelper.</param>
        /// <param name="apiName">An API_Name to use as a key.  Must match the API_Name of one or more IEndPointConfiguration objects.
        /// API_Name is an arbitrary name given to the collection of services exposed by an API.
        /// The name of a database or a domain name are examples of names that might also be used as an API_Name. </param>
        /// <param name="providerName">ProviderName typically represents some implementation of technology such as a DBMS platform.
        /// Examples might be: MSSQL, MySQL, SQLite, etc.</param>
        /// <returns>RegistrationHelper</returns>
        public static RegistrationHelper RegisterMigrationContext <TContext>(this RegistrationHelper helper, string apiName, string providerName) where TContext : DbContext, IMigrationContext
        {
            if (string.IsNullOrEmpty(apiName))
            {
                throw new ArgumentNullException("apiName");
            }
            if (string.IsNullOrEmpty(providerName))
            {
                throw new ArgumentNullException("providerName");
            }

            helper.Builder.RegisterType <TContext>().Keyed <IMigrationContext>(apiName + providerName);
            return(helper);
        }
        /// <summary>
        /// Registers a class that derives from DbContext using the supplied apiName as a key.
        /// </summary>
        /// <typeparam name="TContext">Type that derives from DbContext.</typeparam>
        /// <param name="helper">An instance of RegistrationHelper.</param>
        /// <param name="apiName">An API_Name to use as a key.  Must match the API_Name of one or more IEndPointConfiguration objects.
        /// API_Name is an arbitrary name given to the collection of services exposed by an API.
        /// The name of a database or a domain name are examples of names that might also be used as an API_Name.</param>
        /// <returns>RegistrationHelper</returns>
        public static RegistrationHelper RegisterDbContext <TContext>(this RegistrationHelper helper, string apiName) where TContext : DbContext
        {
            if (string.IsNullOrEmpty(apiName))
            {
                throw new ArgumentNullException("apiName");
            }

            helper.Builder.RegisterType <TContext>().InstancePerLifetimeScope(); // DbContext is normally injected into a service - no key needed

            // For when we have an endPoint and we need to know what DbContext is associated.
            // We do not want InstancePerLifetimeScope here - when looping through a collection of EndPoints
            // we want a new instance for each endpoint.
            helper.Builder.RegisterType <TContext>().Keyed <DbContext>(apiName).UsingConstructor(typeof(DbContextOptions));
            return(helper);
        }
        /// <summary>
        /// Registers a collection of EndPointConfiguration objects.
        /// </summary>
        /// <param name="endPoints">Collection of EndPointConfiguration objects</param>
        public static RegistrationHelper RegisterEndPoints(this RegistrationHelper helper, IEnumerable <IEndPointConfiguration> endPoints)
        {
            if (endPoints == null)
            {
                throw new ArgumentNullException(nameof(endPoints));
            }

            // Do not register endpoints with the container.  A list of endpoints is available when a Perimeter is resolved.
            endPoints = endPoints.Where(x => x.IsActive);
            EndPointUtilities.ValidateEndPoints(endPoints);

            foreach (var perimeter in endPoints.GroupBy(x => x.API_Name))
            {
                helper.EndPointDict.Add(perimeter.Key, new Perimeter(perimeter.Key, perimeter.ToList()));
            }

            return(helper);
        }
 /// <summary>
 /// Registers an Action that accepts logging messages.
 /// </summary>
 /// <param name="logger"></param>
 public static RegistrationHelper RegisterLogger(this RegistrationHelper helper, Action <string> logger)
 {
     helper.Builder.RegisterInstance <Action <string> >(logger);
     return(helper);
 }
Exemplo n.º 12
0
        /// <summary>
        /// Registers a ServiceManifest, a class that exposes a property for each type of service associated with an API.  A ServiceManifest is keyed to an EndPointType, an API_Name, and a ProviderName.
        /// </summary>
        /// <typeparam name="TService">Type of service manifest. Must derive from ServiceManifestFactory.</typeparam>
        /// <typeparam name="TInterface">Interface that describes TService.</typeparam>
        /// <param name="helper">An instance of RegistrationHelper.</param>
        /// <param name="endPointType">Describes the technology or protocol used by an IEndPointConfiguration.</param>
        /// <param name="apiName">An API_Name to use as a key.  Must match the API_Name of one or more IEndPointConfiguration objects.
        /// API_Name is an arbitrary name given to the collection of services exposed by an API.
        /// The name of a database or a domain name are examples of names that might also be used as an API_Name. </param>
        /// <param name="providerName">ProviderName typically represents some implementation of technology such as a DBMS platform.
        /// Examples might be: MSSQL, MySQL, SQLite, etc.</param>
        /// <returns></returns>
        public static RegistrationHelper RegisterServiceManifest <TService, TInterface>(this RegistrationHelper helper, string endPointType, string apiName, string providerName) where TService : ServiceManifestFactory
        {
            if (string.IsNullOrEmpty(endPointType))
            {
                throw new ArgumentNullException(nameof(endPointType));
            }
            if (string.IsNullOrEmpty(apiName))
            {
                throw new ArgumentNullException(nameof(apiName));
            }
            if (string.IsNullOrEmpty(providerName))
            {
                throw new ArgumentNullException(nameof(providerName));
            }

            helper.RegisterPerimeter(typeof(TInterface), apiName);

            helper.Builder.Register <Func <string, string, TInterface> >(c => {
                ILifetimeScope cxt = c.Resolve <ILifetimeScope>();
                return((ept, pn) => new ResolutionHelper(cxt).ResolveClient <TInterface>(ept, pn));
            }).PropertiesAutowired();

            helper.Builder.RegisterType <TService>().Keyed <TInterface>(endPointType + providerName).PropertiesAutowired().InstancePerLifetimeScope();
            helper.Builder.RegisterType <TService>().As <TInterface>().PropertiesAutowired().InstancePerLifetimeScope();
            return(helper);
        }