Partial classes / partial methods versus base / inherited classes

question about class design. I currently have the following structure:

abstract base storage class

Repository implementation class by default (implements some abstract methods, where the logic is distributed through all concrete classes, but leaves others empty)

A specific repository implementation class (implements what remains empty in the default class above)

Now I come to the problem when I have a specific Update () method in a particular class, but when all the code in this method executes some code from the Default base class, it must also be executed.

I could do it like this:

public override Update()
{
    // do Specific class actions and updates
    // ....

    // follow with base.Update()
    base.Update();
}

but this requires those base.XYZ () calls in all inherited methods. Can I get around this somehow with particles?

, , ( ), . , , ?

+3
5

- :

public abstract class YourBaseClass
{
    public void Update()
    {
        // Do some stuff
        //

        // Invoke inherited class  method
        UpdateCore();
    }

    protected abstract void UpdateCore();
}

public class YourChildClass : YourBaseClass
{
     protected override void UpdateCore()
     {
         //Do the important stuff
     }
}


//Somewhere else in code:
var ycc = new YourChildClass();
ycc.Update();
+7

partial , :

, . , .

.

, .

( , ):

: . . , , .

// Definition in file1.cs
partial void onNameChanged();

// Implementation in file2.cs
partial void onNameChanged()
{
  // method body
}

, - .

+2

:

public sealed override void Update()
{
    UpdateCore();
    base.Update();
}

public abstract /* or virtual */ void UpdateCore()
{
    // Class-specific stuff
}
+2

, . , , . . OnXxxx() Control, MSDN " ", , .

, . , , . , , , . , , , , .

+1

( , ?) , , .

Call the virtual method at the appropriate implementation point of the abstract default implementation method.

You left your abstract class as it is, and transparently increased the default implementation flexibility.

0
source

Source: https://habr.com/ru/post/1728293/


All Articles