I am trying to get active directory groups with their users, all while disposing of all IDisposable
assets when they reach the end of their usefulness. The groups and users are passed into a method that extracts certain properties and adds them to ExpandoObjects
. This requires a decent number of nested using
blocks.
Since the method is private, and most of the variables only exist in the using
blocks, I gave the variables very short names. Naming them properly as groupPrincipal
or principalSearchResult
would involve breaking even more lines to fit the long names.
private IEnumerable<ExpandoObject> GetOuGroupsUsersData(
PrincipalContext pc)
{
var data = new List<ExpandoObject>();
using (var gp = new GroupPrincipal(pc))
{
using (var ps = new PrincipalSearcher(gp))
{
using (var psr = ps.FindAll())
{
foreach (var g in psr.GetGroupPrincipals())
{
using (var gpsr = g.GetMembers())
{
data.AddRange(
gpsr.GetUserPrincipals().Select(
u => GenerateData(g, u))
.Cast<ExpandoObject>());
}
}
}
}
}
return data;
}
Is there anything I can do to reduce the number of using
blocks? Am I even disposing of everything properly?
foreach (var groupPrincipal in principalSearchResult.GetGroupPrincipals())
is indented six times and with the names spelled out like that, is 98 chars long. \$\endgroup\$