44// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
55
66using System ;
7+ using System . Collections . Generic ;
8+ using System . Linq ;
79using FileContextCore . Diagnostics ;
810using FileContextCore . Infrastructure . Internal ;
911using FileContextCore . Infrastructure ;
@@ -24,6 +26,30 @@ namespace FileContextCore
2426 /// </summary>
2527 public static class FileContextDbContextOptionsExtensions
2628 {
29+ /// <summary>
30+ /// Configures the context to connect to an in-memory database.
31+ /// The in-memory database is shared anywhere the same name is used, but only for a given
32+ /// service provider.
33+ /// </summary>
34+ /// <typeparam name="TContext"> The type of context being configured. </typeparam>
35+ /// <param name="optionsBuilder"> The builder being used to configure the context. </param>
36+ /// <param name="connectionString">A connection string that is used to build the options</param>
37+ /// <param name="databaseRoot">
38+ /// All in-memory databases will be rooted in this object, allowing the application
39+ /// to control their lifetime. This is useful when sometimes the context instance
40+ /// is created explicitly with <c>new</c> while at other times it is resolved using dependency injection.
41+ /// </param>
42+ /// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
43+ /// <returns> The options builder so that further configuration can be chained. </returns>
44+ public static DbContextOptionsBuilder < TContext > UseFileContextDatabaseConnectionString < TContext > (
45+ [ NotNull ] this DbContextOptionsBuilder < TContext > optionsBuilder ,
46+ string connectionString ,
47+ [ CanBeNull ] FileContextDatabaseRoot databaseRoot = null ,
48+ [ CanBeNull ] Action < FileContextDbContextOptionsBuilder > inMemoryOptionsAction = null )
49+ where TContext : DbContext
50+ => ( DbContextOptionsBuilder < TContext > ) UseFileContextDatabaseConnectionString (
51+ ( DbContextOptionsBuilder ) optionsBuilder , connectionString , databaseRoot , inMemoryOptionsAction ) ;
52+
2753 /// <summary>
2854 /// Configures the context to connect to an in-memory database.
2955 /// The in-memory database is shared anywhere the same name is used, but only for a given
@@ -63,21 +89,53 @@ public static DbContextOptionsBuilder<TContext> UseFileContextDatabase<TContext>
6389 /// service provider.
6490 /// </summary>
6591 /// <param name="optionsBuilder"> The builder being used to configure the context. </param>
66- /// <param name="databaseName">
67- /// The name of the in-memory database. This allows the scope of the in-memory database to be controlled
68- /// independently of the context. The in-memory database is shared anywhere the same name is used.
69- /// </param>
70- /// <param name="location">An optional parameter to define the location where the files are stored</param>
92+ /// <param name="connectionString">A connection string that is used to build the options</param>
7193 /// <param name="databaseRoot">
7294 /// All in-memory databases will be rooted in this object, allowing the application
7395 /// to control their lifetime. This is useful when sometimes the context instance
7496 /// is created explicitly with <c>new</c> while at other times it is resolved using dependency injection.
7597 /// </param>
7698 /// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
77- /// <param name="serializer">The serializer to be used. Defaults to json</param>
78- /// <param name="filemanager">The file manager to be used.</param>
7999 /// <returns> The options builder so that further configuration can be chained. </returns>
80- public static DbContextOptionsBuilder UseFileContextDatabase (
100+ public static DbContextOptionsBuilder UseFileContextDatabaseConnectionString (
101+ [ NotNull ] this DbContextOptionsBuilder optionsBuilder ,
102+ string connectionString ,
103+ [ CanBeNull ] FileContextDatabaseRoot databaseRoot = null ,
104+ [ CanBeNull ] Action < FileContextDbContextOptionsBuilder > inMemoryOptionsAction = null )
105+ {
106+ string [ ] connectionStringParts = connectionString . Split ( ';' ) ;
107+ Dictionary < string , string > connectionStringSplitted = connectionStringParts
108+ . Select ( segment => segment . Split ( '=' ) )
109+ . ToDictionary ( parts => parts [ 0 ] . Trim ( ) . ToLowerInvariant ( ) , parts => parts [ 1 ] . Trim ( ) ) ;
110+
111+ return UseFileContextDatabase ( optionsBuilder ,
112+ connectionStringSplitted . GetValueOrDefault ( "serializer" ) ,
113+ connectionStringSplitted . GetValueOrDefault ( "filemanager" ) ,
114+ connectionStringSplitted . GetValueOrDefault ( "databasename" ) ,
115+ connectionStringSplitted . GetValueOrDefault ( "location" ) , databaseRoot , inMemoryOptionsAction ) ;
116+ }
117+
118+ /// <summary>
119+ /// Configures the context to connect to a named in-memory database.
120+ /// The in-memory database is shared anywhere the same name is used, but only for a given
121+ /// service provider.
122+ /// </summary>
123+ /// <param name="optionsBuilder"> The builder being used to configure the context. </param>
124+ /// <param name="databaseName">
125+ /// The name of the in-memory database. This allows the scope of the in-memory database to be controlled
126+ /// independently of the context. The in-memory database is shared anywhere the same name is used.
127+ /// </param>
128+ /// <param name="location">An optional parameter to define the location where the files are stored</param>
129+ /// <param name="databaseRoot">
130+ /// All in-memory databases will be rooted in this object, allowing the application
131+ /// to control their lifetime. This is useful when sometimes the context instance
132+ /// is created explicitly with <c>new</c> while at other times it is resolved using dependency injection.
133+ /// </param>
134+ /// <param name="inMemoryOptionsAction">An optional action to allow additional in-memory specific configuration.</param>
135+ /// <param name="serializer">The serializer to be used. Defaults to json</param>
136+ /// <param name="filemanager">The file manager to be used.</param>
137+ /// <returns> The options builder so that further configuration can be chained. </returns>
138+ public static DbContextOptionsBuilder UseFileContextDatabase (
81139 [ NotNull ] this DbContextOptionsBuilder optionsBuilder ,
82140 string serializer = "json" ,
83141 string filemanager = "default" ,
@@ -91,8 +149,7 @@ public static DbContextOptionsBuilder UseFileContextDatabase(
91149 var extension = optionsBuilder . Options . FindExtension < FileContextOptionsExtension > ( )
92150 ?? new FileContextOptionsExtension ( ) ;
93151
94- extension = extension . WithStoreName ( databaseName ) ;
95- extension = extension . WithCustomOptions ( serializer , filemanager , location ) ;
152+ extension = extension . WithCustomOptions ( databaseName , serializer , filemanager , location ) ;
96153
97154 if ( databaseRoot != null )
98155 {
0 commit comments