Abstract Factory and other patterns

M

Mike

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
 
M

Matt Bailey

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?
 
N

Nicholas Paldino [.NET/C# MVP]

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/d...tLibJan2006_DataAccessAppBlock.asp?frame=true

Here is the link for the main page:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag2/html/EntLib2.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]
- (e-mail address removed)

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
 
S

sloan

Why re invent an already created wheel?

http://www.gotdotnet.com/codegallery/codegallery.aspx?id=295a464a-6072-4e25-94e2-91be63527327

http://www.gotdotnet.com/Community/...mpleGuid=137F13F8-AA21-4042-BAB1-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.






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
 
J

Joanna Carter [TeamB]

"Mike" <[email protected]> a écrit dans le message de %[email protected]...

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
 
M

Mike

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top