Disable possibility to update subclasses

157 views Asked by At

I am developing a inheritance strategy in fluent nhibernate. Everything works correctly but I have a one question. Is there any possibility to disable updating base property through the subclass?

Here is a dummy code:

public class ObjectA
{
    public virtual string StatusA { get; set; }
}

public class ObjectB : ObjectA
{
    public virtual string StatusB { get; set; }
}

public class ObjectBMap : SubclassMap<ObjectB>
{
    public ObjectBMap()
    {
        Map(x => x.StatusB);
    }
}

When I am updating objectB I don't want to update StatusA. I want to change the status A when I will be updating ObjectA. Does nhibernate have this kind of feature? Does it have sense?

Edit: Additional explenation The reason why I want to do such thing is that in my system (asp mvc application) we have two different places where we manage objectsA and objectsB. First we create object A and later we want to 'convert' object A to objectB. Then we can edit these two objects in two different modules.

My flow for editing objectB: -Read objectB from db, convert it to viewmodel -post form from view, convert view data from form to objectB and update in db.

I don't want to extend view model for objectB for data from object A, store this data in some hidden fields and convert from view model.

I thought that if could mark that this data couldn't be updated by Session.SaveorUpdate(objectB) it would resolve my problems. So basically that was my question.

1

There are 1 answers

1
Radim Köhler On

Try to check the documentation:

Small cite:

<class
    name="ClassName"                              (1)
    table="tableName"                             (2)
    ...
    dynamic-update="true|false"                   (7)
    dynamic-insert="true|false"                   (8)
    ...

...
(7) dynamic-update (optional, defaults to false): Specifies that UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.
(8) dynamic-insert (optional, defaults to false): Specifies that INSERT SQL should be generated at runtime and contain only the columns whose values are not null.

What we can see, is the setting "dynamic-update" ... which does what we would expect: update only properties which were changed

This would be the most native way how to learn NHibernate to issue updates only to the ObjectB ... if there are no changes in the ObjectA defined properties.

But in general: simply leave it up NHibernate. What it does, would most likely be the best we should require... it is a mature tool

EXTEND

Based on the question extension - I would say: do not go that way... Do not.

With ORM tools you will gain a lot, if your model, the business domain model, is kept as easy as it could be. NHibernate can help with lot of stuff e.g.:

  • selection of all or only some properties - called projections
  • paging
  • sorting
  • filtering
  • cascading of WRITE operations
  • and even more...

But it won't help you to manage the "unexpected" or "exceptional" domain model designs.

Please read this:

Composition over inheritance (or Composite Reuse Principle) in object-oriented programming is a technique by which classes may achieve polymorphic behavior and code reuse by containing other classes that implement the desired functionality instead of through inheritance...