Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What is the best way of dealing with multiple models. #15

Closed
OGKevin opened this issue Aug 5, 2019 · 3 comments
Closed

What is the best way of dealing with multiple models. #15

OGKevin opened this issue Aug 5, 2019 · 3 comments

Comments

@OGKevin
Copy link

OGKevin commented Aug 5, 2019

I was thinking of passing 1 object box instance down as dependency to methods. But i soon reliased that you need to create 1 objectbox per model. e.g. the following will not work:

	ob, err := objectbox.NewBuilder().
		Directory(dir).
		Model(package1.ObjectBoxModel()).
		Model(package2.ObjectBoxModel()).
		Build()
box1 := BoxForModel1(ob)
box2 := BoxForModel2(ob)

And this is because of

	builder.Error = model.validate()
	if builder.Error != nil {
		builder.model = nil
	} else {
		builder.model = model // this line
	}

So as i understand, i need to create 1 objectbox + 1 BxoForMODEL with one ObjectBox per model. Whith this being said, what is the recomanded way to deal with mutliple models? Instead of passing down from top of the stack just create these 2 instances every single time i need it ? I rather have 1 thing to pass down that has everything e.g. the sql driver (objecbox) which you create repositories (BoxForMODEL) with. 🤔

Also is it an idea to return an error when you try to regiester more then 1 model? Causes less confusion later when you get errors like

Could not create query - please check your query conditions: unordered_map::at: key not found

@OGKevin
Copy link
Author

OGKevin commented Aug 5, 2019

I think i might have solved my problem with https://godoc.org/go.uber.org/fx :)

@vaind
Copy link
Contributor

vaind commented Aug 6, 2019

See #14 (comment) for a potential answer to this question and let me know if that helps.

Thanks for the error suggestion, I'll have a look if we can do something about it.

@OGKevin
Copy link
Author

OGKevin commented Aug 8, 2019

I ended up declaring all my models in a models package. This seems to be the best solution.

@OGKevin OGKevin closed this as completed Aug 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants