This is a place where you will use _mySQLDriver to check, whether you are in a transaction This is a place where you will use _mySQLDriver to commit transaction This is a place where you will use _mySQLDriver to rollback transaction This is a place where you will use _mySQLDriver to begin transaction This is a place where you will use _mySQLDriver to handle the DbQuery Public DatabaseResult DoQuery(DbQuery query) Public MyMySQLDatabase(CSharpMySQLDriver mySQLDriver) Private readonly CSharpMySQLDriver _mySQLDriver So you need a class to implement this interface. Your (very simple example) database interface might look like this (the DatabaseResult or DbQuery classes respectively would be your own implementations representing database operations): public interface Databaseīecause this is an interface, it itself does not really do anything. Idatabase help code#Because both interfaces and abstract classes have the behaviour of you not being able to use them directly, but you have to either implement (in case of interface) or extend (in case of abstract class) them, the code itself already suggests, you will need to have specific implementations to fullfil the contract given by either the interface or the abstract class. You create an interface - or abstract class, if you are coding in a language like C++ - and add generic methods to this interface. (AKA the Hollywood principle).ĭecoupling an application using abstraction 1. You might also want to check the Adapter design pattern, which is basically what hiding implementation details behind interface's public methods means.ĭependency injection, coupled with Factory design pattern, is the foundation stone and an easy way to code the Strategy design pattern, which is a part of IoC principle.ĭon't call us, we will call you. Here I will try to thoroughly show you the benefit of using abstraction (using interfaces) to make your application less coupled and more versatile.īefore reading further, I recommend you to read and get a basic understanding of Dependency injection, if you do not know it yet. This question, especially in the database context, has been asked too many times. Those are two responsibilities and should not be put into a single class. His Foo class acts both as a database facade and factory. Throw new Exception("You forgot to configure the database!") Īs far as a better way of setting up the correct IDatabase implementation at run time in your application, you should look into things like " Factory Method", and " Dependancy Injection".Ĭaleb's answer, while he is on the right track, is actually wrong. If(/*some way to tell if should use MySql*/)Įlse if(/*some way to tell if should use MySql*/) What you want is multiple implementations for the interface that your application uses.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |