We have several (5+) applications that hit the same database and we’re running into issues where the applications all take it into their own hands to build queries that fetch the same data. However, since different developers are creating these queries they’re not identical as they should be, thus returning different results from time to time.
As far as I can see there are 2 possible solutions.
- Create a library that each application references to get the proper query
- Create a WCF service that encapsulate common queries used by each application and create a client for each application to use
I’m leaning towards option 2 since this would give us the flexibility to modify the filters we’re using in our queries, thus updating each application at once.
The service hierarchy would look something like
public class QueryService
{
private readonly IRepository _repository;
public QueryService(IRepository repository)
{
_repository = repository;
}
public IEnumerable<int> GetCommonOperation()
{
return _repository.GetCommonOperation();
}
public IEnumerable<AnotherDto> AnotherCommonOperation()
{
return _repository.AnotherCommonOperation();
}
}
Then create a client that would interact with QueryService.
So my question what is the general approach for creating querying services with the sole goal of encapsulate commonly used queries/operations for several applications?
You’ve already answered your own question. Essentially, you want to seperate the interface from the implementation, just as a web service (or WCF) would accomplish. If you create an assembly, you’ll still need to ensure that the assembly is deployed with your seperate clients, or at least in a location that is accessible by any application utilizing it. Seems like a lot more work. However, keep in mind, if you make changes to the service signatures, any client utilizing that service will also have to update to reflect the new definitions.
The great thing about a web service (WSDL) is that it’s generic. You can write the client’s in any language that can consume and parse WSDL — unlike an assembly, where you would be targeting a specific build or OS. So whether you have C#, Java, iOS, Android, etc. clients, they are all capible of utilizing your services.