Table Per Hierarchy based on fields in the Navigation Property - entity-framework-4

I have an entity which has a navigation property. I would like to define derived entities from the base entity using a field in the navigation property as the discriminator. Is this possible. I could not find a way to do this in the designer.

No it is not possible with navigation property. It should be possible to map two tables in 1:1 relation into single entity. You can try to combine it with table per hiearchy mapping. I'm interested if it works but i don't have chance to try it now.

Related

How to make EF detect navigation properties on a view?

I'm using database first with EF 6 and when I add a SQL view it doesn't generate any navigation properties.
Currently we use a hack where we have a table with the same columns and relations as the view which we rename to the view's name when we want to regenerate the EF entities, however this is obviously far from ideal.

iOS - Dynamically Create Controls in MVVM

I have a requirement to create controls dynamically at run-time based on config retrieved from server. I'm also trying to make use of MVVM pattern.
To keep example simple, lets say I have to create N number of UILabels in a View, each with its own settings (color, font, etc.) all based on config from server.
First thing that comes to mind, is to have a listUILabels property in my ViewModel, and have the ViewModel be responsible for creation of UILabel objects and setting their attributes / properties (color, font, etc).
The View would then iterate through each UILabel in viewModel.listUILabels adding each one via self.view.addSubview()
However I read in some examples, that the ViewModel should not reference UIKit, instead should just provide data, properties & enums for the View (feel free to comment on this).
To adhere to MVVM rules, how should I partition my logic, what goes inside the View and what goes inside the ViewModel in this case?
In my opinion your View class should contain the logic for creation of UILables and setting up its UI behavior based on the config settings.
ViewModel should provide the interfaces (properties) to interact with the underlying data which should update the View when data changes in your data source (model) and data source (model) should be updated when the data changes based on user interaction (if any).
Basically the ViewModel should be as independent on the View as possible. It should be a representation of data that are related and logically belong on a single page, but it should not be dependent on the way the view itself is implemented or represented. Ideally it should be possible to change the view and its layout without having to modify the ViewModel at all.
So your best course of action would be not to include a list of UILabels in the ViewModel and instead put a list of custom classes, that will store the "data" that you want to surface on the view. Because in this case you are dealing with UI related data, there should be no problem including color or font attributes as properties of these custom classes.
Then in the view itself you can observe this list and dynamically create appropriate controls (UILabels) based on the provided data.

iOS Container ViewController data flow

My objective is to show dealer locations in two different ways: a map and a tableview.
I have a container view controller (DealersViewController) which contains two child view controllers: DealersMapViewController and DealersListViewController. The user can switch between the VCs with a UISegmentedControl placed on the navigation bar. Tapping on a map annotation or a tableview cell will push a DealersDetailViewController.
The switching is already implemented (using code from Changing view controller when Segmented Control changes) and seems to work fine, as does the pushing of the detail.
What I would like to improve is the flow of data between container and children. The dealer locations are downloaded (JSON) from the internet in the parent and on completion an NSArray *locations property is set on both the map VC and the list VC. This array will contain dictionary objects created automatically by AFNetworking, each with location data (each location dictionary will have a title, subtitle, latitude, longitude to conform to MKAnnotation protocol, but also other things like image and description etc).
My question is: How can I be sure that the container VC and both child VCs 'agree' on how location data is structured? Theoretically, if someone wanted to develop another child view controller to add to my container that shows dealer locations in a collection view for example, how can he formally know how to expect data.
Apple says: "If the container needs the child to declare methods or properties, it should define a protocol to enforce this:". I could force the children to declare the locations property but the array could contain anything…
Maybe the parent could be a datasource for the children? I haven't tried this approach yet.
I am probably overcomplicating things but my objective is also to learn how to properly create reusable components and also practice using stuff like custom protocols / delegates and design patterns in general.
Thanks.
If I understand correctly, your issue is structuring data so that all of your controllers have the same understanding of it.
Probably the best way to go around it is to create a custom location class, and have the JSON deserialize into an instance of that class and then just pass it around as you see fit. There's quite a few ways how to go around deserializing, but JSON Model is a nice example of how to handle it automatically.
Tl;dr: convert your JSON dictionaries to custom classes and then pass them to your child view controllers, via properties or delegates, whichever you find more convenient.

Should I use a protocol or category with associative objects to give my child view controllers properties?

Im building a custom view controller container and im figuring out which to use to give my children view controllers properties, similarly like UINavigationController grabs a view ontroller's title property, and left and right bar button item. What are the pros and cons of each? Note, i have seen the category method used more such as in te excellent View Deck Controller (https://github.com/Inferis/ViewDeck) and a bunch of other components
EDIT
viewDeck and other controllers use associative objects in conjunction with the category to pull this off.
I'd recommend not using a category: there are a number of XCode/LLVM compiler issues with adding libraries/frameworks that use categories to XCode projects (see https://developer.apple.com/library/mac/#qa/qa2006/qa1490.html). Protocols work cleanly for this sort of thing, in my opinion.
I'm not sure that I would use either. I think I would create a base view controller class with the properties, and then have all your child view controllers inherit from this class. Categories can contain methods but not storage, so no ivars or properties (that have a backing ivar).

Load up entity from db rather than pulling data directly from db

I created an iOS app that allowed me to enter data into database and then display in tableview (using NSFetchedResultsController & tableView cellForRowAtIndexPath). This worked. Now, I added 3 one-to-many relationships and another entity and I need to load up the entity (on the one side of the one-to-many) from the db instead of pulling it directly from the db so that the data can be used in the relationships. Do I still use NSFetchedResultsController?
relationshipEntity2 is the one-to-many relationship between Entity1 & Entity2. The many point to Entity2
I know I'm supposed to use:
Entity2 *entity2 = [[self.entity1.relationshipEntity2 allObjects]
objectAtIndex:indexPath.row];
I am new to iOS development and even newer to Core Data but I must learn it. Any bit of help or pointing to a book or tutorial I haven't come across yet would be greatly appreciated. I haven't had much luck finding anything that does what I'm supposed to be doing.
Thanks and have a great week!
-------added for more description on project-------
I'm given the task of having a View Controller with a 3-part segmented button. There is also an add UIButton that pops up a view (bringing subview to the front, not a popover segue) with a UITextField for input to add to the table view on the view controller. There are 2 entities and three 1-to-many relationships. There is 1 relationship for each button on the segmented button. If the user, has the first part of the segmented button selected, adding a value to the popup textbox, should only add it to the table view seen when the first segmented button is selected. I have the CoreDataGeneratedAccessors created. I had this project saving data to database and fetching data to present in the table view but that was before the segmented part was added. Now I have to figure out out to separate the data into "collections" and then show all of say the first collection when the first segment of the segmented button is chosen. I'm just learning and just figured it out without the segmented part and now it's changed on me. I've worked with relational databases but in iOS it seems like it's new again.
Your question is by no means clear. You are not describing any problem, nor do you explain intelligibly what you want to accomplish.
Trying to infer your meaning: yes, you would continue to use your fetchedResultsController. This controller gives you the right object for each indexPath. When building your cell contents, you can easily get to the relationship entities with entity.relationship.
The code above will not work because entity.relationship returns an NSSet which is an unordered group of objects. (The additional allObjects does nothing and is redundant.) Therefore, objectAtIndex will not work (this only works for NSArray objects).
Hope this helps.

Resources