Microsoft Developer Network >
Página principal de foros
>
Visual C# General
>
Interface casting best practice question
Interface casting best practice question
- I have implemented a class factory for classes representing rows from database tables. The class factory returns an interface to a class which could be either a SqlServer aware class, or an Oracle aware class,. Design pattern stuff. But I am not sure I did it right. Exemple with pseudo code:
interface ICar{Load();Save();Delete();}
class Car{MakeModelYear}class SqlServer.Car : Car, ICar{// Do stuff with Sqlserver SQL}class Oracle.Car : Car, ICar{// Do stuff with Oracle SQL}Then in the code I use the interface object, and I dont know if the data came form SqlServer or Oracle. However, I need to get to the base class to get its properties and then there are several options:1. Add a getter to the interface classes for each properties (dont like this, to much work)2. Cast the interface to its associated type ( second variable required, dont want to use the "as", make code difficult to read)3. Add a getter called "Base" to the interface which returns "this" in the associated class (which is what the debugger is doing)Which approach would be best? Also should my "Car" class be abstract instead?Thanks
Respuestas
- Hi,
Seems to me that you would want to abstract your database access and pass its interface into the constructor of your Car class.
Search for the Dependancy Injection design pattern.
HTH
Andy
www.sharpcontrols.net :: Free custom controls for .NET- Propuesto como respuestaLingzhi SunMSFT, Moderadormiércoles, 11 de noviembre de 2009 10:11
- Marcado como respuestaLingzhi SunMSFT, Moderadorjueves, 19 de noviembre de 2009 0:24
Todas las respuestas
- Hi,
Seems to me that you would want to abstract your database access and pass its interface into the constructor of your Car class.
Search for the Dependancy Injection design pattern.
HTH
Andy
www.sharpcontrols.net :: Free custom controls for .NET- Propuesto como respuestaLingzhi SunMSFT, Moderadormiércoles, 11 de noviembre de 2009 10:11
- Marcado como respuestaLingzhi SunMSFT, Moderadorjueves, 19 de noviembre de 2009 0:24
Hi Keboo,
How is the problem? I think Andy’s post is really helpful and the Dependency Injection design pattern is fit for your scenario.
Here is some additional references:
http://msdn.microsoft.com/en-us/magazine/cc163739.aspx
http://msdn.microsoft.com/en-us/library/cc707845.aspx
Have a nice weekend!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

