C
C#•3mo ago
Mastervoliumpl

Add an object to list

Hello, I am currently working on a school project. I currently have a Models folder, BLL in which there is Interfaces and Repositories. Now I'm trying to create a blog post. However, the list that keeps all the blog posts is in PostRepo. How would I do it so that every time a blogpost or any other inherited class from post will save its "post" in that list? I attached pictures for reference.
No description
No description
No description
No description
No description
77 Replies
Mastervoliumpl
Mastervoliumpl•3mo ago
I tried something with the constructor and the method in PostRepo but it didn't work So now I'm lost
Evyr
Evyr•3mo ago
you could pass a reference for PostRepo around so that classes can add to that instance's list or you could use something like the singleton pattern to have one instance of the PostRepo class for the whole application
Mastervoliumpl
Mastervoliumpl•3mo ago
I think I understand. Let me try and see if it works. Thank you!
sibber
sibber•3mo ago
first note is that creating an interface for every implementation for a repo defeats the point of interfaces e.g. youd typically have in your case maybe IRepo<TSelf> which defines Create Update etc, which all those concrete implementations would implement also why does a repo have a property of a single post id? CreateBlogPost does nothing currently it creates an object which immediately becomes garbage to be collected are you trying to create a factory? i dont see why thats really needed for this
Mastervoliumpl
Mastervoliumpl•3mo ago
I'm just beginning to understand C# and I'm just having difficulty trying to grasp all of this. So you're saying I should have an interface that all the repos inherit, and still defy each Create, update, etc in their respective repo? why does a repo have a property of a single post id?? fair, I'm not sure why I put it there either. CreateBlogPost does something now thanks to help from Evyr (Attached picture) As to the last question, I don't know what a factory is, so I can't tell you lol. For context, we're told by our teacher/professor to create a backend for a blogpost/portfolio. And to use program/console to show the functions/methods. Later on were supposed to learn about Razorpages and then Database, which we will incorporate into this. As to what should be included, it's upto your own discretion as long as there are interfaces, repositories and model classes.
No description
No description
sibber
sibber•3mo ago
So you're saying I should have an interface that all the repos inherit, and still defy each Create, update, etc in their respective repo?
yup interfaces are used to define a public api so that methods can say for example "i want something that has an Add method and a Remove method", and so you can say in your class definition that your class will have those methods, using interfaces also $code
MODiX
MODiX•3mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
sibber
sibber•3mo ago
whats the purpose of BlogPostRepo?
Mastervoliumpl
Mastervoliumpl•3mo ago
Also, this is my program class https://paste.mod.gg/ysfjjhjdlpif/0
BlazeBin - ysfjjhjdlpif
A tool for sharing your source code with the world!
Mastervoliumpl
Mastervoliumpl•3mo ago
It is a bit outdated as I changed my mind on some things as I learnt something new or it didn't make sense anymore.
No description
Mastervoliumpl
Mastervoliumpl•3mo ago
Devblogs and PortfolioItem have since been removed And TagList is now created with each post, so you can add or remove tags from each post from it's tag list, instead of having to create new taglists and etc. Post is the main post class but Blog post and Portfolio would have their own unique properties.
sibber
sibber•3mo ago
a couple of questions what would Post.EditPost do? and is there a reason why Post isnt an interface? oh i see nevermind the last question
Mastervoliumpl
Mastervoliumpl•3mo ago
The post methods have been removed, and moved to only blogpost or portfolio Actually no I lied Hold on, let me just make the github repo public, will be easier
sibber
sibber•3mo ago
still why are those on the post objects themselves?
Mastervoliumpl
Mastervoliumpl•3mo ago
GitHub
GitHub - Mastervoliumpl/DevBlogPF
Contribute to Mastervoliumpl/DevBlogPF development by creating an account on GitHub.
sibber
sibber•3mo ago
the thing that stores the objects is the repo posts shouldnt have a reference to the repo that doesnt make much sense its not their job
Mastervoliumpl
Mastervoliumpl•3mo ago
Just pushed all recent changes
sibber
sibber•3mo ago
alrigth im looking from the top down
Mastervoliumpl
Mastervoliumpl•3mo ago
Models.Post is only this
C#
public abstract class Post
{
public Guid PostID { get; set; }
public DateTime DateCreated { get; set; }
public Author Author { get; set; }
public string Title { get; set; }
public TagList TagList { get; set; }
public Enum PostType { get; set; }
public List<Image> Images { get; set; } = new List<Image>();

public Post(Author author)
{
Guid id = Guid.NewGuid();
PostID = id;
TagList = new TagList(id);
Author = author;
DateCreated = DateTime.Now;
}
}

public enum PostType
{
BlogPost,
Portfolio
}
C#
public abstract class Post
{
public Guid PostID { get; set; }
public DateTime DateCreated { get; set; }
public Author Author { get; set; }
public string Title { get; set; }
public TagList TagList { get; set; }
public Enum PostType { get; set; }
public List<Image> Images { get; set; } = new List<Image>();

public Post(Author author)
{
Guid id = Guid.NewGuid();
PostID = id;
TagList = new TagList(id);
Author = author;
DateCreated = DateTime.Now;
}
}

