Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

How to delete an entity right way with validation (ddd)? #11234


User avatar
0
cangunaydin created

Hello, Right now on my asp.net zero project, i am trying to refactor and apply some rules from ddd. So in my case, i need a business validation on my domain before delete happens. To give an example, let me simplify this. Let's say that I have Book Entity which holds information like

public class Book: FullAuditedEntity,IMustHaveTenant
{
    public Book(string name,int tenantId)
    {
        Name=name;
        TenantId=tenantId;
    }
    public void AssignCategory(string categoryName){
        Check.NotNull(categoryName,nameOf(categoryName));
        CategoryName=categoryName;
    }

    public int TenantId { get; set; }
    
    public string Name {get;private set;}
    
    
    public string CategoryName{get; private set}

}

public class BookManager: DomainServiceBase,IBookManager
{
    private readonly IRepository<Book> _bookRepository;
    
    public BookManager(IRepository<Book> bookRepository){
        _bookRepository=bookRepository;
    }

   public DeleteBook(Book book){
           //Check sth from database.
            var isExistWithSameCategoryName=await _bookRepository.GetAll().AnyAsync(o=>o.CategoryName==book.CategoryName && o.Id!=book.Id);
            if (isExistWithSameCategoryName)
                throw new UserFriendlyException("You can not delete");
            //if validation pass then delete
            _bookRepository.Delete(book);
   }

}

as you can see in the above example i want my clients to use only BookManager (my domain service) to do delete operation. But since delete is an option in repository, i can not restrict the clients over here. They can still use repositories delete method on the application layer. Is there any solution to restrict the delete process? I couldn't get my head around it.

And second question is about TenantId, since i implement IMustHaveTenant interface TenantId property should be public, that means clients constructing the entity can change that value. is there a way to restrict it also?

Thank you for the assistance. hope i can explain what i am looking for.


4 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @cangunaydin

    Unfortunately, we don't have any suggestion for both questions. I couldn't think of any way for technically restricting such usages.

  • User Avatar
    0
    cangunaydin created

    Hello @ismcagdas thank you for the answer. Over here I try to simplify the things, but sometimes deleting operations in our project can have some business rules. For ex deleting an order can only be done in some kind of state. I understand it can't be done for now, but what I would like to ask is

    can I mark some entities with an interface. So instead of using IRepository interface use another repository interface and restrict users to use IRepository on the application layer somehow with those entities?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @cangunaydin

    Maybe you can do it on runtime but I can't think of a way to do it on development time. So, probably this will not work for you.

  • User Avatar
    0
    cangunaydin created

    I am closing this then. thank you for the time.