Populate TreeView From Entity Framework - wpf

I saw this question many times but funny enough I didn't understand any answer given (and it seems like people who asked this question didn't understand the answer either because they didn't accept any answer).
I have a to-do list and in an entity of a to-do, you can have another to-do with no limit, you can have 100 layers of parents.
My way of solving this is that in my database (using EF6), I have an int column (which is called parent) that represents the ID of a parent which can be null in case it's the first level (has no parent).
I want to populate that into a TreeView in a WPF application - what would be the shortest and simplest way to do that?

what would be the shortest and simplest way to do that?
Lazy Loading.
The alternatives are
Eager Loading all ToDo entities.
Loading with a Raw SQL query that implements the recursive logic. EG in SQL Server a Recursive CTE.

Related

How does the ExtJS4 Column class work?

I wrote my own sub-class, for some selection stuff and paging etc. But those all worked with the renderer(), which required them to return a string.
This worked fine, I created some image-tags and returned those.
But how does this class work in detail?
I want a sub-class which displays a chart in a cell, which should technically be no problem, but the renderer() doesn't seem the right place for it, since it only works for strings.
Does the Class get instantiated for every row? or is it really just one instance for a column, which methods get called for every row with the needed data and the instances don't hold any state about the rows?
The renderer() mechanism is actually implemented in Ext.grid.column.Column, for which there is one per column.
As you have mentioned, the renderer() function returns a string, which could be an HTML string (which could be rather complex - have a look at the templates used by ExtJs for the standard columns). But you cannot return a component (chart).
To the best of my knowledge (based on my own understanding and replies to similar questions), ExtJs does not offer a straight-forward way to render components within grid cells. If you really think about it - you are asking a grid for much more than its intended role. It was designed to present records per raw, with the addition of simple user interaction facilities, like checkboxes.
But what you are really asking for is more of a way to layout charts, for which problem i suggest you look into the Table layout.
Alternatively you should be able to render a chart into a dom element, which will be defined in your own custom column template. But I will consider this to be an involved task.

Creating a Silverlight, drag & drag enabled query builder

what would be the best components to create a query builder? I imagine I'll need some sort of grid control. 30x30 cells perhaps. I would also love to have a snap-to-grid functionality as well. I'll be parsing the contents of the grid to create the queries so if I can iterate through the cells it would be a great help to. I'd be more than happy to post a sample project for anyone else to use if you all could help me with the base requirements. Thanks!
If you are already using the Telerik components for Silverlight, what about the RadExpressionEditor or RadDataFilter don't fit the requirements for what you're looking to do? Just curious as they are meant to handle this type of scenario.
ExpressionEditor allows for typing, which may not be the ideal if you're looking for a less error-prone scenario (aka, you don't trust users to write queries):
http://demos.telerik.com/silverlight/#ExpressionEditor/FilteringGridView
Otherwise DataFilter lets users select options based on the items and their respective types found in your collection, so it is much harder for a difficult user to break ;D :
http://demos.telerik.com/silverlight/#DataFilter/FirstLook
Is neither of those fit the bill, can you post more info in regards to your scenario/what you're looking to accomplish (and why you're looking at a 30x30 grid setup for the basis) and we might be able to brainstorm something good up. :)
-Evan

Loading large graphs and detaching them

I am developing an application that uses Entity Framework and WPF with MVVM design pattern. I have a number of entities, but to keep it simple, lets work with just a subset of them. Companies which can contain Contacts, Addresses, and PhoneNumbers; the aforementioned Contacts which contain Addresses, PhoneNumbers and Shipments.
Originally I was loading these entities from the database and leaving the DataContext open to use again later. This presented problems from a concurrency aspect as any of the objects that got updated may very well not be represented correctly in other parts of the program since those parts had not been updated.
I am trying something new. In my MainViewModel (which can easily be accessed from every other ViewModel) I have several ObservableCollections (my generic collection of choice when working with WPF Databound objects) which I intent to temporarily store my entities. Also there are several methods which will take care of adding entities to the context, attaching, detaching, saving changes etc. In my Record Collection displaying ViewModels (for instance the CompaniesViewModel) I have a backgroundworker load the entities, detach them, and store them in the Collections in MainViewModel. Unfortunately, I apparently have to create explicit queries such as:
Dim results = From Comp As Company In RVShipContext.Companies _
.Include("Contacts.PhoneNumbers") _
.Include("Addresses") _
.Include("PhoneNumbers") Select Comp
Mind you, that is not so much of a problem, except when it comes time to load up a Contact and now I have to go back (attach, query, detach) to get the Addresses, and Shipments. Now, I don't have a real problem with that, but some of these graphs are going to be several layers deep. A Shimpent contains one or more Packages contains one or more DiskSets contains several disks.
Is there a way to load large graphs easily when using detached entities?
Is there a way to use LazyLoading effectively when using detached entities?
Is keeping my entities in a central set of collections (accessible to other parts of the program) a good idea or should I abandon it before I spend an inordinate amount of time setting up infrastructure for it?
Any help would be greatly appreciated!
You should abandon your current approach. I don't understand WPF an VMMV but in case of MVP (model-view-presenter) you should use one ObjectContext / DbContext instance per Presenter and keep your entities attached. Check this article - it is about NHibernate but principle is the same. Similar approach should be used with VMMV.
The complexity of your query looks like you are trying to load everything to shared context which is very problematic solution. Especially detaching entities in stateful application like WPF app looks like big overhead and a lot of work to do when attaching changes back to context instead of using attached entities and let the context track changes for you. Also keeping entities attached solves the problem with lazy loading.

Wpf Tree View Searching

How do i search for a node in very large treeview , i am using on-demand loading of nodes?
Your question is a little light on details. For example, I would be curious to know if this is a user-specified search and what the search criteria would look like. In any case, I'll take a stab at giving you a general answer to your general question:
WPF makes it easier than ever to base the view on a data model structure of your own choosing, rather than the other way around.
I would suggest creating an object model that represents the underlying data (you may have this already) and "bind" it to the tree view via data templates (specifically the HierarchicalDataTemplate). Define your search in terms of the data structure, not the view.

Retrieving data for a sidebar

In CakePHP I have a layout created and named default.ctp. In that layout I have a sidebar with some blocks and there're some statistics taken from the database.
My solution: I just created model called Sidebar.php and there're some functions, then I set up data in controller to display it in layout. Is this the best solution? As far I know, I will have to re-set every data in every controller, so need suggestions how to solve that.
Bear in mind that this is coming from a 10,000' level - I know nothing of your particular circumstances, but IMO it's not the best solution. I say that because you've created a model that represents a presentation component. If it were me, I'd probably look at using an element for display. Displaying dynamic components gets a little dodgy, but can be done without violating the MVC "covenant".
Your models should represent your domain entities (you've mentioned nothing about what your stats represent, so I won't offer any specific examples), not how they're presented.

Resources