C
C
csharpnoob2017-07-03 15:06:31
Python
csharpnoob, 2017-07-03 15:06:31

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:
644d384c9e3942808ab94ead737255c4.jpg
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

3 answer(s)
D
Drill, 2018-12-12
@Drill

s1 = her.readline().strip()

K
Kenit, 2018-12-13
@Kenit

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 = " ".

F
Fat Lorrie, 2017-07-03
@Free_ze

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 
{ ... }

ZY In fact, you should have 2 entities:
Each has its own repository, and above them a common localization service.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question