Nice Rx way of subscribing to a collection within a collection - c#

I'll describe my object model, then what I want to do.
It is a Silverlight application, and these are model objects that are bound to UI elements.
An Agreement has a collection of TradingBranch, branches can be added or removed. A branch has collection of Product.
agreement.Branches
.SelectMany(x => x.Products)
.Distinct()
These collections are driven by a matrix of branches and products. The same products can be selected by more than one branch, hence the Distinct.
Essentially I want to let the user select from a list of all the products that have been selected as available for any of the branches. I want this list to be updated when there is a change in the matrix.
So rather than having to add a CollectionChanged handler for the branches, then more handlers to listen on the Products collection, work out whether the product is already there and then have to unsubscribe when branches are removed etc etc, I was hoping there was some nice Rx syntax I could employ to say simply - "listen to this piece of LINQ" and update this other observable collection that I'm binding my ListBox to when it changes.

Despite the name similarities, IObservable and ObservableCollection are completely unrelated and unfortunately also incompatible. They have two very different models of observing a collection.
Have a look at Bindable LINQ. It attempts to define LINQ-to-ObservableCollection, such that a LINQ query on a ObservableCollection results in a ObservableCollection again. The project seems dead, though, and I haven't used the recommend replacement yet (Obtics).

Try my ObservableComputations library:
agreement.Branches
.SelectingMany(x => x.Products)
.Distincting()
agreement.Branches and Products should be of type INotifyCollectionChanged (ObservableCollection).

Related

observable collection with multiple observers with different filter criterion

I want to develop an observablecollection which can have many observers each with it's own Linq based filter criterion.
I understand that I could easily create a filtered observable collection for each observer manually. But I am more interested in an elegant design approach to keep this abstracted from the observers.
filter criterion for each observer will be loaded from a configuration based on object properties. Then the observer should subscribe to the primary collection for changes in objects based on given filter criterion.
I want to write this implementation in a generic way so it can work for any object and it's collection.
Addition to this, I want to use the same filter for notifying the changes in the the collection items itself. So the subscriber are subscribing to the changes in the elements and not the collection itself. subscriber gets notified of the change in the element only if the subscription filter is satisfied.
So for example:
I have a collection of order objects in Order List. Subscribers subscribe to my collection class based on given LINQ based criterion. Whenever any object in the collection is updated, only the subscribers which satisfy the filter criterion are notified.

Refresh entity framework collection property

I'm trying to update parts of my entity framework 6 entities, specifically collections with data from the database. Reading the data from the database the first time is fast so it should be possible to read it again quickly.
Disposing the context and creating a new one is fast, but it doesn't work for me, because doing so would lead to losses of information added by the user.
So in this case, I have a class called Evaluation, Evaluation has a many-to-many relationship through the class Amount to Classification. Classification in turn has n-1 relationship to Substance, i.e. one Substance can have multiple Classifications.
Evaluations and Amounts are handled in program A, while Classifications and Substances are handled in program B. The users frequently do updates in program B, which they must be able to see in program A, while a context is already opened.
In my user interface, the user adds Classifications to Evaluations, the user first selects a Substance from a datagrid, a second datagrid then shows the Classifications related to the selected Substance.
The substances are updated as they should, because the wpf binding is to a property that will always read from the database (using a few search parameters), so if I add or remove substances, they are added or removed in the UI as well.)
I managed to refresh Classifications as well, by binding to a property that returns:
_context.Classifications.Where(c => c.SubstanceID == SelectedSubstance.SubstanceID).ToList();
instead of just binding to the navigational property of the Substance.
In this way, additions and removals of Classifications are noticed in the UI. If I change a Classification property (that is shown in the datagrid), that change is not noticed though. And it's not a matter of the UI not updating, the View Model has the old value as well.
I can update the properties by doing:
_context.Entry(Classification).Reload(); // very slow for a single entity!
A Classification also has a n-m relationship to RiskPhrase through the entity ClassificationToRiskPhrase, these riskphrases are shown in the datagrid through a converter. At first, I wasn't able to get either the added or removed ClassificationToRiskPhrases, but doing this:
_context.Entry(Classification).Collection(c => c.ClassificationToRiskPhrases).Query().ToList());
Makes the additions appear, but if I remove a ClassificationToRiskPhrase, this isn't shown.
So my first question is: How do I update an entity so that removals in one of its collection navigational properties is noticed?
My second question is more general: What's a good way to handle partial refreshes in general, does anyone know of literature in any form which covers it? I guess it's common to keep a context alive during the time a user edits a certain entity? I know short lived contexts are prefered, but to keep all changes from being automatically saved, one has to wait before doing context.SaveChanges().

The optimal way to load referenced entities from other collections from C# and MongoDb

