<@U01SP2GJYAU> I'm trying to make a base repositor...
# komapper
d
@Toshihiro Nakamura I'm trying to make a base repository without exposing Komapper to my domain layer:
Copy code
class KomapperBaseRepository<ENTITY : Any, ID : Any, META : EntityMetamodel<ENTITY, ID, META>>(
    private val db: R2dbcDatabase,
    private val entityMetamodel: META,
) : RepositoryBase<ENTITY, ID> {
    override suspend fun create(entity: ENTITY): ENTITY = db.runQuery(QueryDsl.insert(entityMetamodel).single(entity))

    override suspend fun get(id: ID): ENTITY? {
        return db.runQuery(QueryDsl.from(entityMetamodel).where { entityMetamodel...? eq id }.singleOrNull())
    }
}

// on my domain layer:
interface RepositoryBase<T : Any, ID : Any> {
    suspend fun create(entity: T): T

    suspend fun get(id: ID): T?
}
but I can't find what to put in my where as the id property...
I had to add the property here:
Copy code
class KomapperBaseRepository<ENTITY : Any, ID : Any, META : EntityMetamodel<ENTITY, ID, META>>(
    private val db: R2dbcDatabase,
    private val entityMetamodel: META,
    private val idProperty: PropertyMetamodel<ENTITY, ID, *>
) : RepositoryBase<ENTITY, ID> {
    override suspend fun create(entity: ENTITY): ENTITY = db.runQuery(QueryDsl.insert(entityMetamodel).single(entity))

    override suspend fun get(id: ID): ENTITY? {
        return db.runQuery(QueryDsl.from(entityMetamodel).where {idProperty eq id }.singleOrNull())
    }
}
but maybe there's another way?
t
I do not recommend such standardization because there are very few parts that can be standardized and it is not worth it.
d
So my implementation where I pass the idProperty is still ok?
You mean in the case where there's composite keys, then it would get more complex?
t
So my implementation where I pass the idProperty is still ok?
Yes, It works fine if you don’t use composite keys.
👍🏼 1