here is the end goal: get a new connection from a factory by name like so factory.CreateConnection<SqlConnection>("my db")
Here is my code so far:
type IConnectionFactory =
abstract member CreateConnection : unit -> IDbConnection
type DefaultConnectionFactory<'t when 't : (new : unit -> 't) and 't :> IDbConnection> () =
member this.CreateConnection () = new 't()
interface IConnectionFactory with
member this.CreateConnection () = this.CreateConnection() :> IDbConnection
type DefaultConnectionFactoryFactory() =
member this.CreateConnectionFactory connectionType =
if not (typeof<IDbConnection>.IsAssignableFrom connectionType)
then invalidArg "connectionType" (sprintf "type %O is not assignable to IDbConnection" connectionType)
let genericFactoryType = typedefof<DefaultConnectionFactory<_>>
let factorytype = genericFactoryType.MakeGenericType([| connectionType |])
Activator.CreateInstance(factorytype) :?> IConnectionFactory
type ConnectionFactory (connectionStrings : Dictionary<string, (IConnectionFactory * string)>) =
let ConnectionStrings = connectionStrings
member this.CreateConnection connectionStringName : 't :> IDbConnection =
if ConnectionStrings.ContainsKey connectionStringName
then
let connectionFactory, connectionString = ConnectionStrings.Item connectionStringName
let connection = connectionFactory.CreateConnection ()
connection.ConnectionString <- connectionString
connection :?> 't
else invalidArg "dataSourceName" (sprintf "no data source found named %s" connectionStringName)
on this line :
let genericFactoryType = typedefof<DefaultConnectionFactory<_>>
I am getting this error: Type constraint mismatch when applying the default type 'IDbConnection' for a type inference variable. A generic construct requires that the type 'IDbConnection' have a public default constructor Consider adding further type constraints
I'm not sure how to resolve it... Any other suggestions are welcome as well
ConnectionFactoryFactory
?? ...how meta. – Daniel