public enum PostType
{
BlogPost,
Portfolio
}
Or well all model classes just have properties or constructors
sibber
sibber•3mo ago
feeks like Author should be names User but doesnt matter thats a nit pick lol sorry anyway
Mastervoliumpl
Mastervoliumpl•3mo ago
as for PostRepo it has a AddPost and GetAllPosts. (Plus the add or remove tags)
sibber
sibber•3mo ago
shouldnt the id be readonly also some of these properties should be required no?
Mastervoliumpl
Mastervoliumpl•3mo ago
It might be in the future. For now I intended there to be only the author for the posts. No one else will be able to post on the devblog or porfolio. But IF i have time I will introduce comments for the dev blog. Yes, and yes. But how do you make a property required?
sibber
sibber•3mo ago
right but renaming is a hassle anyway
public required string Title { get; set; }
public required string Title { get; set; }
Mastervoliumpl
Mastervoliumpl•3mo ago
And what does that do? Like will it throw if the property is missing?
sibber
sibber•3mo ago
no it forced you to set it when creating the object
Mastervoliumpl
Mastervoliumpl•3mo ago
C#
public required Guid PostID { get; set; }
public required DateTime DateCreated { get; set; }
public required Author Author { get; set; }
public required string Title { get; set; }
public required TagList TagList { get; set; }
public required Enum PostType { get; set; }
C#
public required Guid PostID { get; set; }
public required DateTime DateCreated { get; set; }
public required Author Author { get; set; }
public required string Title { get; set; }
public required TagList TagList { get; set; }
public required Enum PostType { get; set; }
So something like this? In Models.Post.cs
sibber
sibber•3mo ago
you want all of them to be required? also Guid PostID { get; init; } for readonly or if you want to set that here you can do Guid PostID { get; } = Guid.NewGuid()
Mastervoliumpl
Mastervoliumpl•3mo ago
I mean, the way I understand it is that all of these properties would be required for a post to be created. What would you make required? and why? (Trying to understand)
sibber
sibber•3mo ago
(btw theres a new Guid.CreateVersion7() which generates a v7 guid which can be ordered by creation time, which makes lookups faster for a lot of cases) no thats fine also theres no need for a constructor if youre not doing anything special you can set those properties directly in the same line as the declaration
Mastervoliumpl
Mastervoliumpl•3mo ago
That is true. Let me try But how would I do this?
C#
Guid id = Guid.NewGuid();
PostID = id;
TagList = new TagList(id);
C#
Guid id = Guid.NewGuid();
PostID = id;
TagList = new TagList(id);
The constructor:
C#
public Post(Author author)
{
Guid id = Guid.NewGuid();
PostID = id;
TagList = new TagList(id);
Author = author;
DateCreated = DateTime.Now;
}
C#
public Post(Author author)
{
Guid id = Guid.NewGuid();
PostID = id;
TagList = new TagList(id);
Author = author;
DateCreated = DateTime.Now;
}
sibber
sibber•3mo ago
whats id?
Mastervoliumpl
Mastervoliumpl•3mo ago
Ops forgot to include the 3rd one too There
sibber
sibber•3mo ago
still where did id come from lol
Mastervoliumpl
Mastervoliumpl•3mo ago
Models.Post constructor posted it above
sibber
sibber•3mo ago
i dont see where id is declared what you posted doenst compile unless im blind which i sometimes am
Mastervoliumpl
Mastervoliumpl•3mo ago
Guid id You declate the variable in the constructor using Guid.NewGuid()
sibber
sibber•3mo ago
ah
Mastervoliumpl
Mastervoliumpl•3mo ago
Then use that to set PostID and TagList = new TagList(id);
sibber
sibber•3mo ago
turns out i am indeed blind
Mastervoliumpl
Mastervoliumpl•3mo ago
It's fine, had more of these moments that I'd like to admit 😄
sibber
sibber•3mo ago
also you should use DateTimeOffset because it keeps track of the time zone so a TagList takes the id of the post what happens if someone sets this TagList to another list? i think you may want to make this readonly as well but why does it take the id anyway?
Mastervoliumpl
Mastervoliumpl•3mo ago
Their not supposed to. TagList is like a background thing to prevent stack overflow. (Many tags for Many Posts). It takes the PostID of the post it is bound to. Thats how I imagined it anyways TagList property?
sibber
sibber•3mo ago
none of those are on the stack wdym many tags for many posts? why is it not just a list of strings? or a list of tag if you want some string typing for some reason or another?
Mastervoliumpl
Mastervoliumpl•3mo ago
So when you create a blog post, you will be able to set the tags that you would like the post to have. You just add the tags you want to the tag list of a post.
sibber
sibber•3mo ago
thats fine why cant it be a list of strings? or List<Tag> if you need tags to be their own objects i still dont see why it needs the id of the post i see you have a tag type is there a reason you need tags to have ids?
Mastervoliumpl
Mastervoliumpl•3mo ago
Need to think about this lol. Made it but forgot why. To be able to reference that tag. Like Add this tagid to this TagList of a post.
sibber
sibber•3mo ago
right but why? why not just a string
Mastervoliumpl
Mastervoliumpl•3mo ago
As in just keep the name?
sibber
sibber•3mo ago
wdym? what name?
Mastervoliumpl
Mastervoliumpl•3mo ago
C#
public Guid TagID { get; init; }
public string TagName { get; set; }
C#
public Guid TagID { get; init; }
public string TagName { get; set; }
sibber
sibber•3mo ago
oh yeah
Mastervoliumpl
Mastervoliumpl•3mo ago
I guess your right. So then I would use TagName as the unique identifier for that specific Tag? Guess that works
sibber
sibber•3mo ago
yeah unless you need more properties for the tag again Upload/DeleteImage dont really belong in the image object they belong in whatever uploads/deletes the images now your original question i dont know what you mean
Mastervoliumpl
Mastervoliumpl•3mo ago
Which one 😂
sibber
sibber•3mo ago
hahaha
How would I do it so that every time a blogpost or any other inherited class from post will save its "post" in that list?
Mastervoliumpl
Mastervoliumpl•3mo ago
I figured it out by adding this into BlogPostRepo:
C#
private readonly IPostRepo _postRepo; // <--- This part

