コード例 #1
0
ファイル: EntityExtensions.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Dynamically configures a CoreXT based DBContext object, and optionally tests that a connection can be made using the given connection string.
        /// </summary>
        /// <typeparam name="TContextProvider">The type of provider to use to get the DBContext instances.</typeparam>
        /// <param name="sp">The CoreXT service provider.</param>
        /// <param name="isReadonly">True of the context should be read-only.</param>
        /// <param name="onConfiguring">An function expression to execute if a new context is created (in order to setup the context, such as setting a connection string).</param>
        /// <param name="commandTimeout">The number of seconds to wait before a command executed against the context times out. If not specified the system default is used, or whatever is specified via the connection string.</param>
        /// <param name="createNew">If true, a new instance is returned and not the per-request cached instance. Default is false.</param>
        /// <param name="testConnectingBeforeReturning">If true (default) calls 'ExecuteSqlCommand()' on the context to make sure the connection is valid.</param>
        /// <returns></returns>
        public static ICoreXTDBContext ConfigureCoreXTDBContext <TContextProvider>(this ICoreXTServiceProvider sp, bool isReadonly, Action <DbContextOptionsBuilder> onConfiguring = null, int?commandTimeout = null, bool createNew = false, bool testConnectingBeforeReturning = true)
            where TContextProvider : class, IContextProvider
        {
            var contextProvider = sp.GetService <TContextProvider>();
            var context         = isReadonly ? contextProvider.GetReadonlyContext(createNew) : contextProvider.GetContext(createNew);

            if (context == null)
            {
                throw new InvalidOperationException("There is no " + typeof(TContextProvider).Name + " service object registered.");
            }

            if (context.Database == null)
            {
                throw new InvalidOperationException("The 'Database' property is null for DBContext type " + typeof(TContextProvider).Name + ".");
            }

            if (commandTimeout != null)
            {
                context.Database.SetCommandTimeout(TimeSpan.FromSeconds(commandTimeout.Value));
            }

            var logger = sp.GetService <ILoggerFactory>()?.CreateLogger <TContextProvider>();

            try
            {
                if (onConfiguring != null)
                {
                    context.Configuring += onConfiguring;
                }

                if (string.IsNullOrWhiteSpace(context.ConnectionString))
                {
                    throw new IOException("Cannot connect to any database - the connection string is empty.");
                }

                try
                {
                    if (testConnectingBeforeReturning)
                    {
                        context.Database.ExecuteSqlCommand("SELECT 1"); // (test the connection now)
                    }

                    return(context);
                }
                catch (Exception ex)
                {
                    logger?.LogError(new EventId(-1, "CoreXT.Entities"), ex, "The database is not reachable.");
                    throw;
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(new EventId(-1, "CoreXT.Entities"), ex, "Error configuring the database context.");
                throw;
            }
            // --------------------------------------------------------------------------------------------------------------------
        }
コード例 #2
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Returns a context to be used as READONLY.  This context type is normally registered for the MVC request scope,
        /// which means a new instance on each request, but the same instance used across the request (for caching).
        /// This greatly helps to reduce database hits.
        /// </summary>
        public static ICoreXTDemoReadonlyContext GetCoreXTDemoReadOnlyContext(this ICoreXTServiceProvider sp, string connectionString = null, int?commandTimeout = null, bool testConnectingBeforeReturning = true)
        {
            if (connectionString == null)
            {
                var settings = sp.GetCoreXTDemoAppSettings();
                connectionString = settings.DefaultConnectionString;
            }
            return((ICoreXTDemoReadonlyContext)sp.ConfigureCoreXTDBContext <ICoreXTDemoContextProvider>(true, options => options.UseMySql(connectionString), commandTimeout, testConnectingBeforeReturning));
        }
コード例 #3
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates a menu control.
        /// </summary>
        public ActionLink(ICoreXTServiceProvider sp) : base(sp)
        {
        }
コード例 #4
0
ファイル: CollapseButton.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates an empty link control.
        /// </summary>
        public CollapseButton(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #5
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates a menu control.
        /// </summary>
        public MenuItem(ICoreXTServiceProvider sp) : base(sp)
        {
        }
コード例 #6
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary> Creates a button component. </summary>
        /// <param name="services"> Application services. </param>
        public Button(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #7
0
 public TableController(ICoreXTServiceProvider sp) : base(sp)
 {
 }
コード例 #8
0
ファイル: Table.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates a menu control.
        /// </summary>
        public Table(ICoreXTServiceProvider sp) : base(sp)
        {
        }
コード例 #9
0
 /// <summary>
 /// Initializes a new instance of <see cref="CompositeViewEngine"/>.
 /// </summary>
 /// <param name="optionsAccessor">The options accessor for <see cref="MvcViewOptions"/>.</param>
 public CompositeViewEngine(IOptions <MvcViewOptions> optionsAccessor, ICoreXTServiceProvider services)
 {
     _CompositeViewEngine = new Microsoft.AspNetCore.Mvc.ViewEngines.CompositeViewEngine(optionsAccessor);
     _Services            = services;
 }
コード例 #10
0
ファイル: LightBox.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        public LightBox(ICoreXTServiceProvider sp) : base(sp)
        {
        }
コード例 #11
0
ファイル: Link.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary> Renders a bootstrap form group. </summary>
        /// <param name="services"> The services. </param>
        public Link(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #12
0
 public static CoreXTDemoAppSettings GetCoreXTDemoAppSettings(this ICoreXTServiceProvider sp)
 {
     return(sp.GetService <IOptions <CoreXTDemoAppSettings> >()?.Value);
 }
コード例 #13
0
ファイル: InputContainer.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Renders a bootstrap form group.
        /// </summary>
        public InputContainer(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #14
0
ファイル: EntityExtensions.cs プロジェクト: rjamesnw/CoreXT
 public ContextProvider(ICoreXTServiceProvider sp)
 {
     _ServiceProvider = sp;
     _HttpContext     = _ServiceProvider.GetService <IHttpContextAccessor>().HttpContext; // (*** this is null if there is no current context, such as being called before and after a request ***)
 }
コード例 #15
0
ファイル: ServiceObject.cs プロジェクト: rjamesnw/CoreXT
        // ------------------------------------------------------------------------------------------------------------------------------------

        public ServiceObject(ICoreXTServiceProvider services)
        {
            _ServiceProvider = services;
        }
コード例 #16
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary> Creates a panel group component. </summary>
        /// <param name="services"> Application services. </param>
        public PanelGroup(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #17
0
 public ViewRenderer(ICoreXTServiceProvider serviceProvider)
 {
     _ServiceProvider = serviceProvider;
 }
コード例 #18
0
 /// <summary>
 /// Construct a new CDS READONLY entities context object using a connection string.
 /// This allows selecting a different database server; for example, based on debug, test, or release (go live) modes.
 /// </summary>
 internal CoreXTDemoReadonlyContext(string nameOrConnectionString, ICoreXTServiceProvider services = null)
     : base(nameOrConnectionString, services)
 {
 }
コード例 #19
0
ファイル: Modal.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Creates an empty modal pop-up.
        /// </summary>
        /// <param name="services"></param>
        public Modal(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #20
0
 public CoreXTDemoContextProvider(ICoreXTServiceProvider sp) : base(sp)
 {
 }
コード例 #21
0
ファイル: FormGroup.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Renders a bootstrap form group.
        /// </summary>
        public FormGroup(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #22
0
 /// <summary>
 /// Construct a new CoreXT.Demos entities context object by passing in the name of a connection string in the web.config file, or a whole connecting string itself.
 /// This allows selecting a different database server; for example, based on debug, test, or release (go live) modes.
 /// </summary>
 internal CoreXTDemoContext(string nameOrConnectionString, ICoreXTServiceProvider services = null)
     : base(new DbContextOptionsBuilder().UseMySql(nameOrConnectionString).Options, services)
 {
 }
コード例 #23
0
ファイル: DbSetXT.cs プロジェクト: rjamesnw/CoreXT
        // --------------------------------------------------------------------------------------------------------------------------------------------

        /// <summary>
        /// Construct a new DbSetXT object.
        /// </summary>
        /// <param name="context">The DbContext this DbSet is associated with.</param>
        /// <param name="services">The CoreXT service provider that the underlying DbContext is associated with.</param>
        public DbSetXT(DbContext context, ICoreXTServiceProvider services)
        {
            DbContext = context ?? throw new ArgumentNullException(nameof(context));
            _DbSet    = new InternalDbSet <TEntity>(DbContext);
            Services  = services;
        }
コード例 #24
0
 public CoreXTDemoContext(ICoreXTServiceProvider services) : base(services)
 {
 }
コード例 #25
0
 public CommonController(ICoreXTServiceProvider services)
 {
     ServiceProvider = services;
 }
コード例 #26
0
 public CoreXTDemoReadonlyContext(DbContextOptions <CoreXTDemoContext> options, ICoreXTServiceProvider services) : base(options, services)
 {
 }
コード例 #27
0
 public HomeController(ICoreXTServiceProvider sp) : base(sp)
 {
     _AppSettings = ServiceProvider.GetCoreXTDemoAppSettings();
 }
コード例 #28
0
        // --------------------------------------------------------------------------------------------------------------------

        /// <summary> Renders a bootstrap form group. </summary>
        /// <param name="services"> The services. </param>
        public Input(ICoreXTServiceProvider services) : base(services)
        {
        }
コード例 #29
0
        // --------------------------------------------------------------------------------------------------------------------

        public Label(ICoreXTServiceProvider sp) : base(sp)
        {
        }
コード例 #30
0
 /// <summary>
 /// Construct a new DBContext with services access.
 /// Giving access to services allows custom LINQ extension methods to have access to application services via DbSetXT.
 /// <para>This method is usually the one called when the context is created from the DI container.</para>
 /// </summary>
 /// <param name="services">A reference to the application services.</param>
 public CoreXTDBContext(ICoreXTServiceProvider services)
 {
     Services = services ?? throw new ArgumentNullException(nameof(services));
     _Populate();
 }