// -------------------------------------------------------------------------------------------------------------------- /// <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; } // -------------------------------------------------------------------------------------------------------------------- }
// -------------------------------------------------------------------------------------------------------------------- /// <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)); }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Creates a menu control. /// </summary> public ActionLink(ICoreXTServiceProvider sp) : base(sp) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Creates an empty link control. /// </summary> public CollapseButton(ICoreXTServiceProvider services) : base(services) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Creates a menu control. /// </summary> public MenuItem(ICoreXTServiceProvider sp) : base(sp) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Creates a button component. </summary> /// <param name="services"> Application services. </param> public Button(ICoreXTServiceProvider services) : base(services) { }
public TableController(ICoreXTServiceProvider sp) : base(sp) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Creates a menu control. /// </summary> public Table(ICoreXTServiceProvider sp) : base(sp) { }
/// <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; }
// -------------------------------------------------------------------------------------------------------------------- public LightBox(ICoreXTServiceProvider sp) : base(sp) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Renders a bootstrap form group. </summary> /// <param name="services"> The services. </param> public Link(ICoreXTServiceProvider services) : base(services) { }
public static CoreXTDemoAppSettings GetCoreXTDemoAppSettings(this ICoreXTServiceProvider sp) { return(sp.GetService <IOptions <CoreXTDemoAppSettings> >()?.Value); }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Renders a bootstrap form group. /// </summary> public InputContainer(ICoreXTServiceProvider services) : base(services) { }
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 ***) }
// ------------------------------------------------------------------------------------------------------------------------------------ public ServiceObject(ICoreXTServiceProvider services) { _ServiceProvider = services; }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Creates a panel group component. </summary> /// <param name="services"> Application services. </param> public PanelGroup(ICoreXTServiceProvider services) : base(services) { }
public ViewRenderer(ICoreXTServiceProvider serviceProvider) { _ServiceProvider = serviceProvider; }
/// <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) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Creates an empty modal pop-up. /// </summary> /// <param name="services"></param> public Modal(ICoreXTServiceProvider services) : base(services) { }
public CoreXTDemoContextProvider(ICoreXTServiceProvider sp) : base(sp) { }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> /// Renders a bootstrap form group. /// </summary> public FormGroup(ICoreXTServiceProvider services) : base(services) { }
/// <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) { }
// -------------------------------------------------------------------------------------------------------------------------------------------- /// <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; }
public CoreXTDemoContext(ICoreXTServiceProvider services) : base(services) { }
public CommonController(ICoreXTServiceProvider services) { ServiceProvider = services; }
public CoreXTDemoReadonlyContext(DbContextOptions <CoreXTDemoContext> options, ICoreXTServiceProvider services) : base(options, services) { }
public HomeController(ICoreXTServiceProvider sp) : base(sp) { _AppSettings = ServiceProvider.GetCoreXTDemoAppSettings(); }
// -------------------------------------------------------------------------------------------------------------------- /// <summary> Renders a bootstrap form group. </summary> /// <param name="services"> The services. </param> public Input(ICoreXTServiceProvider services) : base(services) { }
// -------------------------------------------------------------------------------------------------------------------- public Label(ICoreXTServiceProvider sp) : base(sp) { }
/// <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(); }