I'm trying to understand sharing interfaces between modules and I faced following problem: I have three projects in which:
- Console Application - is main project, which wants to fetch data.
- Core Library - is library, which decides which data service will be used based on shared interfaces.
- Data Library - is library, which provides necessary data
Basic concept is that when I would like to change between databases like MySQL and SQL Server I do not have change all base code but only would need to change one module which is Data Library. Thanks to Core Library other projects doesn't need to know what is happening in w Data Library, because Core Library is one which manages which class should be used.
Projects references look like:
ConsoleApplication --> CoreLibrary <-- DataLibrary
So I've created following projects to present the problem.
Console application
class Program
{
static void Main(string[] args)
{
var core = new Core();
var dataService = core.GetDataService();
}
}
Core Library
public class Core
{
public IDataService GetDataService()
{
return ???
}
}
public interface IDataService
{
public IUserData UserData { get; set; }
}
public interface IUserData
{
public string Name { get; set; }
public string LastName { get; set; }
}
Data Library
public class Data : IDataService
{
public IUserData UserData { get; set; }
}
public class UserData : IUserData
{
public string Name { get; set; }
public string LastName { get; set; }
}
In Core Library's method GetDataService I can't return Data's instance of object because it is not referenced to DataLibrary. If I reverse the reference between them i will not be able to implement interface from CoreLibrary. In the case I want to reference both to each other i get an error "A reference to 'DataLibrary' could not be added. Adding this project as a reference would cause a circular dependency".
What mistake do I do. Is it good approach but I'm missing something? Or maybe the whole thing is wrong and I should consider other solution?