Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Question about the repository and specification design #71

Closed
wyshmily opened this issue Nov 20, 2017 · 6 comments
Closed

Question about the repository and specification design #71

wyshmily opened this issue Nov 20, 2017 · 6 comments

Comments

@wyshmily
Copy link

The repository and specification design looks good, but when I use it in my work, I found it hard to handle when the logic is a little more complicate.
For example, when my api accepts a new Student object For create, I have to check if the student'scardnumber already exists, do i define a specification to do this check? It's annoying to do a lot of such works.

@wyshmily
Copy link
Author

And I don't think such specifications are needed by the applicationcore.

Want some guide, thanks!

@ardalis
Copy link
Collaborator

ardalis commented Nov 20, 2017

Specification is meant to encapsulate complex queries. If you just want to know if a record exists, you can do a GetById() and see that it returns null - no Spec needed.

@wyshmily
Copy link
Author

but in my case, the query is not by id, but other field like studentnumber

@ardalis
Copy link
Collaborator

ardalis commented Nov 20, 2017

In that case if you're already using specs in your design, it would be easy to create a spec for StudentByStudentNumber(int studentNumber) that would include the required lambda expression to fetch student(s) with that number.

Alternately unless you expect it to be a regular occurrence for student creation to result in potential duplicates, you could just attempt the create and catch the exception when the record already exists. If it truly is an exceptional condition, this would be an appropriate design as well.

@wyshmily
Copy link
Author

In fact it would not cause an exception, because I use soft delete, with a IsDeleted field in each table.
Such checks can only be performed manually since there is no unique index.
So the answer is creating specs to do the job?

@ardalis
Copy link
Collaborator

ardalis commented Nov 22, 2017

If you're already set up to use Specifications (that is, your Repository can accept an ISpecification<T> or similar) then yes, a spec would do the job easily. If you're not using the Specification pattern or don't want to use it for this case, the simplest approach would be to create a method on your interface (example: IStudentRepository has method GetStudentByStudentNumber(int number) ). The reason I don't prefer this approach is it results in bigger repositories and more repository interfaces and classes. I'd rather have the specification classes, which better follow SRP, than many inconsistent IWhateverRepository implementations, given the choice. Hope that helps.

@ardalis ardalis closed this as completed Nov 22, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants