Factory Method
Hi everyone,
My issue is that my application supports Oracle,SQL Server and Sybase database. The problem is that when i have to execute some database script (lets say 15 table creation queries) but the syntax varies with respect to user backhand DBMS.
They solution strick in my mind is to have an abstract class as parent and inherits "OracleChildClass","SQLChildClass" and "SysbaseChildClass" from the parent class.But in this case
1. I have to write Full queries in all 3 child.If all are different then its fine but in my case 2 to 3 queries may be exactly the same.so i have to write the queries un necessarily again.
2. How can I make sure that all child will have equal no. of queries. So that in future if someone else enhance the application he wont miss out the logic.
Please advice
Thanks and Regards,
SebastianPosted Feb 26, 2010, 7:27 PM
If it is the first my advice would be to have an abstract base class (DBParentClass) and implement all queries in ANSI SQL if possible and in the cases where you have to use another query you just override it.
Another way would be to use some kind of template pattern, i. e. your DBParentClass has a method public void CreateTable(string tablename, ......) that calls template methods that simply build the Query. The default implementation would lead to ANSI SQL but every derived class could override the template methods, like CreateForeignKey, CreatePrimaryKey etc.
MaverickPosted Feb 24, 2010, 3:56 PM
Thanks Sam,
May be I made the question bit complex by using "OracleChildClass", "SQLChildClass" and "SybaseChildClass" actually these are derived classes from common parent i.e. "DBParentClass".
Please read the above questions with these understand. I hope u get the understanding, otherwise I will explain it again.
Thanks a lot for ur sincere efford to help
Thanks and Regards,
Sam HobbsPosted Feb 24, 2010, 12:21 PM
I am not sure I understand, but I think you want to create an interface. Classes cannot derive from multiple classes. So then you can write derived classes that derive from either Oracle, SQL or Sysbase classes and the derived classes would also derive from your interface. You will want to use virtual functions in your interface.
Another option is to write a class that embeds a database class. The base class would just have stub implementations of virtual functions. Then in derived classes you would embed either Oracle, SQL or Sysbase classes and override the virtual functions as needed.
All this would be described in good books about C#.