In my Mongo database I have two collections, let's they are A and B.
Entities of type A has, for example, a list of ids of entities of type B. This list is just a list of strings, where each string is a string representation of B's Id.
I want to reflect these reference relationships in my domain classes (and data classes at the same time) more clearly, so that I have a list of Bs inside of A, and would be able to query this list with possible filtering or ordering via LINQ.
Currently this is implemented in the following way:
I have a class MongoReference with a static event named 'MongoReferenceLoadRequested'. This event has event args that contain a type of referenced item and its id, also it has a read-write property of type Object, where the actual loaded object is supposed to be written to.
The event is fired whenever somebody wants to have an access to A's instance's list of Bs.
In my Global.asax.cs I subscribe to this event and in the handler load desired entity and assign it to event args' property. This is done to decouple application configuration, where MongoDb connection string is stored, and POCO entities classes, that do not have (and should not have) access to connection string.
How do you think, is this a correct way of achieving my goals?
Consider revisiting your document model in MongoDB. Sounds like a good candidate for having A have an array of B inside it in MongoDB.
// Document A:
{
_id: ... ,
values: [B1,B2,B3 ..]
}
This Impedance Mismatch is a legacy of RDBMS, and is due to implied relationships and the table-row kind of modeling and thinking.
See here for examples and discussion of appropriate data modeling in some common scenarios.

Consistency between C# lists

I am writing a small game engine for XNA. Right now my entity manager has a list of all entities it can update and render. Lets say, however, that I want to split this list up into 'sub lists'. Maybe some entities do not require rendering, and so we would like to separate the rendering entities from the non-rendering entities, while maintaining another list which shows ALL entities for updating. So thats 3 lists.
Is there a way to maintain consistency between lists easily? To ensure that the exact same objects are being referenced in both lists? (Not a copy of the object?). In C++ we'd just have an array of pointers to entities that render, and another for entities that dont.
The problem isn't really putting entities into these lists, its disposing them thats throwing me off. To destroy an entity, i simply set a 'dispose' bool to true in that entity. The entity manger, when looping through all entities, removes the reference to that entity if 'dispose' is set to true, and the garbage cleaner destroys the entity because there is no more references left to it. Would i have to manually go through each and every list that has something to do with Entities and remove that entity from those lists to keep things consistent?
another example of this 'problem' is with quad trees. I have a 2d game and i generate a quad tree for all the entities. The entities that lie in between 2 grid locations are in 2 branches of the tree. how would i remove it from the tree? search the entire tree for references to that entity? seems a little pointless to have a quad tree if thats the case..!
Consider instead of 3 lists, have one list and 2 queries of that list.
var list = GetYourMainList();
var renderableEntities = list.Where(item => item.IsRenderable);
var nonrenderableEntities = list.Where(item => !item.IsRenderable);
// work with the queries
foreach (var item in renderableEntities)
{
// do whatever
}
Here, you can iterate over each query, the results will stream from the main list, and any update to that list will be reflected in the query results as they are iterated. The caveat is that the list cannot be modified (as in added to or deleted from) while the queries are being iterated.
As for your disposing problem, you could implement IDisposable. For your lists, you could make a class that contains the two lists, and if you need the entire list, implement an enumerator that would enumerate over the two lists.
You could put a wrapper around your entity that knows what lists it is in so that when you want to remove it, you can remove it from all places at once relatively easily.
If you have items in multiple lists, then yes, if you want to remove them you must remove them from all lists in which they occur. A simple way to do this is to assign responsibility for removing items to the items themselves: for example, rendering items would remove themselves from the rendering items list when you call RemoveFromLists() on them.
The use of queries as described by Anthony Pegram eliminates this complexity but is inefficient if the lists are large, since they cannot be incrementally updated.
As for quadtrees, a straightforward thing to do is to have each item keep a list of quadtree nodes in which it occurs (or if you don't want to add a field to your items, use a Dictionary). It is then simple to remove them from all nodes in which they occur in constant time.
You could have your entity expose a "Disposing" event that is fired before it is disposed. Then you could have your list's Add method register an event handler with the entity being added that will remove the entity from the list before it is disposed.

refreshing a collection after a save to db

I save an item to the database, which in turn causes a side-effect which effects a collection I've already obtained from the ObjectContext.
I simply want the collection to be refreshed. (Kind of like doing ObjectContext.Refresh -- but for a collection of items). Iterating over the collection is not an option -- there may have been items added or removed in the database.
How can I accomplish this?
In such case you must execute query to populate the collection again. Moreover you must correctly configure its MergeOption.
context.Things.MergeOption = MergeOption.OverwriteChagnes;
var myThings = context.Things.Where(t => t.IsFat);
EF and other ORMs don't like side effects in database.

Categories

Resources