Answer the question
In order to leave comments, you need to log in
How to implement a generic repository in c#?
I want to create a multilingual project where all the materials stored in the database will be multilingual. Everything will be stored in the database as follows:
In the project, I separate entities into ordinary, translatable, and translation entities.
Ordinary entities contain only Id.
Translated entities are descended from regular entities and in turn contain a collection of translations.
Translations contain the Id of the entity being translated and the Id of the language.
There is the following problem: Entity id can be either int or long. It turns out that because of this, it is necessary to make all entity classes generic.
How would you better implement repositories for said entities?
Answer the question
In order to leave comments, you need to log in
If I understand correctly, the error is that pi has an empty string. And the error is because it is not possible to convert an empty string to an integer value.
And it turns out this because "break" cancels the execution of the else block. That is, the first else block is executed if we do not get the value of pi in the loop, and by default pi = " ".
Why do you need generic entities? It is obvious to you what keys should be there. "generic" ends with the implementation of the base repository
class BaseRepository<TEntity, TKey>
where TEntity: class
{
...
public TEntity GetById(TKey id)
{
_context.Set<TEntity>().Find(id); // в случае EF
}
...
}
class TranslationRepository: BaseRepository<Translation, int>, ITranslationRepository
{ ... }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question