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.
77 Replies
I tried something with the constructor and the method in PostRepo but it didn't work
So now I'm lost
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
I think I understand. Let me try and see if it works. Thank you!
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 thisI'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.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
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/whats the purpose of BlogPostRepo?
Also, this is my program class
https://paste.mod.gg/ysfjjhjdlpif/0
BlazeBin - ysfjjhjdlpif
A tool for sharing your source code with the world!
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.
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.
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
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
still why are those on the post objects themselves?
GitHub
GitHub - Mastervoliumpl/DevBlogPF
Contribute to Mastervoliumpl/DevBlogPF development by creating an account on GitHub.
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
Just pushed all recent changes
alrigth im looking from the top down
Models.Post is only this
Or well all model classes just have properties or constructors
feeks like Author should be names User
but doesnt matter thats a nit pick lol sorry
anyway
as for PostRepo it has a AddPost and GetAllPosts. (Plus the add or remove tags)
shouldnt the id be readonly
also some of these properties should be required no?
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?
right but renaming is a hassle
anyway
And what does that do?
Like will it throw if the property is missing?
no it forced you to set it when creating the object
So something like this?
In Models.Post.cs
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()
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)
(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 declarationThat is true. Let me try
But how would I do this?
The constructor:
whats
id
?Ops forgot to include the 3rd one too
There
still where did
id
come from lolModels.Post constructor
posted it above
i dont see where
id
is declared
what you posted doenst compile
unless im blind
which i sometimes amGuid id
You declate the variable in the constructor using Guid.NewGuid()
ah
Then use that to set PostID and TagList = new TagList(id);
turns out i am indeed blind
It's fine, had more of these moments that I'd like to admit 😄
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?
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?
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?
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.
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?
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.
right but why?
why not just a string
As in just keep the name?
wdym?
what name?
oh
yeah
I guess your right. So then I would use TagName as the unique identifier for that specific Tag?
Guess that works
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 meanWhich one 😂
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?
I figured it out by adding this into BlogPostRepo:
Before this it wouldn't add the created blogpost to the list
thats not a very good idea
dont you have a Create method?
whats wrong with that?
Is it because I'm converting from BlogPost to Post?
Yeah, CreateBlogPost. Haven't done the interface thing you advised me to do yet.
why do you have 2 repos anyway?
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)
why?
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.
uh not really
Then through the interfaces use the methods
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
Yeah, until I get to add an actual database, this is what I have to do.
yeah thats fine
so why do you have 2?
more importantly why do you have 2 that you want to sync?
Repositories? I have 5 currently.
I'm guessing you mean PostRepo and BlogPostRepo?
yeah
i assume this is a school/uni project right?
Yes
you have a list of requirements right?
(School)
do they specify how the databse shoud work?
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. 🙂
no problem at all youre welcome
Today I relized that I completely misunderstood inheritance and that I did not need all the properties in Blogpost or Portfolio
Lmao