❔ i cant return object in JSON correctly in asp.net core web app
i cant serialize object correctly, so im creating asp.net core web app, and i have model structured kie this:
public class Card
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public double Price { get; set; }
public double Price2 { get; set; }
public double Price3 { get; set; }
public double Price4 { get; set; }
public double MorePrices { get; set; }
[ValidateNever]
public string Image { get; set; }
public int Amount { get; set; }
[ValidateNever]
public int? CardId { get; set; }
[ForeignKey("CardId")]
[ValidateNever]
public Card Parent { get; set; }
[ValidateNever]
public int TypeId { get; set; }
[ForeignKey("TypeId")]
[ValidateNever]
public Type Type { get; set; }
[ValidateNever]
public DateTime Created_at { get; set; }
[ValidateNever]
public DateTime Updated_at { get; set; }
[ValidateNever]
[NotMapped]
public List<Card>? Appendices { get; set; }
}
and i have API that pull this this item from db, and populate appendices with items that have CardId equals to id of this exec item. Now api returns that object in JSON, and i got completly unreadable JSON format, i use JSON formatter on chrome but it wont format it as other objects without Appendices. i tried fetching it but it wont. What i am doing wrong?
48 Replies
Describe "unreadable"
Nothing here seems particularly hard to serialize
readable:
and this:
unreadable
also got this:
under that
same page as unreadable
Ah, you have cyclic objects there, a card can reference a card, potentially even itself
ahh
Also, there's the issue of depth
i got it, one can refference another and so on
If you have a tree of 835 cards with their parents, that'll be an issue
As it says, the maximum depth is 32
If it's an API result, I'd honestly just return the parent ID, not the whole card
The frontend can then fetch that parent by its ID, if needed
im creating 2 app
one using react
one clear backend
here is api
Well, yes
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> Proizvodi(int? id)
{
if (id == null id == 0)
{
var mainItems = _unitOfWork.Card.GetAll(x => x.CardId == 0 x.CardId == null, tracked: false, included: "Type,Parent");
return Json(mainItems);
}
var item = await _unitOfWork.Card.FirstOrDefaultAsync(x => x.Id == id,included:"Type,Parent");
if(item == null)
{
return NotFound();
}
if(item.Type.Name == "Proizvod")
{
item.Appendices = _unitOfWork.Card.GetAll(x => x.CardId == item.Id).ToList();
return Json( item );
}
var items = _unitOfWork.Card.GetAll(x => x.CardId == id, included: "Type,Parent");
return Json(items);
}
That changes nothing in what I said
$code
To post C# code type the following:
```cs
// code here
```
Get an example by typing
$codegif
in chat
If your code is too long, post it to: https://paste.mod.gg/_unitOfWork
💀
I hope you're not using generic repositorieskheem, yes
Oof
Ouch
My bones
Im just beginner :((
DbContext
is the unit of work and a generic repositoryhere is unitOfWork and see it
It's much easier to not have abstraction over abstraction over abstraction over abstraction
And just use EF directly
using DataAccess.Context;
using DataAccess.Repository.IRepository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.Repository
{
public class UnitOfWork : IUnitOFWork
{
private readonly ApplicationDbDontext _db;
public ICardRepsitory Card { get; private set; }
public ITypeRepository Type { get; private set; }
public IApplicationUserRepository ApplicationUser { get; private set; }
public UnitOfWork(ApplicationDbDontext db)
{
_db = db;
Card = new CardRepository(_db);
Type = new TypeRepository(_db);
ApplicationUser = new ApplicationUserRepository(_db);
}
public async Task SaveAsync()
{
await _db.SaveChangesAsync();
}
}
}
direcly
Yeah those are just passthrough methods
yes
So they make no sense
There's no point
You basically recreated a
DbContext
with DbSet
s in ityea
and just past through, so it always one DbContext, but because of DI if i do that manually in every controller i request one DbContext it will give me same think
With some magic strings and direct
.Include()
s from the look of it, to bootoke idk why i complicate that stuff
yes
Yeah I don't know either lol
but why people use it then?
maybe when doing crud, just cutting code, but that can done manually in controller no?
Mostly because they were born in the 60s, learned that pattern from a 90-year-old COBOL programmer when they were 23, and haven't learned anything new since the 80s
And nowadays, just cargo cult
Someone said "generic repo good" and people started mimicking it like seagulls from Finding Nemo
ahhaahahaha
Ahh make sense
but probably generic repo is good for larger app
i mean it can cut lot of code
and write it once
It cuts zero code
Use non-generic repositories if you must
But a generic repo is just a passthrough
should be
for example
Or some different name for this method
is just recreating
but is more prone to errors
and if i need more complex query just to overload method in generic repo?
There should be no generic repo
There should be a
CardsRepository
it just make more complex when i start thinking
And it should have a method like
i get it, thanks. I will transform this app now
(while you're at it, make that stuff asynchronous)
(and prefer
.Select()
ing into DTO classes over .Include()
s)I will do it now. Thanks a lot
some error when closing, it wont close 😦
$close
Use the
/close
command to mark a forum thread as answeredOr you can just keep it open should you have other questions
huh
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.