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

Domain Services or AppServices example #2389


User avatar
0
ademg created

Hello guys, I need your opinion/code example on something.

I read that application services should not call each other and I agree with it.

I have a scenario where I have to create a user using UserManger get the id and use that Id to create another entity.

CheckErrors(await UserManager.CreateAsync(user));
            await CurrentUnitOfWork.SaveChangesAsync(); //To get new user's Id.
            candidate.UserId = user.Id;
            await _candidateRepository.InsertAsync(candidate);

What do you think of such case? Is there a better way to deal with this scenario maybe do this in the same UoW or using a domain service.


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

    Hi,

    If you need some piece of code in more than app service, it is a good candidate for a domain service. In your case, you can move this code in a domain service and domain services are UOW by default. Maybe this doc can guide you better <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Domain-Services">http://aspnetboilerplate.com/Pages/Docu ... n-Services</a>.

  • User Avatar
    0
    tteoh created

    <cite>ismcagdas: </cite> Hi,

    If you need some piece of code in more than app service, it is a good candidate for a domain service. In your case, you can move this code in a domain service and domain services are UOW by default. Maybe this doc can guide you better <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Domain-Services">http://aspnetboilerplate.com/Pages/Docu ... n-Services</a>.

    Dear Support,

    Please refer to this link: #2567@d519ab56-ce8e-4c12-8639-3586c191cdc6

    It was mentioned that Domain Service is NOT UoW by default unlike what being mentioned above.

    Appreciate your clarification once again.

    Thanks. /tommy

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @tteoh,

    Sorry for my mistake and thank you for finding it :). Domain services are not UoW by default, you can consider it the final answer :).

    UnitOfWork intercepter is registered here <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/Domain/Uow/UnitOfWorkRegistrar.cs">https://github.com/aspnetboilerplate/as ... gistrar.cs</a>. And it is registerd for IRepository and IApplicationService implementations, see <a class="postlink" href="https://github.com/aspnetboilerplate/aspnetboilerplate/blob/master/src/Abp/Domain/Uow/UnitOfWorkHelper.cs#L20">https://github.com/aspnetboilerplate/as ... per.cs#L20</a>.

    Thanks.

  • User Avatar
    0
    tteoh created

    Hi,

    Thank you providing the additional reference to UoW implementation in github. Certainly help to save time to look them up otherwise.

    To close up the discussion, am I right to understand that if a function implemented within the Domain Service to be executed as UoW, it has to be decorated explicitly as DS by default is not UoW.

    Thanks. /tommy

  • User Avatar
    0
    ismcagdas created
    Support Team

    Yes,

    Exactly, if you want to execute a domain service in a UoW, it needs to be decorated explicitly with a UnitOfWork attribute and it must be marked as virtual in order to be intercepted.

  • User Avatar
    0
    trendline created

    <cite>ismcagdas: </cite> Yes,

    Exactly, if you want to execute a domain service in a UoW, it needs to be decorated explicitly with a UnitOfWork attribute and it must be marked as virtual in order to be intercepted.

    It seems in the document [https://aspnetboilerplate.com/Pages/Documents/Domain-Services]), it doesn't show a walkthrough that methods in the application services needs to be marked as virtual.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @ trendline,

    This might be a useful information, we will add it to this document. Thanks for pointing it out.