Static function qestion - Page 1
Forum section: electronics / general

author: Pete66
author_rank: Regular Contributor
timestamp: December 03, 2020, 10:31:55 pm
content: Here is a question. I dont think there is a right or wrong way Im just curious to see your opinion.The code is in C#.I have 2 classes.Code:public class Author{public int AuthorID{ get; set; }public string AuthorName{ get; set; }}public class Book{public int BookID{ get; set; }public int AuthorID{ get; set; }public string BookName{ get; set; }}I want to create a static method that gets a List of books by an AuthorIDLike this:Code:public static List<Book> GetBooks(int AuthorID){List<Book> lstBooks = new List<Book>();lstBooks = //(code to get all this authors books from DB goes here)return lstBooks;}Here is the question:Would you put this static function in the Author class or in the Book class or somewhere else?

author: rs20
author_rank: Super Contributor
timestamp: December 03, 2020, 10:39:34 pm
content: Opinion: it should be in a Database or DatabaseReader class (or similar, haven't actually put any thought into this). If I did that, it'd also not be static (thisorthis.databasewould be the reference to the database to read from).(Fwiw, this would help with unit testing too, because then you could pass in a fake Database or DatabaseReader for testing (unless C# has special features for unit testing static classes that I'm not aware of)).Backup opinion: meh who cares, if just a random personal project.

author: sleemanj
author_rank: Super Contributor
timestamp: December 03, 2020, 11:37:15 pm
content: I would put it in a "Library" class (as in literary library)Or I would make it a method of Author objects and not a static method. Instantiate an author and ask them what books they wrote.

author: Sojourner
author_rank: Unknown
timestamp: December 06, 2020, 01:11:26 am
content: The approach I would take is to put it in the Book class, since the method pertains to books, and inherit the DB class.There's no need for it to be static, unless you plan on using the method outside of the instantiated class. Such as a helper function of sorts.

author: rs20
author_rank: Super Contributor
timestamp: December 06, 2020, 05:27:45 am
content: Quote from: Sojourner on December 06, 2020, 01:11:26 amI know we're just throwing opinions out here, but I have to disagree with this. If Book inherits DB, then that implies that a Book is a type of DB, just like an apple is a type of fruit. But a Book isn't a DB, it's just a Book. So having Book inherit the DB class is about as wrong is possible (or poor practice, or very likely to cause great confusion to other engineers or however you want to put it).

author: Sojourner
author_rank: Unknown
timestamp: December 06, 2020, 06:20:31 am
content: I agree that Book isn't a type of DB, however Book does have an 'IS-A' relationship to the DB. Book is a table, and has multiple 'HAS-A' relationships to the Book table, such as the BookName, and BookID entities within the table.Book------INT BookID (PK)CHAR(30) BookNameAuthor--------INT ID (PK)INT AuthID (foreign Book.BookID)Please correct me if my logic doesn't track.

author: rs20
author_rank: Super Contributor
timestamp: December 06, 2020, 10:46:55 am
content: Unless I've misinterpreted, the OP is asking about the C# code. So your suggestion of having the Book class inheriting the Database class:Code:class Book : Database {...}Would mean that code like this would be valid:Code:Book book = getBookFromSomwhere(...);Database db = book; // valid code, but what does this mean ?!?!?!?!?And accordingly, Book objects would have to havedatabase-themed methodslike BeginTransaction and ExecuteSqlCommand. All of this makes absolutely no sense (to me at least, and I suspect any reasonably experienced software engineers), which is why I object to the idea of Book inheriting a DB class.

author: Sojourner
author_rank: Unknown
timestamp: December 06, 2020, 01:15:56 pm
content: You're absolutely right. My mistake.It doesn't make sense to have the Book class inherit the DB class.It would make more sense for the "Library" class that sleemanj suggested to inherit the DB class.My apologies.

author: rs20
author_rank: Super Contributor
timestamp: December 08, 2020, 01:27:07 am
content: No need to apologise, we're all learning here!

author: zfzackfrost
author_rank: Newbie
timestamp: January 07, 2021, 02:16:49 am
content: Quote from: rs20 on December 03, 2020, 10:39:34 pmI agree. I dedicated collection class is the cleanest way to go about this.Quote... Well, me of course!

author: radiolistener
author_rank: Super Contributor
timestamp: January 10, 2021, 08:03:53 am
content: these classes are entities, it's better to put such method into separate class to not confuse logic code with entity definitions.