Похожие чаты

Do you guys think packaging using structs is a bad

practice?

for example you have a models package, and inside you have these structs with methods:

User{}.FindAll()
Book{}.FindAll()
...

The other way to do this is to have each of these structs as a separate package inside models package. So:

models/usermodel -> FindAll()
models/bookmodel -> FindAll()

5 ответов

6 просмотров

why not UserFindAll() ?

ㅤ- Автор вопроса
Roman Sharkov
why not UserFindAll() ?

The reason I'm attached to the first approach is that you can define relationships on your structs. So for example, user, err := models.User{}.FindById(1) if err != nil { return } books, err := user.Books() if err != nil { return } So on User{} we have these methods that use the current user's id to do a relational query. func (u *User) Books() (books []Book, err error) { // SELECT * FROM user_books WHERE user_id = u.id <--- }

ㅤ- Автор вопроса

The reason I'm attached to the first approach is t...

Oh and one thing else, if we use different packaging there will be import cycles


The reason I'm attached to the first approach is t...

I recently use the services package where every service is a subpackage all services have methods like: services.users.Create services.users.GetByID services.users.Restrict services.books.Create services.books.Archive services.books.FindByUserID services.booksearch.Find I try to segregate responsibilities as much as possible every service has Writer and Reader interfaces: package users type Writer interface{ Create(ctx context.Context, id, name string) error Restrict(ctx context.Context, id string, p Permissions) error } type Reader interface { GetByID(context.Context, id string) (User, error) } type ReadWriter interface { Reader Writer } func New(rw ReadWriter) *Service {/*...*/} this way the implementation of a service is abstract and I can thus move it out of the monolith to a separate microservice or mock it for testing. There is also a clear separation of concerns, like for example: The users service keeps track of profiles and permissions, the books service keeps track of books, the booksearch service provides extensive search functionality (uses something like Elastic). Then there's also the gateway service, which is a GraphQL/REST API talking to other services, it's the only publicly available service exposed to the internet as an ingress point. You can have those services in one single executable btw, no need to have actual microservices complicating your deployment. But it's going to be easy to split things off to separate microservices later on.

ㅤ- Автор вопроса
Roman Sharkov
I recently use the services package where every se...

That's an interesting way to do it, I'm gonna try a project from scratch with this concept. Thank you

Похожие вопросы

Обсуждают сегодня

Скажите, можно ли как-то "переместить" динамический массив из одной переменной в другую? Скажем, переместить из TList<> в TArray<>. Именно переместить, а не скопировать. Если ...
Eugene Krasnikov (ᴊɪɴ x)
36
Hi everyone! I have a two-folded message to share today. First: updates and news, as we are being asked for those. We still want to go like we did the last 6 years: we w...
Marten | Unibright.io
31
комрады, че-та лыжы не едут var tmpFont: TFont; begin tmpFont:= TFont.Create; try case rgFontColor.ItemIndex of 0: tmpFont.Color:= clWindowText; 1: tmpFo...
Ed Doc
34
VIP-310 Set RedStone as the MAIN oracle for BTC and BNB on BNB Chain Summary If passed, this VIP will perform the following actions: - Configure RedStone as the MAIN oracle...
Venus Announcements
1
М-да. Почему бы просто со stringlist не работать?
Michael Longneck
18
Интересно, нет ли какого-то способа получить из dll не адрес самой метки, а адрес со смещением?
The Bird of Hermes
54
monero is the only tool for privacy and currency . why would we destroy it?
😎
13
🔍 Launch of Sentinel dVPN's Native Explorer 🎉 The Sentinel Growth DAO is thrilled to announce the launch of Sentinel dVPN's new native blockchain explorer! Now anyone can div...
Joey | BadgerBite Joeyy
1
Можно вообще написать: Person fName' lName' age'. Тогда действительно имена полей потребуются лишь в строковом виде, чтобы эти fName' и т.д. достать :-) Но разве для этого нуж...
Михаил
8
Whoa, did you guys see this FUNToken Seed Grant announcement? 💸
Noah Noure
37
Карта сайта