473,395 Members | 1,458 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Abstract Factory and other patterns


Hi,

I am writing a data access layer that would allow connecting to various database systems, such as SqlServer, MySQL, Oracle, Ms Access, etc. Since I also would like to use providers specific to each database system's type, I created a main interface, called IConnectionFactory, with the signatures of the methods. I then implemented it in the different classes, such as MySqlConnectionFactory. However, I have the impression of duplicating the code in every specific provider's class to meet the provider's types. Graphically:

IConnectionFactory <- - - - - - - SqlServerConnectionFactory
IConnectionFactory <- - - - - - - MySqlConnectionFactory

Is there a better pattern to avoid code duplication (for methods like "Open", "Close", etc.)? What about "generics"? Any link or suggestion is appreciated.

Thanks a lot.
Mike



Aug 24 '06 #1
5 1605
I may be missing the point here, but couldn't you just create a parent
class and inherit it as opposed to using the interface. That way you
could create the generic methods yourself and have the children call
them as needed?
Mike wrote:
Hi,

I am writing a data access layer that would allow connecting to various database systems, such as SqlServer, MySQL, Oracle, Ms Access, etc. Since I also would like to use providers specific to each database system's type, I created a main interface, called IConnectionFactory, with the signatures of the methods. I then implemented it in the different classes, such as MySqlConnectionFactory. However, I have the impression of duplicating the code in every specific provider's class to meet the provider's types. Graphically:

IConnectionFactory <- - - - - - - SqlServerConnectionFactory
IConnectionFactory <- - - - - - - MySqlConnectionFactory

Is there a better pattern to avoid code duplication (for methods like "Open", "Close", etc.)? What about "generics"? Any link or suggestion is appreciated.

Thanks a lot.
Mike



------=_NextPart_000_011C_01C6C787.6FA1B9A0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 2029

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2963" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Hi,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I am writing a data access layer that would allow
connecting to various database systems, such as SqlServer, MySQL, Oracle, Ms
Access, etc. Since I also would like to use providers specific to each database
system's type, I created a main interface, called IConnectionFactory, with the
signatures of the methods. I then implemented it in the different classes, such
as MySqlConnectionFactory. However, I have the impression of duplicating the
code in every specific provider's class to meet the provider's types.
Graphically:<BR></FONT></DIV>
<DIV><FONT face=Arial size=2>IConnectionFactory &lt;- - - - - - -
SqlServerConnectionFactory</FONT></DIV>
<DIV><FONT face=Arial size=2>IConnectionFactory &lt;- - - - - - -
MySqlConnectionFactory</DIV></FONT>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Is there a better pattern to avoid code duplication
(for methods like "Open", "Close", etc.)? What about "generics"? Any link or
suggestion is appreciated.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Thanks a lot.</FONT></DIV>
<DIV><FONT face=Arial size=2>Mike</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_011C_01C6C787.6FA1B9A0--
Aug 24 '06 #2
Mike,

I am curious, have you checked out the Data Access Application Block (DAAB) in the Enterprise Application blocks from Microsoft?

http://msdn.microsoft.com/library/de...asp?frame=true

Here is the link for the main page:

http://msdn.microsoft.com/library/de...asp?frame=true

The data access application block enables you to some degree to abstract your data access to a great degree. It's a great help, especially if you are trying to save time in writing your own.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Mike" <no**@none.comwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...

Hi,

I am writing a data access layer that would allow connecting to various database systems, such as SqlServer, MySQL, Oracle, Ms Access, etc. Since I also would like to use providers specific to each database system's type, I created a main interface, called IConnectionFactory, with the signatures of the methods. I then implemented it in the different classes, such as MySqlConnectionFactory. However, I have the impression of duplicating the code in every specific provider's class to meet the provider's types. Graphically:

IConnectionFactory <- - - - - - - SqlServerConnectionFactory
IConnectionFactory <- - - - - - - MySqlConnectionFactory

Is there a better pattern to avoid code duplication (for methods like "Open", "Close", etc.)? What about "generics"? Any link or suggestion is appreciated.

Thanks a lot.
Mike



Aug 24 '06 #3

Why re invent an already created wheel?

http://www.gotdotnet.com/codegallery...2-91be63527327

http://www.gotdotnet.com/Community/U...1-3781DF71DCDD
But if you're bent on it, then I think the enterprise library is using a combination of
Factory
Template
(design patterns)

design patterns. and an item like Database is an abstract class.

"Mike" <no**@none.comwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...

Hi,

I am writing a data access layer that would allow connecting to various database systems, such as SqlServer, MySQL, Oracle, Ms Access, etc. Since I also would like to use providers specific to each database system's type, I created a main interface, called IConnectionFactory, with the signatures of the methods. I then implemented it in the different classes, such as MySqlConnectionFactory. However, I have the impression of duplicating the code in every specific provider's class to meet the provider's types. Graphically:

