This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Partial classes are a must to extend the model possibilities. If I understand well the concept you can add there additional properties and methods and they will be preserved and extend the model. Now, if I add there a property, is it part of the model and may I use it as a "normal" property in queries? If so, why I can't see it displayed in the model? It is like the ugly duck, and the model don't recognize it. Regards. |
|
|
Correct, properties and methods you add in the partial class are preserved and do extend the model. If you add a persistent field in a partial class, then you may use it as a "normal" property in queries. Transient fields, and properties without a backing field (such as computed properties), may NOT be used in queries because they do not map to the database. Fields added in partial classes are not displayed in the designer because the flow from the design surface to the code is one-way. That is, the designer holds one representation (the .lsmodel file, which is XML under the covers) and generates code from that lsmodel; the designer does NOT read your C# or VB code and generate a visualisation from it. In terms of built-in designers, it is like the LINQ to SQL designer, not like the Class Diagrams tool. If you need to write a field in the partial class for some reason, but you also want it to appear on the designer, then you can add a property to the designer and set its Generation option to None. This tells the designer to display the property and include it in database sync, but not to generate code (field and wrapper property) for it. You can then write the code for that property yourself in your partial class. |
|
|
A very complete answer. Thanks for your insight. |
|
|
Mmhhh, so it's preferible to create the properties in the designer (unless a special issue) and to reserve partial classes for methods and transient properties? And if you add methods you can use it normal, isn't it? May you declare a class or method as static? |
|
|
Correct. Using the designer to create persistent properties is the best way to ensure correctness of the LightSpeed model and consistency with the database. Partial classes are generally best reserved for things which LightSpeed doesn't care about (but you do): * Computed properties (by which I mean "computed in code" rather than computed columns in the database) * Transient fields (and their associated properties) * Domain methods * Customising setter logic The first three are "non-invasive" in that they're additional to anything the designer generates. The last one is a bit different because normally the designer generates the property setter, and of course you can't redeclare the same setter in the partial class. So if you need to customise setter logic, you must set the property's Generation option to FieldOnly (or None) so that the designer does not generate the property code: this then leaves you free to implement your own property with a custom getter and setter in the partial class. You can declare a method in a partial class as static, but you cannot declare the partial class static, because a static class cannot derive from another class (and all entity classes are declared in the generated code as deriving from Entity<TId>). (It would make no sense to declare an entity class as static anyway because the whole point of having an entity class is to instantiate it!) |
|
|
This is a great and very concise answer. You must be a teacher, you have the gift. Thank you. |
|
|
Hi Ivan, Are there any plans to (optionally) allow the generator to create setters something like this: private string _surname; public string Surname { get { return _surname; } set { Set(ref _surname, value, OnSurnameChanged); } // method-group parameter. Optional. Called by Set if present } partial void OnSurnameChanged(string oldValue, string newValue); // for developer to implement in partial class Perhaps this could be turned on through a new Generation Option. John |
|
|
No, but you could implement something like this through a custom template. You wouldn't be able to change the signature of Set itself, but you could certainly add a partial method call. Here's some completely and utterly untested code to get you started:
See http://www.mindscapehq.com/documentation/lightspeed/Working-with-Models-in-the-Visual-Designer/Customising-the-Generated-Code for info about setting up custom templates. |
|
|
Nice! Thanks. |
|