开发者

Fluent NHibernate - Map 2 Identical classes to same table

开发者 https://www.devze.com 2023-02-03 21:17 出处:网络
I\'ve seen this (unanswered) question asked once before, but in a different context.I\'m looking to have two domain objects map to the same table, WITHOUT a discriminator.The two classes are:

I've seen this (unanswered) question asked once before, but in a different context. I'm looking to have two domain objects map to the same table, WITHOUT a discriminator. The two classes are:

public class Category
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual ReadOnlyCategory ParentCategory { get; private set; }
}

and

public class ReadOnlyCategory
{
    public virtual int Id { get; private set; }
    public virtual string Name { get; private set; }
    public virtual ReadOnlyCategory ParentCategory { get; private set; }
}

The main difference is that all public properties of ReadOnlyCategory are read-only. My idea here is that I want all users of this class to know that they should only mess with the category they are currently 'looking' at, and not any other categories in the hierarchy. (I've left off other properties regarding t开发者_StackOverflow中文版he subcategories.)

Clearly, in the database, Category and ReadOnlyCategory are the same thing, and NHibernate should treat them very similarly when persisting them. There are three problems wrapped into one here:

1) How do I do the mapping?

2) When instantiating the objects, how do I control whether I instantiate Category or ReadOnlyCategory?

3) When persisting the objects, will the mapping be smart enough, or do I need to use an extensibility point here?

Any pointers on how I can get this to happen?

(Or am I crazy?)


This looks like wrong object model design to me. I don't see a good reason to introduce a new class just for authorisation reasons (whether user allowed to modify a given category object?). You may as well use one class and throw for example InvalidOperationException if an end user is not supposed to modify a category.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号