NHibernate - how to achieve this n:m relation? - nhibernate

I have two objects: Color and Palette. Each palette can get a range of colors assigned. That is what I am able to configure out with Fluent NHibernate without any problem.
But: the assignment of colors is only a constraint. If no color is assigned the palette should have every color which is available in the database.
My approach would be to check if Palette.Colors.Count() is 0, and if so, to fetch all the colors by ColorRepository.GetAllColors() and assign them manually to the palette.
Is there another "built-in" way?

It will be difficult (read: impossible) for NHibernate to map something that the database does not reflect.
You would get a huge headache if you assigned all available colors to the palette if no color is assigned. Before persisting you would have to empty the list, so that you do not end up with all those colors in the PaletteColorAssociation table.
The way I understand you, I see two options here:
1) Turn the whole thing around. What I mean is, do not save the colors that are assigned but those that are not assigned. Make that a PaletteColorExclusion table. Of course there are drawbacks to this: 1) If you add a new color to your Colors table, then that color will implicitly be part of every palette and 2) If your general rule is to only have a fraction of the available colors assigned to a palette, you will end up with more rows in the table that you would need.
I do not recommend that approach. It is only meant to show a way how to reflect that logic in the DB.
2) A modification of your apporach seems like the better idea: Do not put that logic within the Palette class but in your business logic classes. Example:
public ReadOnlyCollection<Color> GetColorsForPalette(Palette p)
{
if (p.Colors.Count() != 0)
{
return new ReadOnlyCollection<Color>(p.Colors);
}
else
{
return new ReadOnlyCollection<Color>(ColorRepository.GetAllColors());
}
}
I did not test that code and I put ReadOnlyCollection there to avoid the temptation of altering the collection, since it may not be the palette's colors.

Related

Blender: split object with a shape

I've got a flat object that I want to split in multiple pieces (background: I want to print it later, but the surface of my printer is not large enough). I've modeled a simple puzzle-shape:
I would like to use this shape to cut through my object, but if I use the boolean modifier, blender generates vertexes where the shape and the object intersects, but it won't cut the object since my shape got a thickness of 0:
I don't want to make my shape thicker, because otherwise it would delete something of my object...
You are able to separate the two sides of the object from each other, and then rejoin them afterwards if you need to. (This does include the use of the boolean modifier)
First, you should add the boolean modifier to the main mesh where you want it, with the 'difference' operation. Then in edit mode, as you explained before, the vertexes are created but there isn't the actual 'cut' that you were looking for.
I recreated the scenario with a plane intersecting a cube:
This is what it looks like in edit mode after having applied the boolean modifier:
Second what you can do is (after applying the boolean modifier) select the faces you want to be separated in edit mode. Then, pressing P (shortcut for separate, you can get to it by right clicking) click on 'selection' and you should have two separate objects. One of the objects will have what looks like a missing face: If you wanted two separate objects, then you just need to add a face on the object with the missing face and you can look no further. If you wanted separate parts of objects that are separate within edit mode (all together one object in object mode) then you can select the two objects and press crtl+j. Hope this helps somehwhat!
I have selected half of the cube that I want cut out (the selection does not include the face in the middle):
There are now two objects, completely seperated from each other:

Using placeholder template in core data without saving the placeholders?

I am wondering if its possible to have a core data and use a NSFetchedResultsController to display template holders; without saving the placeholder template holder to core data.
I am writing an app where a table view will have the following characteristics
Maximum amount of rows (ie: 8)
Each cell uses a partial view, a single table cell instance with clickable buttons;
A user clicking on the buttons replaces the content for each button
When a user comes to save the data it stores the content in the exact places, but crucially it should not save the blank placeholder state; but still respect the placement positioning.
IE: Lets say a user picks the right button for cell 1 and saves it;
when the the user returns to this dataset sometime in the future, it
should have the imagery and data in the correct place; but still keep
the left button for cell 1 empty (placeholder)
The reason I don't want to save the placeholder content is because I consider this data to be "dirty"; its meaningless and has no place in a dataset; its a view; not data.
Currently:
I create data using insertInManagedObjectContext but do not save it until the user has pressed save.
Each matchRow has many fighters. This relationship is a NSOrderedSet, managed by core data.
The problems I'm having are;
NSFetchedResultsController only respects saved core data items; and ignores the placeholder data.
If a user only saves one (right button) data, the NSOrderedSet sees this as the first (left button) data; which is in correct
If I strip out all the placeholder structures from the core data in a pre-save action, the effect is that the order the user wanted is now lost and it causes a crash when reloading because the size of the arrays are incorrect.
Currently the only way around this is to stop using
NSFetchedResultsController and use a NSArray; and put the placeholders into the array too, then save everything, (dirty objects) and all.
I really don't want to do that. The placeholder content isn't meant to be saved.
I've been reading a blog post on using separate classes for special cases where he has different data sources; and uses the technique to separate class for the "empty" case, and to set it as the table view’s datasource when it's empty.
Whilst this is interesting, it only solves it for an empty case; when my requirement is that the partial view should handle all states; empty data and partial data.
One idea I have is that we need another entity between our record and our match rows;
Match - MatchRowMeta - Fighter
Not sure what the relationship would be
where;
RowMeta {
record.obj (relationship)
fighters.obj (relationship)
positionID (integer) - which button has been set
}
But I don't think this is the best way to do it; it seems expensive and a lot of heavy lifting to do something relatively simple.
Thus, my question:
Is it possible to have a table view made up of template holders; where the structure is saved to core data but none of the empty (dirty) objects are saved; plus NSFetchedResultsController will respect the structure; even when its empty or partially empty?
Edit: Added entity diagram

