code_junkie

Method return an interface

Nov 14th, 2011
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. IDataReader myReader = questDatabase.ExecuteReader(getQuest);
  2.  
  3. IDbCommand command = GiveMeSomeCommand();
  4. IDataReader r = command.ExecuteReader();
  5.  
  6. private static IDbCommand GiveMeSomeCommand()
  7. {
  8. return new OleDbCommand();
  9. }
  10.  
  11. private static IDbCommand GiveMeSomeCommand()
  12. {
  13. return new SqlCommand();
  14. }
  15.  
  16. DbProviderFactory factory = GiveMeSomeFactory();
  17. IDbCommand command = factory.CreateCommand();
  18. IDataReader r = command.ExecuteReader();
  19.  
  20. //and create more objects
  21. IDataAdapter adapter = factory.CreateDataAdapter();
  22. IDbConnection conn = factory.CreateConnection();
  23.  
  24. private DbProviderFactory GiveMeSomeFactory()
  25. {
  26. if(something)
  27. return SqlClientFactory.Instance;
  28. else if(somethingElse)
  29. return OracleFactory.Instance;
  30. else if(notThisAndNotThat)
  31. return MySqlFactory.Instance;
  32. else
  33. return WhateverFactory.Instance;
  34.  
  35. }
Add Comment
Please, Sign In to add comment