Exemplo n.º 1
0
        /// <summary>
        /// Returns an instance of DbContext keyed to the API_Name of the passed IEndPointConfiguration.
        /// </summary>
        /// <param name="helper">An instance of ResolutionHelper.</param>
        /// <param name="ep">The IEndPointConfiguration whose properties will be used as keys.</param>
        /// <returns>DbContext</returns>
        public static DbContext ResolveDbContext(this ResolutionHelper helper, IEndPointConfiguration ep)
        {
            if (ep == null)
            {
                throw new ArgumentNullException("ep");
            }

            IDbContextOptions options = null;

            try
            {
                options = ResolveDbContextOptions(helper, ep);
            }
            catch (ComponentNotRegisteredException ex)
            {
                throw new ComponentNotRegisteredException($"DbContext could not be resolved. See InnerException for additional detail.", ex);
            }

            DbContext context = helper.scope.ResolveOptionalKeyed <DbContext>(ep.API_Name, new TypedParameter(typeof(DbContextOptions), options.Options));

            if (context == null)
            {
                throw new ComponentNotRegisteredException($"DbContext could not be resolved for API_Name {ep.API_Name}. Call RegisterDbContext with an API_Name of {ep.API_Name} to register the required component.");
            }

            return(context);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Registers a client. Call RegisterEndPoints before calling this method.
        /// </summary>
        /// <typeparam name="TClient">Concrete class that implements TInterface i.e. OrdersClient</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="api_name">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 IRegistrationHelper Register <TClient, TInterface>(string endPointType, string api_name, string providerName)
        {
            if (String.IsNullOrEmpty(endPointType))
            {
                throw new ArgumentNullException("endPointType");
            }
            if (string.IsNullOrEmpty(api_name))
            {
                throw new ArgumentNullException("api_name");
            }
            if (providerName == null)
            {
                providerName = string.Empty;
            }


            RegisterPerimeter(typeof(TInterface), api_name);

            builder.Register <Func <string, string, TInterface> >(c => {
                IComponentContext cxt = c.Resolve <IComponentContext>();
                return((ept, pn) => ResolutionHelper.ResolveClient <TInterface>(cxt, ept, pn));
            });
            builder.RegisterType <TClient>().Keyed <TInterface>(endPointType + providerName);
            return(this);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Returns an implementation of IDbContextOptions keyed to the ProviderName of the passed IEndPointConfiguration.
        /// </summary>
        /// <param name="helper">An instance of ResolutionHelper.</param>
        /// <param name="ep">The IEndPointConfiguration whose properties will be used as keys.</param>
        /// <returns>An implementation of IDbContextOptions.</returns>
        public static IDbContextOptions ResolveDbContextOptions(this ResolutionHelper helper, IEndPointConfiguration ep)
        {
            IDbContextOptions options = helper.scope.ResolveOptionalKeyed <IDbContextOptions>(ep.ProviderName, new TypedParameter(typeof(string), ep.ConnectionString));

            if (options == null)
            {
                throw new ComponentNotRegisteredException($"IDbContextOptions have not been registered for ProviderName {ep.ProviderName}. Call RegisterDbContextOptions with a ProviderName of {ep.ProviderName} to register the required component.");
            }

            return(options);
        }
Exemplo n.º 4
0
        public static TInterface ResolveClient <TInterface>(this ResolutionHelper helper, string ept, string providerName)
        {
            if (providerName == null)
            {
                providerName = string.Empty;
            }

            object result = null;

            helper.cxt.TryResolveKeyed(ept + providerName, typeof(TInterface), out result); // Not an error if not resolved.
            return((TInterface)result);
        }
Exemplo n.º 5
0
        public static IEndPointValidator ResolveValidator(this ResolutionHelper helper, string endPointType, string providerName)
        {
            IEndPointValidator result = null;

            result = helper.cxt.ResolveOptionalKeyed <IEndPointValidator>(endPointType + providerName);

            if (result == null)
            {
                throw new ComponentNotRegisteredException($"An attempt to resolve an EndPointValidator for EndPointType { endPointType } failed.  Use the RegisterEndPointValidator method on the RegistrationHelper to register an EndPointValidator for each EndPointType.");
            }

            return(result);
        }
Exemplo n.º 6
0
        public static IPerimeter ResolvePerimeter(this ResolutionHelper helper, Type typ)
        {
            IPerimeter result = null;

            result = helper.cxt.ResolveOptionalKeyed <IPerimeter>(typ);

            if (result == null)
            {
                throw new ComponentNotRegisteredException($"Interface { typ.Name } has not been registered.  Use RegistrationHelper to register { typ.Name } with one or more implementations, EndPointConfiguration, and API names.");
            }

            return(result);
        }
Exemplo n.º 7
0
        /// <summary>
        /// This overload is primarily for internal use by AdaptiveClient.  Resolves an implementation of IDbContextOptions using
        /// EndPointContext.CurrentEndPoint which is set internally by AdaptiveClient.
        /// </summary>
        /// <param name="helper">An instance of ResolutionHelper.</param>
        /// <returns>An implementation of IDbContextOptions</returns>
        public static IDbContextOptions ResolveDbContextOptions(this ResolutionHelper helper)
        {
            Func <IEndPointConfiguration> epFactory = helper.scope.Resolve <Func <IEndPointConfiguration> >();  // Registered by AdaptiveClient.  Returns EndPointContext.CurrentEndPoint
            IEndPointConfiguration        ep        = epFactory();

            if (ep == null)
            {
                throw new Exception("EndPointContext.CurrentEndPoint is null.");
            }


            return(ResolveDbContextOptions(helper, ep));
        }
Exemplo n.º 8
0
        /// <summary>
        /// Returns a MigrationContext which is a placeholder type that derives from DbContext.  The MigrationContext that is
        /// returned is keyed to API_Name and ProviderName of the passed IEndPointConfiguration.
        /// One or more MigrationContexts might derive from the same DBContext.  The type name of each migration context is
        /// used by AdaptiveClient as a key for resolving supporting objects such as DbContextOptions.  The type name of the
        /// migration context is also used by EF itself for creating migrations and updating the
        /// database (--context command line option).
        /// </summary>
        /// <param name="helper">An instance of ResolutionHelper.</param>
        /// <param name="ep">The IEndPointConfiguration whose properties will be used as keys.</param>
        /// <returns>DbContext</returns>
        public static DbContext ResolveMigrationContext(this ResolutionHelper helper, IEndPointConfiguration ep)
        {
            IDbContextOptions options = null;

            try
            {
                options = ResolveDbContextOptions(helper, ep);
            }
            catch (ComponentNotRegisteredException)
            {
                return(null);
            }

            return(helper.scope.ResolveOptionalKeyed <IMigrationContext>(ep.API_Name + ep.ProviderName, new TypedParameter(typeof(DbContextOptions), options.Options)) as DbContext);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Returns an instance of IDatabaseInitializer which is used to seed a database after it is created or a migration is applied. The instance
        /// of IDatabaseInitializer that is returned is keyed to the API_Name and ProviderName of the passed IEndPointConfiguration.
        /// </summary>
        /// <param name="helper">An instance of ResolutionHelper.</param>
        /// <param name="ep">The IEndPointConfiguration whose properties will be used as keys.</param>
        /// <returns>IDatabaseInitializer</returns>
        public static IDatabaseInitializer ResolveDatabaseInitializer(this ResolutionHelper helper, IEndPointConfiguration ep)
        {
            DbContext context = null;

            try
            {
                context = ResolveDbContext(helper, ep);
            }
            catch (ComponentNotRegisteredException)
            {
                return(null);
            }

            return(helper.scope.ResolveOptionalKeyed <IDatabaseInitializer>(ep.API_Name + ep.ProviderName, new TypedParameter(context.GetType(), context)));
        }
Exemplo n.º 10
0
 protected override void Load(ContainerBuilder builder)
 {
     base.Load(builder);
     builder.RegisterGeneric(typeof(AdaptiveClient <>)).As(typeof(IAdaptiveClient <>)).InstancePerLifetimeScope();
     builder.RegisterType <NetworkUtilities>().As <INetworkUtilities>();
     builder.Register <Func <IEndPointConfiguration> >(c => { IComponentContext cxt = c.Resolve <IComponentContext>(); return(() => cxt.Resolve <EndPointContext>().CurrentEndPoint); });
     builder.Register <Func <Type, IPerimeter> >(c => { IComponentContext cxt = c.Resolve <IComponentContext>(); return(t => ResolutionHelper.ResolvePerimeter(cxt, t)); });
     builder.Register <Func <string, string, IEndPointValidator> >(c => { IComponentContext cxt = c.Resolve <IComponentContext>(); return((eptype, providerName) => ResolutionHelper.ResolveValidator(cxt, eptype, providerName)); });
     builder.RegisterType <EndPointContext>().InstancePerLifetimeScope();     // per lifetimescope - see notes in EndPointContext.cs
     builder.RegisterType <EndPointCache>().SingleInstance();                 // singleton
     builder.RegisterGeneric(typeof(ClientFactory <>)).As(typeof(IClientFactory <>));
     builder.RegisterGeneric(typeof(ClientEvaluator <>)).As(typeof(IClientEvaluator <>));
     builder.RegisterInstance <Action <string> >(msg => { }); // default logger.  User can override by calling RegistrationHelper.RegisterLogger
 }