public void CreateBlogPost(Author author, string title, string bodyText)
{
// Create a new BlogPost
BlogPost blogPost = new BlogPost(title, author, bodyText);
_postRepo.AddPost(blogPost); // <--- And this
}
C#
private readonly IPostRepo _postRepo; // <--- This part

public void CreateBlogPost(Author author, string title, string bodyText)
{
// Create a new BlogPost
BlogPost blogPost = new BlogPost(title, author, bodyText);
_postRepo.AddPost(blogPost); // <--- And this
}
Before this it wouldn't add the created blogpost to the list
sibber
sibber•3mo ago
thats not a very good idea dont you have a Create method? whats wrong with that?
Mastervoliumpl
Mastervoliumpl•3mo ago
Is it because I'm converting from BlogPost to Post? Yeah, CreateBlogPost. Haven't done the interface thing you advised me to do yet.
sibber
sibber•3mo ago
why do you have 2 repos anyway?
Mastervoliumpl
Mastervoliumpl•3mo ago
I have a Repo for each Model class that needs it. As in has methods E.g Author does not have any methods. (At least currently)
sibber
sibber•3mo ago
why?
Mastervoliumpl
Mastervoliumpl•3mo ago
And the way I understood Reposetories is that it's basically where you want all your buissness logic. (Methods) So instead of having methods in the model classes where one would be able to access it, you instead do that in the repo.
sibber
sibber•3mo ago
uh not really
Mastervoliumpl
Mastervoliumpl•3mo ago
Then through the interfaces use the methods
sibber
sibber•3mo ago
that depends on what the methods are what i see is that youre using them as sort of a database which is what repositories typically are the layer which connects you to the database
Mastervoliumpl
Mastervoliumpl•3mo ago
Yeah, until I get to add an actual database, this is what I have to do.
sibber
sibber•3mo ago
yeah thats fine so why do you have 2? more importantly why do you have 2 that you want to sync?
Mastervoliumpl
Mastervoliumpl•3mo ago
Repositories? I have 5 currently. I'm guessing you mean PostRepo and BlogPostRepo?
sibber
sibber•3mo ago
yeah i assume this is a school/uni project right?
Mastervoliumpl
Mastervoliumpl•3mo ago
Yes
sibber
sibber•3mo ago
you have a list of requirements right?
Mastervoliumpl
Mastervoliumpl•3mo ago
(School)
sibber
sibber•3mo ago
do they specify how the databse shoud work?
Mastervoliumpl
Mastervoliumpl•3mo ago
Well see, heres the issue. We haven't gotten to databases yet (as in how to use them with code). And we haven't gotten to Razorpages yet, which is how you would interact with this website. I'm meant to make the backend and have it work. I guess I could add something simple like saving things in a csv or something. Then later exchange the things for a database. But until then it's not or requirment/need. As for requirements, there isn't any requirements regarding what NEEDs to be included feature wise. But what the teacher will be looking at is the interfaces, repositories and model classes. And obviously what would be needed for a blogpost and portfolio to work. -------------------- Also, I will be heading of for today. Currently it's 00:17 lol. I really appreciate the help, thank you. I will look through this and see what I can implement and think through some of this stuff. Will probably come back tomorrow for some questions if no one minds. 🙂
sibber
sibber•3mo ago
no problem at all youre welcome
Mastervoliumpl
Mastervoliumpl•3mo ago
Today I relized that I completely misunderstood inheritance and that I did not need all the properties in Blogpost or Portfolio Lmao
Want results from more Discord servers?
Add your server