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

When to use [UnitOfWork] Attribute #2567


User avatar
0
bilalhaidar created

Hello, in this source code below, the class already inherits from MyTestsDomainServiceBase, hence, all methods are handled automatically by UOW.

Why the code still adds an attribute [UnitOfWork], is it necessary in this case?

Thanks

public class FriendshipManager : MyTestsDomainServiceBase, IFriendshipManager
    {
        private readonly IRepository<Friendship, long> _friendshipRepository;

        public FriendshipManager(IRepository<Friendship, long> friendshipRepository)
        {
            _friendshipRepository = friendshipRepository;
        }

        [UnitOfWork]
        public void CreateFriendship(Friendship friendship)
        {
            if (friendship.TenantId == friendship.FriendTenantId &&
                friendship.UserId == friendship.FriendUserId)
            {
                throw new UserFriendlyException(L("YouCannotBeFriendWithYourself"));
            }

            using (CurrentUnitOfWork.SetTenantId(friendship.TenantId))
            {
                _friendshipRepository.Insert(friendship);
                CurrentUnitOfWork.SaveChanges();
            }
        }

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

    Hi,

    Normally you don't need this but I couldn't remember why we did this at the moment. I will check this and let you know. You can remove it and try chat functionality in the mean time.

    Thanks.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Sorry for the late reply, Normally Domain Services are not UoW by default, that is why UnitOfWork attribute is used.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Thank you. I understand now. Why would DS not be UOW?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Because domain services are mostly used in app services/controllers and cannot be reached from UI directly. Since app services and controllers are unit of work by default, there is no need to make domain services UoW by default.

    Thanks.

  • User Avatar
    0
    DennisAhlin created

    Just FYI, here you can read more about the implementation of UOW :)

    <a class="postlink" href="https://www.aspnetboilerplate.com/Pages/Documents/Unit-Of-Work">https://www.aspnetboilerplate.com/Pages ... it-Of-Work</a>

  • User Avatar
    0
    bilalhaidar created

    Thanks everyone.