neozhu / RazorPageCleanArchitecture

This is a solution for creating the Razor Pages Application with ASP.NET Core(.net 7.0) following the principles of Clean Architecture
https://commercial.blazorserver.com/
MIT License
401 stars 94 forks source link

Adding ApplicationUser to Product as navigation property #98

Closed rwxzig closed 6 months ago

rwxzig commented 1 year ago

Is it possible to add ApplicationUser to src/Domain/Entities/Product.cs as a navigation property? So that for instance when a new product is created, the product would have the id of the user that did create the product. Suppose a scenario that I am using this architecture and I want to have ApplicationUser as the user that can make create/edit/delete on the other entities, I tried to do this, but of course I get a circular dependency error because Application projects includes Domain, and Infrastructure includes Application application, so if I would include Infrastructure in Domain a circular dependency error would be raised. What changes could be done so that the example above would be possible to implement? Regards. 😊

neozhu commented 1 year ago

I have implemented the feature you wanted, please refer to this project on Github https://github.com/neozhu/CleanArchitectureWithBlazorServer/tree/navigationIdentityUser move Identity folder from Infrastructure to Domain Project. change product class:


public class Product : BaseAuditableEntity
{
    public int Id { get; set; }
    public string? Name { get; set; }
    public string? Description { get; set; }
    public string? Brand { get; set; }
    public string? Unit { get; set; }
    public decimal Price { get; set; }
    public IList<string>? Pictures { get; set; }
    [ForeignKey("CreatedBy")]
    public ApplicationUser? CreatedByUser { get; set;}
}
DJJones66 commented 1 year ago

I haven't fully played with this code yet to explore a different option but if you look at the product as it stands now it is based on the AuditableEntity which does contain data for the CreatedBy. Maybe can use that to achieve the functionality.

I say this because just moving the Identity folder will probably not go as easily as it sounds.

I am going to test this out as I have time because I think we have a desire for something similar in my using customer as an example, while I want to maintain who the owner is and they have full permission, I also want the owner to allow other users to edit the same customer. This of course will require a bit more work, an additional entity, and data to handle the relationship between users/customer

This also does raise a question as I thought more about this, why are there 2 projects? In theory, given the object of Clean Architecture, you should be able to have both a Razor and Blazer front end in the same project and then select the one you wish to run. No clue really if this would work just a thought, I do backend coding and just starting to learn this side of things.

neozhu commented 1 year ago

Yes, Your idea is good. I've made it for blazor project https://github.com/neozhu/CleanArchitectureWithBlazorServer

DJJones66 commented 1 year ago

Yes, Your idea is good. I've made it for blazor project https://github.com/neozhu/CleanArchitectureWithBlazorServer

I only asking this here because this is where the conversation is though probably could be in the other project.

Your thoughts on what it would take to use Razor for that project instead of blazor or even as I spoke about above, having both there and merely selecting which one you wish to start up with? I don't do this enough to do anything other than guess and try but are there any headaches I am missing?

For me, this would be an idea because I have some projects that are best for Razor and others for Blazor.