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 4573034
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T19:43:48+00:00 2026-05-21T19:43:48+00:00

I have two POCO types, A and B. I have a repository for each,

  • 0

I have two POCO types, A and B. I have a repository for each, Rep<A> and Rep<B>, both of which implement IRep<A> and IRep<B> served up by an IoC container (AutoFac in this case).

There are several kinds of repositories – load-on-demand from a DB (or whatever), lazy-loaded in-memory collections, cached web-service results, etc. Callers can’t tell the difference. Both Rep<A> and Rep<B> happen to be in-memory collections as A’s and B’s don’t change very much and live a long time.

One of the properties of B is an A. What I do now is, when a B is asked for its A, B gets IRep<A> to find its A and returns it. It does this every time – every request for B’s A involves IRep<A>.Find(). The upside is B’s never hold onto A’s and each request takes into account whatever the state of Rep happens to be. The downside is a lot of IoC/IRep<A> churn.

I am thinking of using the Lazy<T> pattern here so that a B asks IRep<A> once and holds onto what it got. But what happens if an A is deleted from its repository?

I am looking for a clean way for Rep<A> to notify whoever is interested when it has changed. In my example, a certain B’s A may be deleted, so I would like Rep<A> to raise an event when something is deleted, or added, etc. Rep<B> might subscribe to this event to clean up any B’s that refer to A’s that are now gone, etc. How to wire it up?

Ideally nothing changes when instantiating a Rep<A>. It should have no idea who listens, and A’s might be manipulated all day long without ever firing up a Rep.

But when Rep<B> is born it needs a way to subscribe to Rep<A>’s event. There might not be a Rep<A> alive yet, but surely there will be once a B is asked for its A, so it seems ok to fire up a Rep<A>.

In essense, when Rep<B> is instantiated, it want it to register itself with Rep<A> for the event notification. I don’t want to pollute the IRep<T> interface becaue this shouldn’t matter to anyone outside the Repository layer. And other types of repositories might not have to worry about this at all.

Does this make any sense?

  • 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-21T19:43:49+00:00Added an answer on May 21, 2026 at 7:43 pm

    What if you made the Rep<A> return an “observable” object that can evaluate to an A, and also has a subscribable event that is raised when something about that A changes? Just a thought. This way, you don’t have to have the handlers check to make sure that their A changed; if the event they’re listening for is fired, it concerns their instance and not any other.

    You might code it as follows:

    public class Observable<T>:IDisposable
    {
       private T instance;
       public T Instance
       {
          get{return instance;}
          set{
             instance = value;
             var handlers = ReferenceChanged;
             if(handlers != null) handlers(this, instance);
       }
    
       public static implicit operator T(Observable<T> obs)
       {
          return obs.Instance;
        }
    
       //DO NOT attach anonymous delegates or lambdas to this event, or you'll cause a leak
       public event EventHandler<T> ReferenceChanged;
    
       public void Dispose()
       {
          var handlers = ReferenceChanged;
          if(handlers != null) handlers(this, null);
          foreach(var handler in handlers) ReferenceChanged -= handler;
       }
    }
    
    public class Rep<T>
    {
       private Dictionary<T, Observable<T>> observableDictionary = new Dictionary<T, Observable<T>>();
    
       ...
       public Observable<T> GetObservableFactory(Predicate<T> criteria)
       {
          //criteria should test only uniquely-identifying information
          if(observableDictionary.Keys.Any(criteria))
             return observableDictionary[observableDictionary.Keys.First(criteria)];
          else
          {
             //TODO: get object from source according to criteria and set to variable queryResult
             var observable = new Observable<T>{Instance = queryResult};
             observableDictionary.Add(queryResult, observable);
             return observable;
          }
       }
    }
    
    ...
    
    var observableA = myRepA.GetObservable(myCriteria);
    observableA.ReferenceChanged += DoSomethingWhenReferenceChanges;
    

    Now, the consuming code will be notified if the internal reference is changed, or the observable is disposed (which also disposes of the internal reference). To have the observable also notify consuming code if child references of As change, A must itself be observable, firing an event handled by Observable<T> which will “bubble” it through either ReferenceChanged or a more specific handler such as InstanceDataChanged,(or whatever you want to call it).

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

Sidebar

Related Questions

I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two webapplication, one is a simple authenticationsite which can authenticate the logged
I have two insert statements, almost exactly the same, which run in two different
I have a code-first, POCO project in which I am trying to adjust an
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two elements: <input a> <input b onclick=...> When b is clicked, I
I have two identical tables and need to copy rows from table to another.
I have two classes, and want to include a static instance of one class
I have two threads, one needs to poll a bunch of separate static resources

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.