As an example, say I have a single domain object with 100 properties. In my UI I need complex validation of the style:
If A = 1, show controls B, C, D. B is required, C is not, and D is not required must be less than 30 if it is populated.
If A = 2, show controls B, D, E. B is not required, D is required but has no limits, and E is not required.
If A = 3, show controls B, E, F. B is required and must be more than 10, E is required, F is not required.
If B = 3 and F = 5, then show control G, but only when A = 3.
You can see my problem here. The relationships between the properties are hideously complex, with validation changing dependant on earlier values and in combination with other values.
How have people modelled and handled this in the past? Validation does not need to very often, but a config/xml-based solution would probably be best.
You could project this gigantic domain object into smaller objects containing only the subsets of properties needed and name them according to the scenario they are describing. It's creating some sort of ViewModel if you will.
There has to be a specific use case for each case of A = "X" or B = "Y" etc. If you are splitting things up you can define validation rules per ViewModel. Those ViewModels could also contain the visible/hidden settings for your controls.
Related
I have a data model in which entity A contains references to two other entities, B and C. If either B or C is deleted, I want A to be deleted.
When creating A, it's possible to name either B or C as its parent. Is it also possible to name both B and C as its parents so that if either B or C is deleted, A is also deleted?
In more concrete terms, say search results, a result might have both a category and a region, say a web page about birds in North America. The result is stored with a reference to its category and region. Later, you want to delete the category birds and you want the result also deleted. Likewise, you delete the region North America and want the result deleted.
I hate to go on at such length about such a trivial scenario. But it doesn't seem to be covered in any of the Datastore documentation. What am I missing? Is it a basically flawed data model?
Single-parent limitation:
A child can have only one parent in Datastore. In other words, A can only be a child of B OR C, not both. Of course, a parent can have multiple children, though.
Alternative:
You can use a KeyProperty with repeated=True argument and store many Entity keys on it. In Python, this would be like this:
class A(ndb.Model):
associated_with = ndb.KeyProperty(repeated=True)
some_other_property = ndb.StringProperty()
a_entity = A(
associated_with = [b_key, c_key],
some_other_property = 'any value'
)
a_entity.put()
Automatically triggering deletes:
Datastore doesn't offer this functionality out of the box, but you can mimic it in your application. Just one idea for implementing in Python, for example, you could extend the Model class with your own delete method (haven't tested this code, it's just for illustration):
class A(ndb.Model):
associated_with = ndb.KeyProperty(repeated=True)
some_other_property = ndb.StringProperty()
def delete_ext(entity): # entity object
if entity.associated_with:
for associated in entity.associated_with:
associated.delete()
entity.key.delete()
You may want to wrap all the deletes in a transaction. Beware that a single transaction can operate on up to 25 entity groups.
Assume I have 3 different classes, A, B & C. C is designed to have a list of As and Bs.
If load a list of As and Bs and keep them attached, how can I set them as lists in a transient C object and persisting the transient object ?
The problem is that each of As and Bs lists are managed by different PersistenceManagers. So if I try to persist a transient C object with a list of As (attached, managed by a PersistenceManager) and a list of Bs (attached, managed by a second PersistenceManager), I'll get an exception saying that the As and Bs are being managed by a different PersistenceManager.
Is there an efficient way to resolve this instead of having to reload all the lists again by the one PersistenceManager responsible for persisting the C object ?
I have a table for ticket type, (like new,update,escalate), and they have subtypes (for example new have around 5 subtypes eg. server,access etc).
Now every subtype have different info to be stored..(subtype server needs start date, end date, server name and access needs customer id,access type,confirmation attachment etc).
Basically, every subtype will require diff no.of info... so i want to ask , whether i should create diff table for each subtype...for Server i create a table with 4 column and for access i create a table with 7 col.
Chances are low new subtypes will be required once deployed, but still a possibility, so a new table will be created...
Is going this way the correct thing to do, or any other method is there?
Whether you should create a new table for each subtype depends on the nature of the inheritence. The following rule of thumb applies:
MANDATORY; AND: 1 Table for superclass + all subclasses
MANDATORY; OR: 1 Table for each subclass
OPTIONAL; AND: 1 Table for the superclass, 1 table for all subclasses
OPTIONAL; OR: 1 Table for superclass, 1 table for each subclass
MANDATORY means: every member of the superclass must be a member of a subclass.
AND means: a member of the superclass can be several subtypes
Calculated example: let's say you have a model that contains 1 superclass and 3 subclasses.
The total number of tables you would get for each of the four types of inheritence is respectively: 1, 3, 2, 4
I have a unique sql database setup in which I'm using nested sets to determine the hierarchy. Here's an example of it:
HierarchyID = 1, HierarchyText = "Contract", HierarchyLeft = 1,
HierarchyRight = 54
HierarchyID = 2, HierarhcyText = "Part 1...", HierarchyLeft = 2, HierarchyRight = 41
HierarchyID = 3, HierarchyText = "Part 2...", HierarchyLeft = 42,
HierarchyRight = 45
HierarchyID = 8, HierarchyText = "General Provisions",
HierarchyLeft = 3, HierarchyRight = 40
The idea is that the children (or subcategories) are contained within the parent's LEFT and RIGHT values. So, for instance, "General Provisions" is a child of "Part 1..." which is a child of "Contract". "Part 1..." and "Part 2" are siblings of each other since they don't contain each other going by the left and right values.
The question I have is this: Is there a way to use MVC3 with Razor Engine to create a tree and display it pulling from this database? The only examples I've seen have been databases that have each node in the database pointing to its parent. If it's possible, could you provide sample code for the CONTROLLER and VIEW? I'd appreciate that. Also, I'm able to pull the hierarchy names by level. So, I'd appreciate an example in which I could feed the tree control a level at a time. For instance, feed the top level... when the user clicks on it, feed the next leve, etc. down the tree. I would really appreciate any help!
You seem to be using the Nested Set Model to define hierarchical data. Personally I would use recursive display templates once the data is fetched from the database into a view model to display the tree. Here's an example in which I illustrated similar concepts in action which you could adapt to meet your specific requirements.
We have a series of drop down controls that determine the sort order of columns. The problem we are having is when the user selects a column as the 2nd column the other dropdown lists need to have their values changed so that there is only one "2nd".
Column A [1]
Column B [2]
Column C [3]
Column D [4]
Column E [5]
In the list above, when you change Column D to [2], Column B becomes [3], C becomes [4], etc. I can manage it on the server side but I was wondering if anybody had some clues how to do this on the client side with javascript.
Look at Javascript toolkits like Scriptaculous for client side reordering.
You add your elements as "Sortables" and code your own callbacks to execute when the items are dragged, then dropped -- such as sending an asynchronous request to the server to persist the new order.
Here is a full tutorial on creating sortable lists with Scriptaculous and PHP. For ASP, the client side code will be slightly different, but the process will be similar.
On a note on JavaScript frameworks; I highly recommend jQuery.