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

Clarifications on SignalR and UnitOfWork #7888


User avatar
0
ivanosw1 created

Hi, We are implementing a SignalR service that needs to write e read from database and is authenticated. There are some best practices to deal with database ? We are experiencing some "object disposed" exception accessing to database.

I've looked at your Chat module and I've seen then some time the UnitOfWork attribute is used but not always. Can you lead me on the correct way?

Thank you


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

    I've looked at your Chat module and I've seen then some time the UnitOfWork attribute is used but not always.

    Can you point out the specific code?

    Can you lead me on the correct way?

    Controllers, application services, domain services, repositories, etc. all use work units, so it is recommended that you always use it.

    https://aspnetboilerplate.com/Pages/Documents/Unit-Of-Work

  • User Avatar
    0
    ivanosw1 created

    In ChatMessageManager, Save and GetUnreadMessageCount have the UnitOfWork attribute, while FindMessageAsync hasn't. Why ?

    Anyway, reading with more attention, your link gave me some response to my questions.

    Thank you

  • User Avatar
    0
    musa.demir created

    In addition to atomic processing, UnitOfWork also undertakes tenant management.

    In Save and GetUnreadMessageCount, Uow is used to query database with tenant id.

    For example;

    [UnitOfWork]
    public virtual long Save(ChatMessage message)
    {
       using (CurrentUnitOfWork.SetTenantId(message.TenantId))
       {
           return _chatMessageRepository.InsertAndGetId(message);
       }
    }
    

    This will insert message by considering message.TenantId.

    But in FindMessageAsync method no need to consider about tenant id and/or any atomic processing.

    public async Task<ChatMessage> FindMessageAsync(int id, long userId)
    {
       return await _chatMessageRepository.FirstOrDefaultAsync(m => m.Id == id && m.UserId == userId);
    }
    
  • User Avatar
    0
    ivanosw1 created

    Thank you @demirmusa I think that now I have a complete picture of the behaviour.