How to add data into one-to-one relationship
Hello world!
I'm junior c# developer whose inmediate task is create a function that add Students and Teacher into one Subject. But I dont know where Can I start.
Lets' see the Subject model:
And this is the Teacher Model:
I must to mention that the project methodology is TDD, so I have to unit test first and later refactor if previus step are ok. But the problem is I dont know how could I test this function
So, if anyone could help me with a advice or link documentary, Id be really grateful!
34 Replies
why is there mixed usage of
Nullable<T>
and T?
Nullable<int>
wtfCreate subject, create student, put student in subject.Students, create teacher, put teacher in subject.Teacher
then add to dbcontext
Also, the teacher can just not have a name?
but yeah the answer to your question is simply assign the subject's teacher or add the subject to the teacher's subjects collection
normal OOP things
Regardless, yeah
I dont know this is the model I've got. I doubt the value is undefined
Got it from a teacher, I'd bet?
This is how I did: public IHttpActionResult EnrollTeacher(string id) {
var subject = _repo.GetById(id); var teacher = _repo.GetTeacherbyId(id);
if(_repo.teacherCount() > 1 && subject != null) { Console.WriteLine("Teacher already set up"); } else { subject.TeacherId = teacher.id; } return Ok(subject);
}
repos on top of ef core
dont check teachercount, you cant add more than one teacher anyway in that model above lol
its just one property not a collection
Was about to say, a subject has a single teacher
There shouldn't even be a
teacherCount()
(why is it camelCase btw) method there
Unless... it counts all teachers in the db?
But if so, then what would be the relevance?also youre not adding anything to the db here
Yes, a Subject has a single teacher, and I establish a Count function to check if a Subject already contain a Teacher, if is not, then it add the Teacher into Subject. But the problem is I'm not sure if I did ok nor how to unit test it,
but that count function doesn't seem to have any relation to checking a specific subject/teacher/etc
Why did I know that such garbage can only be created by a teacher
there is no need to count, you are not working with a collection of teachers
Do I substract the nullable property? I know a Id couldn't be undefined.
you check if teacher is null, if its null, add teacher, if its not null, check if you are allowed to replace the teacher
thats it
A property should be nullable only and only every if the value can, actually, be null
If a teacher can just not have a name, sure, the name property should be nullable
only reason i made ids nullable in my entity models is for cascade deletes
If the teacher has to have a name, it should not be
But, in the Controller, the way I add the teacher into Subject is fine? Or should I approach it another way?
you should literally just get the subject, add the teacher and the students normally
like regular c# code
like zzz showed you above
We're working within the constraints of, what seems like, a god-class kind of a repository
So anybody's guess ¯\_(ツ)_/¯
Without Id in the way, and just with number of teacher and that stuff?
Subjects and teachers seem come from the same repo
just scrap the repo lol
With plain EF:
If you must, wrap it up in a repository method
How can I unit test this function? I have to unit first test.
Uh, you can't really unit test EF without, idk, using a test database, or mocking the dbcontext
I'm using a In-Memory database.
How are you unit testing
_repo.GetTeacherbyId(id)
?
You'd do it similarly, ig. Call the repository method, see if it made the appropriate changes to the db
I never used god-class repositories, nor did I ever unit test them, so I probably won't be of much help
Someone with more experience with, uh, enterprise (derogatory) kind of code would be more helpful hereThank you for the help! I'm going to make the changes you suggest me.