C
C#2w ago
iskander

✅ how should I deal with common/similar data types between the backend and front?

so i have two projects, one for my backend and the other for the front. now i have this ShopItemDTO in the backend and a very similar ShopItem class for the front. how
C#
namespace BuyMe.ServiceAPI.Models
{
public class ShopItemDTO
{
public string? Title { get; set; }
public float Rating { get; set; }
public int Price { get; set; }
public int Id { get; set; }
public bool IsOnSale { get; set; }

public ShopItemDTO() { }
//ShopItem is unique to the ServiceAPI
//it has the GUID of the seller that we dont want to share
public ShopItemDTO(ShopItem ShopItem) =>
(Title, Rating, Price, Id, IsOnSale) = (ShopItem.Title, ShopItem.Rating, ShopItem.Price, ShopItem.Id, ShopItem.IsOnSale);
}
}
C#
namespace BuyMe.ServiceAPI.Models
{
public class ShopItemDTO
{
public string? Title { get; set; }
public float Rating { get; set; }
public int Price { get; set; }
public int Id { get; set; }
public bool IsOnSale { get; set; }

public ShopItemDTO() { }
//ShopItem is unique to the ServiceAPI
//it has the GUID of the seller that we dont want to share
public ShopItemDTO(ShopItem ShopItem) =>
(Title, Rating, Price, Id, IsOnSale) = (ShopItem.Title, ShopItem.Rating, ShopItem.Price, ShopItem.Id, ShopItem.IsOnSale);
}
}
C#

namespace BuyMe.Models;

public class ShopItem_Front()
{
public string? Title { get; set; }
public float Rating {get; set;}
public int Price { get; set; }
public int Id { get; set; }
public bool IsOnSale { get; set; }
};
C#

namespace BuyMe.Models;

public class ShopItem_Front()
{
public string? Title { get; set; }
public float Rating {get; set;}
public int Price { get; set; }
public int Id { get; set; }
public bool IsOnSale { get; set; }
};
now im still early in the project so the class will probably have stuff added/removed from it and it be tedious to just reflect the changes from both classes. i was thinking i could maybe make the ShopItemDTO shared between the front and backend? but not sure if that would result in any security concerns? or some other problem that i dont know of 😅
7 Replies
ero
ero2w ago
i would definitely advocate for at least one more project, yes. the name is up to you, i'm not the biggest fan of what i came up with.
namespace BuyMe.ApiProtocols.ShopItems;

public sealed record ShopItemDetailedResponse(
int Id,
string Title,
int Price,
float Rating,
bool IsOnSale);
namespace BuyMe.ApiProtocols.ShopItems;

public sealed record ShopItemDetailedResponse(
int Id,
string Title,
int Price,
float Rating,
bool IsOnSale);
you can then also add another project, or simply add a new namespace to your backend project.
namespace BuyMe.Extensions;

internal static class ShopItemExtensions
{
public static ShopItemDetailedResponse ToDetailed(this ShopItem shopItem)
{
return new(
shopItem.Id,
shopItem.Title,
shopItem.Price,
shopItem.Rating,
shopItem.IsOnSale);
}
}
namespace BuyMe.Extensions;

internal static class ShopItemExtensions
{
public static ShopItemDetailedResponse ToDetailed(this ShopItem shopItem)
{
return new(
shopItem.Id,
shopItem.Title,
shopItem.Price,
shopItem.Rating,
shopItem.IsOnSale);
}
}
iskander
iskanderOP2w ago
im not the greatest with names either but i was thinking i'd just name it BuyMe.Common 😅 thanks a lot for your help! that does seem a whole lot more elegant then just ctrl+tab'ing between files lol
FestivalDelGelato
the issue here is it's not clear what ShopItemDTO is being used for because if it's a creation model, for example, it shouldn't have an id if it's a viewing model maybe you don't want to give all the fields or maybe you want to add more fields that could be useful like an image so it could be useful to have a shared abstract class (or even interface) for some 'core' fields, but most probably there will be fields dedicated to the specific feature
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
iskander
iskanderOP2w ago
oh yeah for sure but im still in the "god please just work" phase of the project. i will put in more thought for this in the future 😅 ah good to know. thanks a lot!
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?