Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3679244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T03:26:11+00:00 2026-05-19T03:26:11+00:00

this is surely a basic question for most of you but it still giving

  • 0

this is surely a basic question for most of you but it still giving me headache when I think about it.

I have a repository class that takes a domain name to be instanciated :

public class RepositoryUserAD :  IRepositoryUserAD , IDisposable
{
   PrincipalContext context;

   public RepositoryUserAD(string domainName)
   { 
    if (string.IsNullOrEmpty(domainName))
       throw new Exception("the domainName cannot be null or empty");
    DomainName = domainName;
    context = new PrincipalContext(ContextType.Domain, DomainName);
   }

   public UserPrincipal GetUser(string username)
   {
    UserPrincipal foundUser = null;
    foundUser = UserPrincipal.FindByIdentity(context, username);
    return foundUser;
   }

   public void Dispose()
   {
    context.Dispose();
   }
}

And here is my problem. If I work like this it is ok but I do not like to have my context opened with the class and closed on the dispose of the class.
I can also use a using block but then I am facing another problem because I lose my reference to the context and thus to the object, or at least to the properties I did not get first.

My architecture is the following

Repository r = new Repository();
Service s = new Service(r);

I am torn in two because in my general approach, I would have liked to be able to filter my query in the service and ask the repository to really fetch the data. But here with the AD, I cannot, open and close my connection at the Repository level, or I losse the flexibility and the repository is fetching everything.

Everything is not clear because it is not clear either in my head, I just hope someone might show me one way out of this s***.

Thanks for your support,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T03:26:12+00:00Added an answer on May 19, 2026 at 3:26 am

    I’m assuming there’s some reason you don’t just create & destroy the context for each method, efficiency?

    I would create a context factory that caches contexts somehow, e.g.

     public class ContextFactory {
           private List<PrincipalContext> contexts = new List<PrincipalContext>();
           public PrincipalContext GetPrincipalContext(ContextType contextType, string domainName)
           {
               PrincipalContext existingContext = contexts.First(item=>item.ContextType==contextType && 
                  item.DomainName == domainName);
               if (existingContext == null) {
                   existingContext = new PrincipalContext(contextType,domainName);
                   contexts.Add(existingContext);
               }
               return(existingContext);
            }
        }
        public void Dispose()
        {
            foreach (PrincipalContext context in contexts) {
                context.Dispose();
            }
         } 
    }
    

    Then in whatever scope you want to use this, create a new instance of the class and use it to get a context, and dispose of it at the end. If this was a web app, it would be trickier to use it beyond the scope of a single page, but you could create (e.g.) a session cache of contexts, and have the object also periodically dispose of them if unused for some period of time. Also this code will have a race condition so you need to deal with that but this is the basic idea. This is essentially similar to the way connection pooling works.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Surely this is possible? I have been hunting through PyQt tutorials and documentation but
Surely there is some kind of abstraction that allows for this? This is essentially
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
Surely there is a better way to do this? results = [] if not
This is a bit of a long shot, but if anyone can figure it
This is kinda oddball, but I was poking around with the GNU assembler today
This past summer I was developing a basic ASP.NET/SQL Server CRUD app, and unit
I have a DataList that has a collection of People bound to it, with
This is starting to vex me. I recently decided to clear out my FTP,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.