Calling a mixin with a return value twice in the same scope

I'm building a LESS file containing a list of color variables that are used through our project. Most of these colors are simple colors (red, blue...), but I want a few of them to be automatically "computed" from the other colors. For example, I have made a mixin to automatically create a color that "stands out" from another color. Following the LESS documentation, I "simulate" a return value by defining a new variable in the mixin, so that I can use it next.
.stand-out-color(#baseColor, #rate) when (lightness(#baseColor) >= 50%) {
#standOutColor: darken(#baseColor, #rate);
}
.stand-out-color(#baseColor, #rate) when (lightness(#baseColor) < 50%) {
#standOutColor: lighten(#baseColor, #rate);
}
.stand-out-color(red, 10%);
#my-first-color: #standOutColor; // A slightly different red, perfect
.stand-out-color(blue, 10%);
#my-second-color: #standOutColor; // The same slightly different red, not the blue I expected
Unfortunately, the return value is written after the first call, and never changed afterwards.
After reading the documentation again, I got this part: "There is only one exception, variable is not copied if the caller contains a variable with the same name (that includes variables defined by another mixin call)."
So it works as it should, but I don't have any idea how to achieve what I want, which is a function I can call more than once within the same scope. As I'm defining a list of global variables, I don't think I can play with scopes to avoid this behavior. Is there any other way to achieve this?
Here's the solution, thanks to seven-phases-max's comment to my question. It solves my initial problem, which is that I needed to compute slightly darker/lighter color variables depending on another variable.
The contrast function of LESS solves my issue, by letting me specify which color to take depending if a color is "dark" or "light".
Applied to my example:
#my-first-color: contrast(#original-color, darken(#original-color, 10%), lighten(#original-color, 10%));

How to specify different editor widgets for the same column in Dojo DataGrid

I am wondering, is there an official way to specify a different widget editor for the same column in a DataGrid (different rows)?
I found dojox.grid.cells._MultipleEditor, but it is quite complicated and not officially supported.
This is for creating things like a property sheet with DataGrid.
EDIT: People seem to suggest using dgrid. However, I am not sure if dgrid has this feature. Also, unfortunately, ... drum roll... horror music... I must support IE6.
Well, there seems to be a way to do it. Doesn't seem to show much negative side effects (so far)...
Create one column for each value type, one after the other.
Tag each column with a CSS class to indicate its value type (via classes). For example: classes="multivalue int"
For each column, tag it with the correct editor widget and the appropriate constraint & options.
Put styles on each row (with onStyleRow) that correspond to each type. For example, add a type-int class to the row that has an int type.
Put in a CSS style that initially hides all the multi-valued cells:
.dojoxGridCell.multivalue { display:none; }
Un-hide all the cells with the correct type:
.dojoxGridRow.type-int .dojoxGridCell.multivalue.int
{
display:table-cell;
*display:block; /* For IE6/7 */
}
For this to work, obviously, each row must match exactly one column.
Obviously, you must set all these fields to the same property name. DataGrid allows you to do that.
Put display:none (via CSS etc.) on all the header cells of multi-valued columns except the first one. Otherwise, you'll end up with too many header cells.

How to handle ViewModel and Database in C#/WPF/MVVM App

I have a task management program with a "Urgency" field. Valid values are Int16 currently mapped to 1 (High), 2 (Medium), 3 (Low), 4 (None) and 99 (Closed). The urgency field is used to rank tasks as well as alter the look of the items in the list and detail view.
When a user is editing or adding a new task they select or view the urgency in a ComboBox. A Converter passes Strings to replace the Ints. The urgency collection is so simple I did not make it a table in the database, instead it is a, ObservableCollection(Int16) that is populated by a method.
Since the same screen may be used to view a closed task the "Closed" urgency must be in the ItemsSource but I do not want the user to be able to select it. In order to prevent the user from being able to select that item in the ComboBox but still be able to see it if the item in the database has that value should I...
Manually disable the item in the ComboBox in code or Xaml (I doubt it)
Change the Urgency collection from an Int16 to an Object with a Selectable Property that the isEnabled property of the ComboBoxItem Binds to.
Do as in 2 but also separate the urgency information into its own table in the database with a foreign key in the Tasks table
None of the above (I suspect this is the correct answer)
I ask this because this is a learning project (My first real WPF and first ever MVVM project). I know there is rarely one Right way to do something but I want to make sure I am learning in a reasonable manner since it if far harder to Unlearn bad habits
Thanks
Mike
I would favor option 2. Sounds very MVVM-stylish to me.
Option 3 would be favorable, when there are other applications or when you have reports accessing the "Urgency" field. Reason: Otherwise you will need to duplicate the knowledge of mapping between Int16 and their meaning. Move the knowledge to the database to keep it in one place.
Maybe consider Enums to make the code more expressive:
enum Urgency { High=1, Medium=2, Low=3, Closed=99 };
This way you will have something nice looking for evaluating the IsEnabled property like this:
if (urgency == Urgency.Closed) return false;
When you need to store the numeric value of the enum, you will need to make a cast to Int16 beforehand.
I think that I'd first fix this in the view. Have a TextBlock that displays "Closed", and a ComboBox that displays the other values, and then use a data trigger to set IsVisible on both depending on whether or not Urgency is 99.
I'd do this not because it's the best technical solution (it's probably not) but because it's (possibly) the best UI solution. If the user can't ever modify a closed item, it's a little misleading to display "Closed" even in a disabled ComboBox, since the ComboBox means, visually, "Here's something you can change." That it's disabled just prompts the user to wonder what he has to do to enable it. Using a TextBlock is an unambiguous way of saying "this is just how it is."