question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Optimize the method: Abp.Organizations.OrganizationUnitManager.ValidateOrganizationUnitAsync(OrganizationUnit organizationUnit)

See original GitHub issue

old: ` protected virtual async Task ValidateOrganizationUnitAsync(OrganizationUnit organizationUnit) { var siblings = (await FindChildrenAsync(organizationUnit.ParentId)) .Where(ou => ou.Id != organizationUnit.Id) .ToList();

        if (siblings.Any(ou => ou.DisplayName == organizationUnit.DisplayName))
        {
            throw new UserFriendlyException(L("OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName));
        }
    }

`

new: ` protected virtual async Task ValidateOrganizationUnitAsync(OrganizationUnit organizationUnit) { var siblings = (await FindChildrenAsync(organizationUnit.ParentId)) .Where(ou => ou.Id != organizationUnit.Id && ou.DisplayName == organizationUnit.DisplayName).Any();

        if (siblings)
        {
            throw new UserFriendlyException(L("OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName));
        }
    }

`

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
easyfastcommented, Sep 3, 2017

但,这个是同步方法啊。换用异步方法,不是更快吗?

1reaction
acjhcommented, Sep 3, 2017

For reference: OrganizationUnitManager.cs#L123-L133


the best: ...CountAsync(... && o.DisplayName != organizationUnit.DisplayName);

  • Any is faster than CountAsync
  • Chain .Where instead of && for clarity
  • Be careful, it should remain as ==

My suggestion:

var hasSiblingWithDuplicateDisplayName = OrganizationUnitRepository
    .Where(ou => ou.ParentId == organizationUnit.ParentId)
    .Where(ou => ou.Id != organizationUnit.Id)
    .Where(ou => ou.DisplayName == organizationUnit.DisplayName)
    .Any();

if (hasSiblingWithDuplicateDisplayName)
{
    throw new UserFriendlyException(L("OrganizationUnitDuplicateDisplayNameWarning", organizationUnit.DisplayName));
}

Note: You can use ```c# for code blocks with syntax highlighting.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class OrganizationUnitManager - ABP Documentation
Performs domain logic for Organization Units. ... Declaration. protected virtual Task ValidateOrganizationUnitAsync(OrganizationUnit organizationUnit) ...
Read more >
Organization Unit Management
The OrganizationUnitManager class can be injected and used to manage OUs. Common use cases are: Create, Update or Delete an OU; Move an...
Read more >
Extending Organization Unit #4973
I am using ASP.NET Core v5.3 jQuery. I am trying to extend the OrganizationUnit entity to include a boolean called IsOrganizer. I followed...
Read more >
What is organizational unit (OU)? | Definition from TechTarget
An organizational unit (OU) is a container within a Microsoft Active Directory domain which can hold users, groups and computers.
Read more >
Managing organizational units (OUs) - AWS Organizations
You can use organizational units (OUs) to group accounts together to administer as a single unit. This greatly simplifies the management of your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found