Join Anton Delsink for an in-depth discussion in this video Create classes User, Post, and Content, part of Object Oriented Programming with C#.
- [Instructor] In Visual Studio, let's create a new solution. File, new, project and I'll select .NET Core and a test project. I'll call it project tests and the solution, social media. Click okay. I'll rename the class by renaming the file Tests_SocialMedia. Say yes to rename the class and I'll just rename the method, Test_001.
Now remember, we do have the use cases and it'll be worth referring to them as we develop our tests. I'll paste in here what we had on the slides before. And so, we're trying to create functionality to support users creating posts, users following other users, users viewing a feed, a collection of content from other users, users liking some of that content and users sharing posts. So quite obviously, users are involved and posts are involved. So, let's create those. So, in my first unit test, let's just act as if those things exist.
So, when I write out var user = new user, those classes obviously don't exist but let's just consider what we would need. So, for a user I'm going to assume the user is already authenticated but we still need some identifier, some unique ID so that this user can be distinguished from another. And so for this, I'll just use a number for now, one for the first user. For the post, maybe we need a unique identifier, maybe we need a URL. For now, for me the focus is that the post actually contains some content.
And so, that would be the minimum we need to instantiate these but the post existing as an object and the user existing as an object mean that there's no relation between the two just yet. And so, in my mind we should have some means of managing this content, managing the links between these two. So I'll create a new content object, some kind of controller to manage your social graph engine and we'll ask it to publish the post that was just created but we need to associate it with the user.
And so, in this case, the user object is not going to know about the post and the post object is not going to know about the user. And so, the responsibilities of those classes would remain clean and focused on their one and only purpose. Instead, I'll make it so that the content class is the one managing the association between users and posts and in this case, I'm saying .Publish. So, we're actually creating a new post and so this is a post created by this user. Now for this to build and compile, of course, we are going to need to generate these classes.
Place my cursor on line 18. Press Control + . And so we'll generate the class user in a new file. I'll do the same for post. Put my cursor on the word post. Press Control + . Generate class post. Same for content on line 21. Control + . Generate content. And one more, little bit different this time, the publish method. Click on the name of the method. Press Control + . and generate the method.
Let's see what was generated. So, for the user, double click User.cs. We can see here, class user with a private variable. I know this is the ID. Control + RR for renaming, refactoring name. Change that to ID. Change this one to ID. Control + RR. This is the user's ID. Take a look at post. Going to rename the v here. Control + RR. This is the text if we're writing a microblogging service then that would be the text of our Tweet or our post.
Control + RR to rename this one. And so, in both cases, the post and the user only knows about their own content. They don't act on anything externally. Going to content. Here it is a bit different. Content knows about both user and post. And so, at this point, the content is intended, it's purpose, is to manage the links among these two. And so when I say a link, we of course still needs to persist, somehow save this association. But for now looking at our test, it looks like we have a way to actually accomplish the publishing of content.
So, save all. Build, and if it compiles go to the test menu and right click run. This must fail. We have not implemented our publish method yet. You'll see once you select the test, that it'll say not implemented exception. And so we will go to the publish method to implement and thus create some association, some link between this user and this post.
- Abstract classes
- Processing text with StreamReader
- IEnumerable and yield return
- Windows Forms controls
- Windows Forms with and without the designer
- Adding, organizing, and testing forms
- Creating classes