C
C#11mo ago
GABRIEL22

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!
No description
No description
34 Replies
Jimmacle
Jimmacle11mo ago
why is there mixed usage of Nullable<T> and T? thonk
Angius
Angius11mo ago
Nullable<int> wtf
linqisnice
linqisnice11mo ago
Create subject, create student, put student in subject.Students, create teacher, put teacher in subject.Teacher then add to dbcontext
Angius
Angius11mo ago
Also, the teacher can just not have a name?
Jimmacle
Jimmacle11mo ago
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
Angius
Angius11mo ago
Regardless, yeah
var student = new Student();
var teacher = new Teacher();

var subject = new Subject();
subject.Teacher = teacher;
subject.Students.Add(student);
var student = new Student();
var teacher = new Teacher();

var subject = new Subject();
subject.Teacher = teacher;
subject.Students.Add(student);
GABRIEL22
GABRIEL22OP11mo ago
I dont know this is the model I've got. I doubt the value is undefined
Angius
Angius11mo ago
Got it from a teacher, I'd bet?
GABRIEL22
GABRIEL22OP11mo ago
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);
}
Jimmacle
Jimmacle11mo ago
repos on top of ef core Harold
linqisnice
linqisnice11mo ago
dont check teachercount, you cant add more than one teacher anyway in that model above lol its just one property not a collection
Angius
Angius11mo ago
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?
linqisnice
linqisnice11mo ago
also youre not adding anything to the db here
GABRIEL22
GABRIEL22OP11mo ago
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,
Jimmacle
Jimmacle11mo ago
but that count function doesn't seem to have any relation to checking a specific subject/teacher/etc
Angius
Angius11mo ago
Why did I know that such garbage can only be created by a teacher KEKW
linqisnice
linqisnice11mo ago
there is no need to count, you are not working with a collection of teachers
GABRIEL22
GABRIEL22OP11mo ago
Do I substract the nullable property? I know a Id couldn't be undefined.
linqisnice
linqisnice11mo ago
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
Angius
Angius11mo ago
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
linqisnice
linqisnice11mo ago
only reason i made ids nullable in my entity models is for cascade deletes
Angius
Angius11mo ago
If the teacher has to have a name, it should not be
GABRIEL22
GABRIEL22OP11mo ago
But, in the Controller, the way I add the teacher into Subject is fine? Or should I approach it another way?
linqisnice
linqisnice11mo ago
you should literally just get the subject, add the teacher and the students normally like regular c# code like zzz showed you above
Angius
Angius11mo ago
We're working within the constraints of, what seems like, a god-class kind of a repository So anybody's guess ¯\_(ツ)_/¯
GABRIEL22
GABRIEL22OP11mo ago
Without Id in the way, and just with number of teacher and that stuff?
Angius
Angius11mo ago
Subjects and teachers seem come from the same repo
linqisnice
linqisnice11mo ago
just scrap the repo lol
Angius
Angius11mo ago
With plain EF:
var subject = await _context.Subjects.FirstOrDefaultAsync(s => s.Id == id);

subject.Teacher = new Teacher();
subject.Students.Add(new Student());

await _context.SaveChangesAsync();
var subject = await _context.Subjects.FirstOrDefaultAsync(s => s.Id == id);

subject.Teacher = new Teacher();
subject.Students.Add(new Student());

await _context.SaveChangesAsync();
If you must, wrap it up in a repository method
GABRIEL22
GABRIEL22OP11mo ago
How can I unit test this function? I have to unit first test.
Angius
Angius11mo ago
Uh, you can't really unit test EF without, idk, using a test database, or mocking the dbcontext
GABRIEL22
GABRIEL22OP11mo ago
I'm using a In-Memory database.
Angius
Angius11mo ago
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 here
GABRIEL22
GABRIEL22OP11mo ago
Thank you for the help! I'm going to make the changes you suggest me.
Want results from more Discord servers?
Add your server