Answer the question
In order to leave comments, you need to log in
How to organize a "repository" pattern with the ability to switch to different DBMS, namely in c#?
Hello.
I write in c#. But the language is not important for the most part.
I have a library in which I work with data from a database. In order not to write queries to the database for typical operations, I decided to organize access to the database through the "Repository" pattern, the main class of which can do CRUD (generalized class). I inherit this class when I need to create a specific repository. For example:
News Repository, Users Repository and so on. In fact, for each table I have my own repository that can CRUD and sometimes something specific, for example, for news there is a "FindByUrl" method and so on.
I am currently working with mysql. Therefore, in the project I have a Repositories / Mysql folder, in which all the repository files are located.
In the future, it is planned to switch simply by switching to other subds. I thought about how to implement this and for this I created my own interface for each repository. Now, for another subd, I just need to create the Repositories / OtherBD folder using the interfaces and create repositories in it.
To switch between subds, I decided to make a Factory. But on this and there was a stupor. And a number of questions arose, for which I ask for advice:
Answer the question
In order to leave comments, you need to log in
It turns out for each repository you need to write a getter / setter in the main repository? A lot and hard.
interface IMyEntityRepository {...}
class MySqlEntityRpository: IMyEntityRepository {...}
...
config.Bind<IMyEntityRepository>().ImplementsBy<MySqlEntityRpository>();
It was necessary to use ORM - this is already Generic Repository and Unit Of Work.
For example Entity Framework. Using EF Code First and different Entity Configurations (in general, one is enough), you can easily solve your problem of switching between databases by changing the connection string and DB provider.
And there will be no problems with JOIN.
Don't like EF - use Dapper.net, lightweight and fast ORM, whole stackoverflow runs on it
Containers are a very good thing, but in order to use them you need to know and apply agile development techniques. Just having a container doesn't rid your code of terrible dependencies. So read about dependency inversion first. There is even a book in Russian: "Dependency Inversion in .Net".
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question