Click here to Skip to main content
15,881,882 members
Home / Discussions / C#
   

C#

 
AnswerRe: Why Interface? Pin
OriginalGriff18-Oct-16 4:03
mveOriginalGriff18-Oct-16 4:03 
SuggestionRe: Why Interface? Pin
Richard Deeming18-Oct-16 4:30
mveRichard Deeming18-Oct-16 4:30 
GeneralRe: Why Interface? Pin
OriginalGriff18-Oct-16 4:41
mveOriginalGriff18-Oct-16 4:41 
AnswerRe: Why Interface? Pin
Gerry Schmitz18-Oct-16 6:21
mveGerry Schmitz18-Oct-16 6:21 
JokeRe: Why Interface? Pin
Pete O'Hanlon18-Oct-16 9:20
mvePete O'Hanlon18-Oct-16 9:20 
GeneralRe: Why Interface? Pin
Gerry Schmitz18-Oct-16 20:36
mveGerry Schmitz18-Oct-16 20:36 
AnswerRe: Why Interface? Pin
Eddy Vluggen18-Oct-16 11:17
professionalEddy Vluggen18-Oct-16 11:17 
SuggestionRe: Why Interface? Pin
Richard Deeming18-Oct-16 12:21
mveRichard Deeming18-Oct-16 12:21 
Unfortunately, not the best example. Smile | :)

You could also swap out the concrete classes for their abstract base types (in System.Data.Common), which gives you many of the same benefits. The abstract base classes also include the task-based async methods, which are missing from the interfaces.
C#
using (DbConnection con = new SqlConnection("..."))
using (DbCommand cmd = con.CreateCommand())
{
    cmd.CommandText = "SELECT 42 As TheAnswer";
    
    await con.OpenAsync();
    int result = (int)(await cmd.ExecuteScalarAsync());
}

Also, given the huge difference in the syntax used to access different databases / providers, you're unlikely to be able to reuse your database code anyway.

If your data access code is scattered around your application, even if you've used the interfaces or base classes, allowing the user to switch databases by editing a file would potentially be a nightmare. They would need to be able to change the CommandText and CommandType of every query, and define a map from your internal parameter names to the query parameters - possibly including duplicate mappings if the provider doesn't support named parameters.

If it's a requirement, it would probably be simpler to create a separate data access assembly for each supported provider, and use some form of DI to let the user swap providers as required. At which point, you do need an interface for the data access classes. Smile | :)



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: Why Interface? Pin
Eddy Vluggen18-Oct-16 12:36
professionalEddy Vluggen18-Oct-16 12:36 
QuestionCan't insert inkPicture into SQL Server database from C# Pin
Member 1178659318-Oct-16 0:17
Member 1178659318-Oct-16 0:17 
AnswerRe: Can't insert inkPicture into SQL Server database from C# Pin
OriginalGriff18-Oct-16 0:22
mveOriginalGriff18-Oct-16 0:22 
AnswerRe: Can't insert inkPicture into SQL Server database from C# Pin
Richard Deeming18-Oct-16 1:49
mveRichard Deeming18-Oct-16 1:49 
GeneralRe: Can't insert inkPicture into SQL Server database from C# Pin
Member 1178659318-Oct-16 18:33
Member 1178659318-Oct-16 18:33 
GeneralRe: Can't insert inkPicture into SQL Server database from C# Pin
Member 1178659318-Oct-16 19:05
Member 1178659318-Oct-16 19:05 
GeneralRe: Can't insert inkPicture into SQL Server database from C# Pin
Richard Deeming19-Oct-16 2:07
mveRichard Deeming19-Oct-16 2:07 
QuestionRe: Can't insert inkPicture into SQL Server database from C# Pin
Eddy Vluggen19-Oct-16 3:35
professionalEddy Vluggen19-Oct-16 3:35 
QuestionConvert Char[ ] To Char ? [ ] Pin
MadDashCoder16-Oct-16 18:15
MadDashCoder16-Oct-16 18:15 
AnswerRe: Convert Char[ ] To Char ? [ ] Pin
Midi_Mick16-Oct-16 19:30
professionalMidi_Mick16-Oct-16 19:30 
AnswerRe: Convert Char[ ] To Char ? [ ] Pin
Nelson Costa Inácio18-Oct-16 4:44
Nelson Costa Inácio18-Oct-16 4:44 
QuestionUpdateProgress no disappear when i have a AsyncFileUpload control inside updatepanel Pin
whitesoul0115-Oct-16 4:26
whitesoul0115-Oct-16 4:26 
AnswerRe: UpdateProgress no disappear when i have a AsyncFileUpload control inside updatepanel Pin
Midi_Mick15-Oct-16 7:43
professionalMidi_Mick15-Oct-16 7:43 
SuggestionRe: UpdateProgress no disappear when i have a AsyncFileUpload control inside updatepanel Pin
Richard Deeming17-Oct-16 1:17
mveRichard Deeming17-Oct-16 1:17 
QuestionPrint if checkbox ticked? Pin
Member 1062775714-Oct-16 10:45
Member 1062775714-Oct-16 10:45 
AnswerRe: Print if checkbox ticked? Pin
NotPolitcallyCorrect14-Oct-16 11:39
NotPolitcallyCorrect14-Oct-16 11:39 
AnswerRe: Print if checkbox ticked? Pin
Midi_Mick14-Oct-16 14:09
professionalMidi_Mick14-Oct-16 14:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.