Dao interface for multiple databases?
up vote
1
down vote
favorite
There is a pattern of making a DAO interface
before DAO implementation
. I googled the advantages of this pattern and one striking point was to support multiple databases.
Now, what i could understand is that multiple databases here means different database engines rather than multiple datasources. Obviously multiple datasources should not have affect on how DAO implementations are made out of DAO interface.
My question is what can be the situations where we may need to support multiple database engines catering the same data? Also if such need arises, how will the REST endpoints
be managed to support different databases?
Will they be like for e.g. /db1/courses/
, /db2/courses
? Do correct me if i have made any wrong assumption or statement in this question.
java spring rest software-design
|
show 3 more comments
up vote
1
down vote
favorite
There is a pattern of making a DAO interface
before DAO implementation
. I googled the advantages of this pattern and one striking point was to support multiple databases.
Now, what i could understand is that multiple databases here means different database engines rather than multiple datasources. Obviously multiple datasources should not have affect on how DAO implementations are made out of DAO interface.
My question is what can be the situations where we may need to support multiple database engines catering the same data? Also if such need arises, how will the REST endpoints
be managed to support different databases?
Will they be like for e.g. /db1/courses/
, /db2/courses
? Do correct me if i have made any wrong assumption or statement in this question.
java spring rest software-design
1
REST has nothing to do with that. You don't design your REST API based on where the code reads its data. Having multiple DAO implementations can be useful if you build a product that must support several DB engines. That doesn't mean the application uses several DB engines at once. It means client1 uses Oracle and client2 uses PostgreSQL, for example.
– JB Nizet
Nov 10 at 16:39
Yes i want to know how to seperate those clients? My question for endpoints was inspired by the versioning of api where we may have different endpoints for different version.
– Navjot Singh
Nov 10 at 16:41
1
You sell your product to client1, and you sell it to client2, and they both configure and install your product, choosing one of the supported DB engines.
– JB Nizet
Nov 10 at 16:42
What if it is a web application and client doesn't install anything? How will we distinguish which client wants to hit which engine? @JBNizet
– Navjot Singh
Nov 10 at 16:48
You're asking for valid reasons to have multiple DAO implementations. The one I provided is the most common one. Just because you can have multiple implementations doesn't mean that you should. If you create a web app, and you're free to use the database you want, then use that one, and just create one implementation of each DAO.
– JB Nizet
Nov 10 at 16:51
|
show 3 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
There is a pattern of making a DAO interface
before DAO implementation
. I googled the advantages of this pattern and one striking point was to support multiple databases.
Now, what i could understand is that multiple databases here means different database engines rather than multiple datasources. Obviously multiple datasources should not have affect on how DAO implementations are made out of DAO interface.
My question is what can be the situations where we may need to support multiple database engines catering the same data? Also if such need arises, how will the REST endpoints
be managed to support different databases?
Will they be like for e.g. /db1/courses/
, /db2/courses
? Do correct me if i have made any wrong assumption or statement in this question.
java spring rest software-design
There is a pattern of making a DAO interface
before DAO implementation
. I googled the advantages of this pattern and one striking point was to support multiple databases.
Now, what i could understand is that multiple databases here means different database engines rather than multiple datasources. Obviously multiple datasources should not have affect on how DAO implementations are made out of DAO interface.
My question is what can be the situations where we may need to support multiple database engines catering the same data? Also if such need arises, how will the REST endpoints
be managed to support different databases?
Will they be like for e.g. /db1/courses/
, /db2/courses
? Do correct me if i have made any wrong assumption or statement in this question.
java spring rest software-design
java spring rest software-design
asked Nov 10 at 16:29
Navjot Singh
196
196
1
REST has nothing to do with that. You don't design your REST API based on where the code reads its data. Having multiple DAO implementations can be useful if you build a product that must support several DB engines. That doesn't mean the application uses several DB engines at once. It means client1 uses Oracle and client2 uses PostgreSQL, for example.
– JB Nizet
Nov 10 at 16:39
Yes i want to know how to seperate those clients? My question for endpoints was inspired by the versioning of api where we may have different endpoints for different version.
– Navjot Singh
Nov 10 at 16:41
1
You sell your product to client1, and you sell it to client2, and they both configure and install your product, choosing one of the supported DB engines.
– JB Nizet
Nov 10 at 16:42
What if it is a web application and client doesn't install anything? How will we distinguish which client wants to hit which engine? @JBNizet
– Navjot Singh
Nov 10 at 16:48
You're asking for valid reasons to have multiple DAO implementations. The one I provided is the most common one. Just because you can have multiple implementations doesn't mean that you should. If you create a web app, and you're free to use the database you want, then use that one, and just create one implementation of each DAO.
– JB Nizet
Nov 10 at 16:51
|
show 3 more comments
1
REST has nothing to do with that. You don't design your REST API based on where the code reads its data. Having multiple DAO implementations can be useful if you build a product that must support several DB engines. That doesn't mean the application uses several DB engines at once. It means client1 uses Oracle and client2 uses PostgreSQL, for example.
– JB Nizet
Nov 10 at 16:39
Yes i want to know how to seperate those clients? My question for endpoints was inspired by the versioning of api where we may have different endpoints for different version.
– Navjot Singh
Nov 10 at 16:41
1
You sell your product to client1, and you sell it to client2, and they both configure and install your product, choosing one of the supported DB engines.
– JB Nizet
Nov 10 at 16:42
What if it is a web application and client doesn't install anything? How will we distinguish which client wants to hit which engine? @JBNizet
– Navjot Singh
Nov 10 at 16:48
You're asking for valid reasons to have multiple DAO implementations. The one I provided is the most common one. Just because you can have multiple implementations doesn't mean that you should. If you create a web app, and you're free to use the database you want, then use that one, and just create one implementation of each DAO.
– JB Nizet
Nov 10 at 16:51
1
1
REST has nothing to do with that. You don't design your REST API based on where the code reads its data. Having multiple DAO implementations can be useful if you build a product that must support several DB engines. That doesn't mean the application uses several DB engines at once. It means client1 uses Oracle and client2 uses PostgreSQL, for example.
– JB Nizet
Nov 10 at 16:39
REST has nothing to do with that. You don't design your REST API based on where the code reads its data. Having multiple DAO implementations can be useful if you build a product that must support several DB engines. That doesn't mean the application uses several DB engines at once. It means client1 uses Oracle and client2 uses PostgreSQL, for example.
– JB Nizet
Nov 10 at 16:39
Yes i want to know how to seperate those clients? My question for endpoints was inspired by the versioning of api where we may have different endpoints for different version.
– Navjot Singh
Nov 10 at 16:41
Yes i want to know how to seperate those clients? My question for endpoints was inspired by the versioning of api where we may have different endpoints for different version.
– Navjot Singh
Nov 10 at 16:41
1
1
You sell your product to client1, and you sell it to client2, and they both configure and install your product, choosing one of the supported DB engines.
– JB Nizet
Nov 10 at 16:42
You sell your product to client1, and you sell it to client2, and they both configure and install your product, choosing one of the supported DB engines.
– JB Nizet
Nov 10 at 16:42
What if it is a web application and client doesn't install anything? How will we distinguish which client wants to hit which engine? @JBNizet
– Navjot Singh
Nov 10 at 16:48
What if it is a web application and client doesn't install anything? How will we distinguish which client wants to hit which engine? @JBNizet
– Navjot Singh
Nov 10 at 16:48
You're asking for valid reasons to have multiple DAO implementations. The one I provided is the most common one. Just because you can have multiple implementations doesn't mean that you should. If you create a web app, and you're free to use the database you want, then use that one, and just create one implementation of each DAO.
– JB Nizet
Nov 10 at 16:51
You're asking for valid reasons to have multiple DAO implementations. The one I provided is the most common one. Just because you can have multiple implementations doesn't mean that you should. If you create a web app, and you're free to use the database you want, then use that one, and just create one implementation of each DAO.
– JB Nizet
Nov 10 at 16:51
|
show 3 more comments
3 Answers
3
active
oldest
votes
up vote
1
down vote
I came across this situation where I had to check two DBs and get the data. The other DB was a back up one.
So this was the flow.
RestController --> Service --> DBService
--> DB1Repository --> Connect to DB1
--> DB2Repository --> Connect to DB2
We can design as we want, all it matters at the end is that we follow SOLID principles.
Basically the high level components should not depend on the low level components, but both should depend on the abstractions.
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
add a comment |
up vote
0
down vote
I just wanted to add my answer to this about beginning Spring development. This is one of the things that initially will not make sense at first. You will end up asking yourself:
- There will be only 1 database, so this doesn't make sense why do it?
- Why would I define an interface when there will only ever be 1 implementation?
But really neither of these are really why you do this. But it is the convention and pattern and this style is just what people are use to and you will like it better overtime. There are some other reasons too:
- Spring Data - this is an alternative to using an entity manager, whereby you only define interfaces and Spring will actually create beans which implement your repository functionality for you.
- Design - ensuring you define an interface will help keep your repository a repository.
- Easier Mocking - although arguably you can still do this in Spring without needing to define an interface it is still a bit cleaner when you want to replace the implementation with another.
But really it is just the Spring way, people will find it easier to understand your code if you do this.
I totally agree.
– Navjot Singh
Nov 12 at 11:54
add a comment |
up vote
0
down vote
Ill pop in here to describe a real world example.
We recently wanted to change out a large production database (Oracle) to a different one (SQL Server).
For different areas of the database, we had different DAO interfaces and implementations. For example, CustomerDAO, AccountsDAO, etc.
For each interace (like CustomerDAO) we had an implementation (CustomerDAOImplOracle).
It was relatively straight forward for us to write SQL Server versions of the DAO's (the SQL syntax and jdbc libraries were of course different) and swap them over with minimal changes to our business logic (the services which use the DAO's).
So, CustomerDAOImplOracle was reimplemented at CustomerDAOImplSQLServer. And so on...
What we learn:
- Interfaces provide good abstractiuon and allow for multiple implementations
- The DAO layer allows us to "switch out" the database (or its client libraries) if necessary
- Hiding implementation details of the database from the business logic reduces coupling and complexity
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
I came across this situation where I had to check two DBs and get the data. The other DB was a back up one.
So this was the flow.
RestController --> Service --> DBService
--> DB1Repository --> Connect to DB1
--> DB2Repository --> Connect to DB2
We can design as we want, all it matters at the end is that we follow SOLID principles.
Basically the high level components should not depend on the low level components, but both should depend on the abstractions.
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
add a comment |
up vote
1
down vote
I came across this situation where I had to check two DBs and get the data. The other DB was a back up one.
So this was the flow.
RestController --> Service --> DBService
--> DB1Repository --> Connect to DB1
--> DB2Repository --> Connect to DB2
We can design as we want, all it matters at the end is that we follow SOLID principles.
Basically the high level components should not depend on the low level components, but both should depend on the abstractions.
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
add a comment |
up vote
1
down vote
up vote
1
down vote
I came across this situation where I had to check two DBs and get the data. The other DB was a back up one.
So this was the flow.
RestController --> Service --> DBService
--> DB1Repository --> Connect to DB1
--> DB2Repository --> Connect to DB2
We can design as we want, all it matters at the end is that we follow SOLID principles.
Basically the high level components should not depend on the low level components, but both should depend on the abstractions.
I came across this situation where I had to check two DBs and get the data. The other DB was a back up one.
So this was the flow.
RestController --> Service --> DBService
--> DB1Repository --> Connect to DB1
--> DB2Repository --> Connect to DB2
We can design as we want, all it matters at the end is that we follow SOLID principles.
Basically the high level components should not depend on the low level components, but both should depend on the abstractions.
edited Nov 10 at 17:35
answered Nov 10 at 17:23
janardhan sharma
2537
2537
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
add a comment |
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
But the application at the end used only one database to respond to client? Am i right?
– Navjot Singh
Nov 10 at 17:30
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
No, it is using both, it checks DB1 if there are values as per query, if the result set is empty, it goes to DB2. As I said, it uses DB2 as fallback.
– janardhan sharma
Nov 10 at 17:34
add a comment |
up vote
0
down vote
I just wanted to add my answer to this about beginning Spring development. This is one of the things that initially will not make sense at first. You will end up asking yourself:
- There will be only 1 database, so this doesn't make sense why do it?
- Why would I define an interface when there will only ever be 1 implementation?
But really neither of these are really why you do this. But it is the convention and pattern and this style is just what people are use to and you will like it better overtime. There are some other reasons too:
- Spring Data - this is an alternative to using an entity manager, whereby you only define interfaces and Spring will actually create beans which implement your repository functionality for you.
- Design - ensuring you define an interface will help keep your repository a repository.
- Easier Mocking - although arguably you can still do this in Spring without needing to define an interface it is still a bit cleaner when you want to replace the implementation with another.
But really it is just the Spring way, people will find it easier to understand your code if you do this.
I totally agree.
– Navjot Singh
Nov 12 at 11:54
add a comment |
up vote
0
down vote
I just wanted to add my answer to this about beginning Spring development. This is one of the things that initially will not make sense at first. You will end up asking yourself:
- There will be only 1 database, so this doesn't make sense why do it?
- Why would I define an interface when there will only ever be 1 implementation?
But really neither of these are really why you do this. But it is the convention and pattern and this style is just what people are use to and you will like it better overtime. There are some other reasons too:
- Spring Data - this is an alternative to using an entity manager, whereby you only define interfaces and Spring will actually create beans which implement your repository functionality for you.
- Design - ensuring you define an interface will help keep your repository a repository.
- Easier Mocking - although arguably you can still do this in Spring without needing to define an interface it is still a bit cleaner when you want to replace the implementation with another.
But really it is just the Spring way, people will find it easier to understand your code if you do this.
I totally agree.
– Navjot Singh
Nov 12 at 11:54
add a comment |
up vote
0
down vote
up vote
0
down vote
I just wanted to add my answer to this about beginning Spring development. This is one of the things that initially will not make sense at first. You will end up asking yourself:
- There will be only 1 database, so this doesn't make sense why do it?
- Why would I define an interface when there will only ever be 1 implementation?
But really neither of these are really why you do this. But it is the convention and pattern and this style is just what people are use to and you will like it better overtime. There are some other reasons too:
- Spring Data - this is an alternative to using an entity manager, whereby you only define interfaces and Spring will actually create beans which implement your repository functionality for you.
- Design - ensuring you define an interface will help keep your repository a repository.
- Easier Mocking - although arguably you can still do this in Spring without needing to define an interface it is still a bit cleaner when you want to replace the implementation with another.
But really it is just the Spring way, people will find it easier to understand your code if you do this.
I just wanted to add my answer to this about beginning Spring development. This is one of the things that initially will not make sense at first. You will end up asking yourself:
- There will be only 1 database, so this doesn't make sense why do it?
- Why would I define an interface when there will only ever be 1 implementation?
But really neither of these are really why you do this. But it is the convention and pattern and this style is just what people are use to and you will like it better overtime. There are some other reasons too:
- Spring Data - this is an alternative to using an entity manager, whereby you only define interfaces and Spring will actually create beans which implement your repository functionality for you.
- Design - ensuring you define an interface will help keep your repository a repository.
- Easier Mocking - although arguably you can still do this in Spring without needing to define an interface it is still a bit cleaner when you want to replace the implementation with another.
But really it is just the Spring way, people will find it easier to understand your code if you do this.
answered Nov 12 at 9:34
Snickers3192
2,2541030
2,2541030
I totally agree.
– Navjot Singh
Nov 12 at 11:54
add a comment |
I totally agree.
– Navjot Singh
Nov 12 at 11:54
I totally agree.
– Navjot Singh
Nov 12 at 11:54
I totally agree.
– Navjot Singh
Nov 12 at 11:54
add a comment |
up vote
0
down vote
Ill pop in here to describe a real world example.
We recently wanted to change out a large production database (Oracle) to a different one (SQL Server).
For different areas of the database, we had different DAO interfaces and implementations. For example, CustomerDAO, AccountsDAO, etc.
For each interace (like CustomerDAO) we had an implementation (CustomerDAOImplOracle).
It was relatively straight forward for us to write SQL Server versions of the DAO's (the SQL syntax and jdbc libraries were of course different) and swap them over with minimal changes to our business logic (the services which use the DAO's).
So, CustomerDAOImplOracle was reimplemented at CustomerDAOImplSQLServer. And so on...
What we learn:
- Interfaces provide good abstractiuon and allow for multiple implementations
- The DAO layer allows us to "switch out" the database (or its client libraries) if necessary
- Hiding implementation details of the database from the business logic reduces coupling and complexity
add a comment |
up vote
0
down vote
Ill pop in here to describe a real world example.
We recently wanted to change out a large production database (Oracle) to a different one (SQL Server).
For different areas of the database, we had different DAO interfaces and implementations. For example, CustomerDAO, AccountsDAO, etc.
For each interace (like CustomerDAO) we had an implementation (CustomerDAOImplOracle).
It was relatively straight forward for us to write SQL Server versions of the DAO's (the SQL syntax and jdbc libraries were of course different) and swap them over with minimal changes to our business logic (the services which use the DAO's).
So, CustomerDAOImplOracle was reimplemented at CustomerDAOImplSQLServer. And so on...
What we learn:
- Interfaces provide good abstractiuon and allow for multiple implementations
- The DAO layer allows us to "switch out" the database (or its client libraries) if necessary
- Hiding implementation details of the database from the business logic reduces coupling and complexity
add a comment |
up vote
0
down vote
up vote
0
down vote
Ill pop in here to describe a real world example.
We recently wanted to change out a large production database (Oracle) to a different one (SQL Server).
For different areas of the database, we had different DAO interfaces and implementations. For example, CustomerDAO, AccountsDAO, etc.
For each interace (like CustomerDAO) we had an implementation (CustomerDAOImplOracle).
It was relatively straight forward for us to write SQL Server versions of the DAO's (the SQL syntax and jdbc libraries were of course different) and swap them over with minimal changes to our business logic (the services which use the DAO's).
So, CustomerDAOImplOracle was reimplemented at CustomerDAOImplSQLServer. And so on...
What we learn:
- Interfaces provide good abstractiuon and allow for multiple implementations
- The DAO layer allows us to "switch out" the database (or its client libraries) if necessary
- Hiding implementation details of the database from the business logic reduces coupling and complexity
Ill pop in here to describe a real world example.
We recently wanted to change out a large production database (Oracle) to a different one (SQL Server).
For different areas of the database, we had different DAO interfaces and implementations. For example, CustomerDAO, AccountsDAO, etc.
For each interace (like CustomerDAO) we had an implementation (CustomerDAOImplOracle).
It was relatively straight forward for us to write SQL Server versions of the DAO's (the SQL syntax and jdbc libraries were of course different) and swap them over with minimal changes to our business logic (the services which use the DAO's).
So, CustomerDAOImplOracle was reimplemented at CustomerDAOImplSQLServer. And so on...
What we learn:
- Interfaces provide good abstractiuon and allow for multiple implementations
- The DAO layer allows us to "switch out" the database (or its client libraries) if necessary
- Hiding implementation details of the database from the business logic reduces coupling and complexity
answered Nov 12 at 9:48
vikingsteve
25k1176112
25k1176112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240990%2fdao-interface-for-multiple-databases%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
REST has nothing to do with that. You don't design your REST API based on where the code reads its data. Having multiple DAO implementations can be useful if you build a product that must support several DB engines. That doesn't mean the application uses several DB engines at once. It means client1 uses Oracle and client2 uses PostgreSQL, for example.
– JB Nizet
Nov 10 at 16:39
Yes i want to know how to seperate those clients? My question for endpoints was inspired by the versioning of api where we may have different endpoints for different version.
– Navjot Singh
Nov 10 at 16:41
1
You sell your product to client1, and you sell it to client2, and they both configure and install your product, choosing one of the supported DB engines.
– JB Nizet
Nov 10 at 16:42
What if it is a web application and client doesn't install anything? How will we distinguish which client wants to hit which engine? @JBNizet
– Navjot Singh
Nov 10 at 16:48
You're asking for valid reasons to have multiple DAO implementations. The one I provided is the most common one. Just because you can have multiple implementations doesn't mean that you should. If you create a web app, and you're free to use the database you want, then use that one, and just create one implementation of each DAO.
– JB Nizet
Nov 10 at 16:51