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
|
Hello, Consider the following class library that I'd like to map to a database.
Trying to map that to LightSpeed requires that I subclass Permission and AccessRule for every single type of resource (aka Blog and Post). Is there a way in LightSpeed that I can define those as "generic" entities and have LightSpeed resolve them accordingly? If so, where can I find information about it? Thanks, Werner |
|
|
No, I don't believe that's possible. I think the closest you could get would to to have generic interfaces, and implement those on the entity classes. |
|
|
Hi Ivan, The problem with that approach is a proliferation of types in my data layer. Supporting generics may reduce code, improve maintenance, reduce errors and simplify my schema. I did an little experiment to see how it mat work. Consider the following non generic version:
I can add and view the data in this model, however can't delete it (see this post). Now if I was to generalize it, the model may look as follows:
This reduces the number of classes in this simple model by 2. Imagine I had 60 classes that had some relationship with permissions, then I would reduce the number of classes by at least 60. Imagine the number of entities I can save if I 10 or so entities that could be "flagged", supports "voting", "comments", "marked as read" or "auditing". The savings in additional types are rather huge. To support this doesn't seem complicated based on how LightSpeed works today. If it encounters a generic type that isn't abstract, it can simply use the type name as the table and generic argument as the discriminator. Thus, "Permission Any change you guys can consider something like this in the near future? It would greatly simplify data access layers and enable certain models to be reused across applications, which is otherwise not that reusable. Thanks, Werner |
|
|
One caveat is that many people eschew this kind of design because it doesn't play nicely with foreign keys. In your Permission table, the ResourceId column can't have a foreign key constraint because it could point to either the Post or Blog table. I'm not sure whether this is something that concerns you, though, as the same thing occurs the 'inheritance' version of your model too. That consideration aside, this does seem like a nice way to avoid hierarchies where you have a utility entity that you want to be attachable to a wide variety of other entities. I've committed a preliminary implementation for you to try out, which will be in the next nightly build. Just apply the new Some caveats and extra info:
If this meets your needs, or if it needs only minor enhancements, then let us know. If it looks like it's going to require major enhancements or fixes to be useful in a real-world scenario, we may have to back it out and come back to it later, as we don't have huge resources to invest in this feature right now. We look forward to your feedback! |
|
|
Thanks Ivan. I'll check it out tomorrow and see how well it works. I'm more of the pragmatic type when using foreign keys and other database features. I'll monitor performance and resource utilization and decide whether it is acceptable. I'm more than happy to live with the constraints. Is the following scenario supported? You don't explicitly say. It is not a train smash if it is not.
One way to get around the foreign key issue is to allow one to specify a "template" for table names, such as:
So that means Werner |
|
|
We have not tested the 'non-generic base class' scenario. I think it would probably work as long as the base class contained only scalar fields, but I'm not making any promises! Give it a try and if there are problems let us know -- if we can see a quick fix we will make it, but if it looks like it will require major work then we will defer it since it is not critical for you. Generic classes mapping to multiple tables is an interesting idea and we could probably explore that at some future date. I think we could do that without breaking compatibility with the current implementation -- we would probably add an option to the DiscriminatedGenericEntityAttribute to specify single table or table per type, similar to the way the InheritanceMappingAttribute selects between single and class table inheritance. But I believe you wanted to use a single table, so we are unlikely to put effort into this unless a customer specifically asks for it. Thanks for the suggestion though -- we will certainly keep it in mind! |
|
|
Howzit, This feature has proven invaluable, especially when implementing "infrastructure" like tables, like that used for security while limiting the classes and tables in my system. However, I'm having a small problem. Consider the following:
How would I add a property Principal on the Permission entity? When I do, LightSpeed complains that it can't resolve the association. Creating a non-generic permission table (with a Principal relationship) and inheriting the generic "Permission" from it also gives me issues. The question I'm trying to answer is given a resource, "who" [aka principal] has specific access to a resource. The idea is to say Bob (user), Sam (user), MyApp (service) has read access to resource with if 123, Tim has write access and Bill owns the resource. Werner |
|
|
This is working for me (adapted from above) - does this give you what you are after?
} Note: PrincipalId on Permission would seem to be superfluous as the association is being made across Resource.
|
|