Avoid App.config file in project when using with SQLite/SubSonic (or any other database)
I have a project which I am developing with SQLite and SubSonic 3. I want to know that how can I avoid the App.config file altogether. How do I tell my program to use SQLite Data provider. I mean, how can I tell it this:
<DbProviderFactories>
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite">
</DbProviderFactories>
also can I add a connection string by code:
<add name="xyz" connectionString="Data Source=xyz.db" providerNam开发者_StackOverflow社区e="System.Data.SQLite"/>
You can create a Provider manually by passing the connection string in:
var p = ProviderFactory.GetProvider("connectionstring", "System.Data.SqlClient");
Then you can use that Provider when you need to. This is ideal for IoC where you can setup a rule for SubSonic's IDataProvider:
ForRequestedType< IDataProvider >()
.TheDefault.Is.ConstructedBy(x =>
ProviderFactory.GetProvider("connectionstring", "System.Data.SqlClient"));
Gotta love IoC :)
A lot of libraries rely on the app.config, so you sooner or later are going to need it anyway. It is very central to the whole .NET platform. And what do you see as the problem?
You can easily provide your own connection string but I'm not so sure about loading a specific ADO.NET provider in code.
It depends on how you use your SQLite provider, but if it's classes are hardcoded into your code then I don't think you even need a DbProviderFactory.
精彩评论