I am working on a Silverlight application using RIA services with Entities Framework.
Forgive me, i'm fairly new with Ria services, but how do i go about getting a list of objects from the db without doing a load operation?
Example: I have an Employees table, in this table there's a IsSupervisor flag. I want to show a list of employees in a grid with a combobox cell bound to a list of supervisors (employees where isSupervisor = true).
The problem i have is that when the list of supervisors come back, the employee list only displays supervisors.
I hope this makes sense....
It's hard to really say without seeing your code, as RIA Services is pretty darn flexible.
It sounds like you are binding a DataGrid to your DomainContext's Employee EntitySet, and then making two calls to the server, one to get all employees, then one to get supervisors. If this is the case then yes your second call can wipe out the first one (depends on how you have LoadBehavior set).
But if you are querying the db to get all employees, then you already have the supervisors on the client side. Just create a separate collection that only contains the supervisors, and bind the ComboBox to this. Something like:
private void OnEmployeesLoaded(LoadOperation<Employee> loadOp) {
if(!loadOp.HasError) {
Employees = new List<Employee>(loadOp.Entities);
Supervisors = new List<Employee>(loadOp.Entities.Where(e => e.IsSupervisor));
}
}
Related
what is the best way to load dropdown lists from reference/lookup tables for a desktop application?
the application is layed out into 3 tiers. I've built up my entities.
the front end has a form with 6 tabs. and one big save (another discussion :)
Should I load them all when the form is initially loaded? Are there any caching mechanisms I could use?
it is vb.net app on a network drive that is accessed by several users.
it's also worth noting that some reference tables may be updated. Via another form.
thanks
T
Lots of factors. One you need to populate in constructor so the data is there to populate the visual elements. Beware that just because a tab is not visible does not mean it is not loaded when you app starts.
For a static list of strings
public class Library : INotifyPropertyChanged
{
private List<string> dropDown1;
public List<string> DropDown1 { get { return dropDown1; } }
public Library()
{
// use data reader to populate dropDown1
}
}
I know this will get comments that can use something lighter than a List but List has a lot of nice features, easy syntax, and easy to populate. As a next step you could structure as a client server and use some static so the list is populated once and then shared by all. If you have more properties then substitute string with a class. For a dynamic list then in the get you grab the current data from the table on demand. In your get you could hold on to the last list and if the next request is within X seconds then return stale data. It depends on if stale data is acceptable.
There are many other approaches and I do not pretend this is the best. Just putting out a relatively simple example to get you started.
When it gets to hierarchical then things get a little more complex. There you can use ADO.NET table to store the static dependent data and then apply a filter on a view.
If its a web page you don't have to load all tabs on page load.
Desktop i think it will be more easy and it should be like that.
Only when the user click on the tab show the page and hide all the pages
associated for other tabs.
i hope all tab pages values will be on session so that user can go and come back to any tab and your Big Save at last.
Something useful related to your question i found here
http://www.syncfusion.com/FAQ/windowsforms/faq_c93c.aspx
and one more
I am building a silverlight app. My requirement is to have Users select a number of skills that apply to them, from a larger list of skills.
Tables: Candidate => CandidateSkills; SkillsCategories => Skills. I think the schema is self explanatory. The front end will show all skills (grouped into different categories), and when the candidate logs in, only his selected skills will show in the check boxes. Fairly simple.
My question: Do I bring all the Skill entities to the front end and then get the CandidateSkill entities, loop through them and set the checkboxes accordingly or is their a easier/better way?
Thanks
I recommend building a class to use as a ViewModel. The class should contain at least a property to indicate whether the item is selected, the text to present, and either the model entity itself or its key.
You can create the set of view model objects by left-joining the set of all skills to the individual candidate's skills, and setting IsSelected to the result of a non-null test on the candidate skill.
You can then bind directly to the ViewModel.
I had a similar situation (Users to Permissions instead of Candidates to Skills) once, and I used this resource as a starting point. I hope it helps.
In my case, I had a "Save" button which, upon click, would run some code-behind code to iterate through the selected items and submit them to my Web service. Without knowing the details of your data and service implementation, I'll not clutter up the post with the nitty-gritty details.
Best of luck!
Comments for Discussion
Here is a pseudo-LINQ procedure creating view models by issuing two database calls:
var userskills = database.CandidateSkills
.Where(cs => cs.UserId == someUserId)
.Select(cs => cs.SkillId)
.ToList();
var skills = from s in database.Skills
select new CandidateSkillViewModel()
{
Text = s.SkillName,
IsSelected = userskills.Contains(s.SkillId),
Value = s.SkillId
};
mylist.ItemsSource = skills;
This would give you a bindable data source. Ultimately, using this pattern, you'll have to translate selections/deselections into inserts/deletes by hand. For me, I do this in the handler for the button click. I retrieve a fresh set of candidate skills, iterate through the items of the list, and insert/delete instances of CandidateSkill as needed.
I realize that depending on a button click to resolve my viewmodel state into database operations might not be considered by purists to be complete MVVM, but it worked for me.
I hope this helps a little more.
I'd like to implement a sort of Addressbook/Contactbook using a Datagrid (or a List) and the MVVM pattern.
Something like in Outlook/Thunderbird, where you've a list of your contacts displayed with a 2-3 main fields (name surname for example), and when you double-click a contact, then you get a new modal box that displays all the details of this specific contact.
In fact my scenario is much more similar to an application that manages Customers, Orders and Products. The user would have as main view 3 datagrids showed through 3 tabs, one shows the list of Customers, one the Orders and one the Products.
Then in each view, you can Add (through opening an extra dialog), Delete (under certain conditions) an object.
Each object has a relation with another one.
For example, in a Customer instance, I've a list of Orders for that Customer and for each Order a list of Products ordered.
Since a couple of weeks/months, I'm reading a lot of stuff about MVVM pattern on the net, but somehow, I get confused. Until now, I could find any sample like this. (perhaps, I searched wrong?)
I'd like to implement something like this using the MVVM pattern.
How could I organize such an application?
Could someone help, how to structure it?
Is there a sample somewhere?
Thx in advance for your help.
Fred
1) This video helped me with understanding the basics of MVVM.
2) Search on SO for "MVVM Master Detail".
3) "Delete (under certain conditions) an object": read about Commands and Relay Commands:
private RelayCommand _delete;
public ICommand Delete
{
get
{
return _delete ?? (_delete = new RelayCommand(action => DoDelete(), condition => CanDelete));
}
}
private bool CanDelete
{
get { return true; // your condition }
}
4) "Then in each view, you can Add (through opening an extra dialog)"
"Each object has a relation with another one" - you need to let other ViewModels know of changes. Typical solution is to use Mediator pattern. Please refer to following articles:
http://sachabarber.net/?p=477
http://marlongrech.wordpress.com/2008/03/20/more-than-just-mvc-for-wpf/
http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx
Edit: just found another nice and simple MVVM sample featuring sorting filtering and list navigation:
http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/
This article about Catel includes a "Person application". It is very simple, but allows you to manage a list of contacts. Maybe it's a starting point for you.
sorry for the silly question but this is my first approach with WPF and Entity Framework.
Let's explain my scenario...
I have a master class (Customers) and a detail class (Orders), both in my EF context.
I load my master class with a LINQ-to-EF query (including Orders) and put the IEnumerable result set in an ObservableCollection, that is used as source to a ListBox.
Then I have a DataGrid where I load the orders.
Now, because the master items are in an ObservableCollection, I am able to add, delete and update an item and that automatically reflects to my ListBox.
The problem is when I need to add an Order to a Customer.
Is there a way to update only an item in the ObservableCollection without re-load all the items from the context?
This is my code (simplified)
// Loading Customers
EntitiesContext context = new EntitiesContext();
IEnumerable<Customer> customers = from c in context.Customer.Include("Orders")
select c;
ObservableCollection<Customer> oc = new ObservableCollection<Customer>(customers);
// Binding ListBox
listCustomers.ItemsSource = oc;
...
// Retrieving a Customer (1st for instance...)
Customer c = (listCustomers.Items[0] as Customer);
Order o = new Order {Customer = c.ID, Item = "xxx"};
context.AddToOrders(o);
// How can I update the ObservableCollection?
Thanks in advance,
Manuel
Part of the problem may be manually setting the association (Foreign Key) property on the Order. When adding an object to a parent object in EF, you really just do something like the following:
Order o = new Order {Item="xxx"};
c.Orders.Add(o);
context.AddToOrders(o);
In this way you create the object, add it to the collection on the local side that you want it (behind the scenes EF tracks it's creation and remembers to give it a Key and Association Key) and it adds it to the contexts over-all Orders collection.
Be sure to call save changes when you are done with that operation and it will create the FK value and save it all to the database.
Also, of note: If you have more elaborate Object Hierarchies thyan just the two classes, (which is likely) you might look into LazyLoading if you don't want to have really goofy include statements hardwired into your code. This will retrieve needed data only when it is actually requested and will keep your LINQ statements a lot more succinct.
quite an explanation here, hope someone has the patience to read it through
I'm building an application in Flex 4 that handles an ordering system. I have a small mySql database and I've written a few services in php to handle the database.
Basically the logic goes like this:
I have tables for customers, products, productGroups, orders, and orderContent
I have no problem with the CRUD management of the products, orders and customers, it is the order submission that the customer will fill in that is giving me headaches:
What I want is to display the products in dataGrids, ordered by group, which will be populated with Flex datamanagement via the php-services, and that per se is no problem. But I also want an extra column in the datagrid that the user can fill in with the amount he wishes to order of that product. This column would in theory then bind to the db table "orderContent" via the php services.
The problem is that you would need to create a new order in the database first that the data could bind to (orderContent is linked to an order in the db).
I do not want to create a new order every time a user enters the page to look at the products, rather I would like to create the order when a button is pressed and then take everything from the datagrids on the page and submit it into the database.
My idea has been to create a separate one-column datagrid, line it up next to the datagrid that contains the products and in that datagrid the user would be able to enter the amount of that product he'd like to order.
I've created a valueObject that contains the data I would need for an order:
Code:
package valueObjects
{
public class OrderAmount
{
public var productId:int;
public var productAmount:int;
public var productPrice:Number;
public function orderAmount()
{
}
}
}
My idea was to use a service to get all products from a certain group, populate an ArrayCollection with the data, then transfer each object in that ArrayCollection to an instance of the Value Object above, add the value object to another ArrayCollection that would the be used as a dataProvider for the one-column datagrid (I would only display amount which would be set to zero at first, but use the other data upon transfering it to the db)
I've tried to use the results from the automatically generated serviceResults that retrieve the products for the datagrid and put in a resultHandler that transfers the valueobjects, however this does not seem to work.
Basically my question is this: Am I approaching this thing completely wrong, or is there a way I can get it to work the way I planned?
Would I need to create a completely new service request to get the product id:s, and price to populate the one-column datagrid.
I'll post some code if that would help.
Thank you if you read this far.
Solved it by creating a Value Object class to hold all the info needed for each row in the grid and from the php service that returned all products in a group, I looped through the result and transfered the data needed into my Value Object.
I then added each Value Object into an ArrayCollection and made that the dataProvider for the dataGrid.
No need to use two grids. I forgot how logic things get when you think of datagrid data just as an ArrayCollection and forget the visual presentation of it on screen.
Put in a few itemRenderers and the whole thing is beautiful!