IConnectionFactory <- - - - - - - SqlServerConnectionFactory
IConnectionFactory <- - - - - - - MySqlConnectionFactory

Is there a better pattern to avoid code duplication (for methods like "Open", "Close", etc.)? What about "generics"? Any link or suggestion is appreciated.

Thanks a lot.
Mike



Aug 24 '06 #4
"Mike" <no**@none.coma écrit dans le message de news:
%2****************@TK2MSFTNGP06.phx.gbl...

I am writing a data access layer that would allow connecting to various
database systems, such as SqlServer, MySQL, Oracle, Ms Access, etc. Since I
also would like to use providers specific to each database system's type, I
created a main interface, called IConnectionFactory, with the signatures of
the methods. I then implemented it in the different classes, such as
MySqlConnectionFactory. However, I have the impression of duplicating the
code in every specific provider's class to meet the provider's types.
Graphically:

IConnectionFactory <- - - - - - - SqlServerConnectionFactory
IConnectionFactory <- - - - - - - MySqlConnectionFactory

Is there a better pattern to avoid code duplication (for methods like
"Open", "Close", etc.)? What about "generics"? Any link or suggestion is
appreciated.

Ys, If you want, you could change the interface to an abstract class which
implements any common code, leaving either abstract or virtual methods to be
overridden in derived classes where there are differences.

If you encounter a scenario where you know what steps have to be done, but
not how the steps will be done, then the Template Method or Strategy pattern
will help.

public abstract class ConnectionFactory
{
// override in derived class
public abstract void DoThing1();

// override in derived class
public abstract void DoThing2();

// sequence or "strategy"
public void DoThings()
{
DoThing1();
DoThing2();
...
}
}

Does this help ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Aug 24 '06 #5

Thanks Joanna. Yes, it helps!

I know there exist code like this, but I am doing it mostly to learn
patterns, and decided to use a common problem as a project.

Thanks.
Mike


"Joanna Carter [TeamB]" <jo****@not.for.spamwrote in message
news:uD**************@TK2MSFTNGP03.phx.gbl...
"Mike" <no**@none.coma écrit dans le message de news:
%2****************@TK2MSFTNGP06.phx.gbl...

I am writing a data access layer that would allow connecting to various
database systems, such as SqlServer, MySQL, Oracle, Ms Access, etc. Since
I
also would like to use providers specific to each database system's type,
I
created a main interface, called IConnectionFactory, with the signatures
of
the methods. I then implemented it in the different classes, such as
MySqlConnectionFactory. However, I have the impression of duplicating the
code in every specific provider's class to meet the provider's types.
Graphically:

IConnectionFactory <- - - - - - - SqlServerConnectionFactory
IConnectionFactory <- - - - - - - MySqlConnectionFactory

Is there a better pattern to avoid code duplication (for methods like
"Open", "Close", etc.)? What about "generics"? Any link or suggestion is
appreciated.

Ys, If you want, you could change the interface to an abstract class which
implements any common code, leaving either abstract or virtual methods to
be
overridden in derived classes where there are differences.

If you encounter a scenario where you know what steps have to be done, but
not how the steps will be done, then the Template Method or Strategy
pattern
will help.

public abstract class ConnectionFactory
{
// override in derived class
public abstract void DoThing1();

// override in derived class
public abstract void DoThing2();

// sequence or "strategy"
public void DoThings()
{
DoThing1();
DoThing2();
...
}
}

Does this help ?

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Aug 24 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Medi Montaseri | last post by:
Hi, Given a collection of similar but not exact entities (or products) Toyota, Ford, Buick, etc; I am contemplating using the Abstraction pattern to provide a common interface to these products....
4
by: max | last post by:
Hello, I analyze this design pattern for a long time but I do not understand how this pattern work and what the purpose is? (I looked a this site...
11
by: FluffyCat | last post by:
In Febraury - April of 2002 I put together in Java examples of all 23 of the classic "Gang Of Four" design patterns for my website. Partly I wanted to get a better understanding of those patterns....
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
16
by: RSH | last post by:
Hi, I am a fairly seasoned developer and have recently started looking at software design patterns. I am starting with the Abstract Factory Pattern and was wondering if someone could help me...
2
by: Duy Lam | last post by:
Hi everyone, Sorry, I don't know what group to post this problem, I think may be this group is suitable. I'm styduing DAO (Data Access Object) pattern in this link...
3
by: suresh_rai | last post by:
Hello I am hoping someone can give me very needed help. I am wanting some C+ + code for 'C++ Abstract Factory'. I need code to be free. I am been learning Design patterns. I found some code on...
4
by: C# | last post by:
http://www.questpond.com/FreeDesign1.htm I was going through the above videos. Can i conclude Abstract factory patterns are extensions of factory patterns. I have made the conclusion after...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.