my problem is this:
I have a base class
public abstract class ViewModelBase
which contains an abstract method "RegisterCommands"
protected abstract void RegisterCommands();
All my Derived classes must, obviously, implements this method, for example my LoginViewModel has
protected override void RegisterCommands()
{
LoginCommand?
.Configure(
execute: (msg) => { Login(User); },
canExecute: (x) => { return CanLogin(); }
);
}
and call it when class is instantiated, but i don't want call this method in every derived class constructor (if I have 100 derived classes I must call RegisterCommand 100 times).
Normal solution is call RegisterCommand in the base class constructor
protected abstract void RegisterCommands();
public ViewModelBase()
{
RegisterCommands();
}
and this usually works (even if I do not know if it's a good practice) but...but...
in my scenario, in all the RegisterCommands methods I use my ICustomCommand objects, which are initialized in the derived class constructor with dependency injection
public class LoginViewModel : ViewModelBase
{
private ICustomCommand _loginCommand;
public ICustomCommand LoginCommand
{
get
{
return _loginCommand;
}
set
{
_loginCommand = value;
}
}
public LoginViewModel(ICustomCommand loginCommand)
{
_loginCommand = loginCommand;
}
protected override void RegisterCommands()
{
LoginCommand?
.Configure(
execute: (msg) => { Login(User); },
canExecute: (x) => { return CanLogin(); }
);
}
So, because base class constructor is called before derived class constructor, I can't call RegisterCommands() in base class constructor (because my ICustomCommands are not initialized yet in derived class so RegisterCommands() try to use my ICustomCommand which are still null).
I know that is not possible call derived class constructor before base class constructor, so what could be a valid, simple and clean solution to call RegisterCommands in all derived class calling this command in only one point?
thanks for answer
UPDATE:
As I said, RegisterCommands() is plural because every derived class could have N ICustomCommand objects
So I can have
LoginCommand for my LoginViewModel
SaveCommand, DeleteCommand for another ViewModel
etc.
One solution I think now is to remove ICustomCommand initialization from constructor and resolve it "on the fly" in getter property trough a static Resolver class, something like this
public ICustomCommand LoginCommand
{
get
{
if(_loginCommand == null)
MyStaticResolver.Resolve<ICustomCommand>();
return _loginCommand;
But I'm still not convinced
If you use interfaces to represent your commands, you could pass them to the base class and expose a method to retrieve the commands. Your Registration method can then use them as needed:
public abstract class ViewModelBase
{
public ViewModelBase(params ICustomCommand[] commands)
{
_commands = commands;
RegisterCommands();
}
private IEnumerable<ICustomCommand> _commands;
protected abstract void RegisterCommands();
//This method gets you the commands
protected T GetCommand<T>() where T : ICustomCommand
{
var command = _commands.FirstOrDefault(c => typeof(T).IsAssignableFrom(c.GetType()));
return (T)command ;
}
}
public class LoginViewModel : ViewModelBase
{
public LoginViewModel(ILoginCommand command):base(command)
{
}
protected override void RegisterCommands()
{
//Get the command from the base class
var command = GetCommand<ILoginCommand>();
command?
.Configure(
execute: (msg) => { Login(User); },
canExecute: (x) => { return CanLogin(); }
);
}
}
public class LoginCommand : ILoginCommand
{
}
public interface ILoginCommand : ICustomCommand
{
}
Related
i need to do something like this in c#. But in the Exec(object) i got a compilation error.
public class ParentClass { }
public class class1 : ParentClass
{
}
public class class2 : ParentClass
{
}
public class class3 : ParentClass
{
}
public class MasterClass
{
public void ExecutionMethod(ParentClass myObject)
{
//some code
Exec(myObject);
//some code
}
public void Exec(class1 obj)
{
//some code
}
public void Exec(class2 obj)
{
//some code
}
public void Exec(class3 obj)
{
//some code
}
}
I solved using Reflection but i think must be a better approach, somebody could give me a nice idea
As #ScottChamberlain pointed out in the comments, you don't have any methods that take an argument of type ParentClass.
Take a look at the Liskov substitution principle - if you've done your implementation properly, you can substitute an instance of, for example, class1 for an instance of ParentClass, but the converse is not true at all.
Odds are, you don't need (or want) the overloads anyway. Just have ParentClass be an abstract class with an abstract Execute method that all child classes have to implement, then you can just call Execute on the class directly without bothering with the overloads. Even better, just make ParentClass an interface. (This is sometimes called the Strategy Pattern by the way).
public interface IParent {
void Execute();
}
public class class1 : ParentClass {
//Execute method implementation
}
public class class2 : ParentClass {
// ...
}
public class class3 : ParentClass {
// ....
}
public class MasterClass
{
public void ExecutionMethod(IParent myObject)
{
//some code
myObject.Execute();
//some code
}
}
I suggest you have a look at object-oriented design patterns. Specifically, the strategy pattern for this problem. Anyway, you can implement what you want like this:
public interface IParent
{
void Exec();
}
public class Child1 : IParent
{
void Exec() { /*code*/ }
}
public class Child2 : IParent
{
void Exec() { /*code*/ }
}
public class Child3 : IParent
{
void Exec() { /*code*/ }
}
public class MasterClass
{
public void ExecutionMethod(IParent myObject)
{
//some code
myObject.Exec();
//some code
}
}
You could also use an abstract class instead of an interface, if you wanted the parent class to have some functionality for the Exec method - then the child classes would have to override the method.
You can use command pattern, with dependency injection. I kind of give you an idea below. The concrete implementation will call execute on your receiver ( you logic goes there
public interface ICommand
{
void Execute();
}
public class Command1 : ICommand
{
public void Execute()
{
throw new NotImplementedException();
}
}
public class Command2 : ICommand
{
public void Execute()
{
throw new NotImplementedException();
}
}
public class Command3 : ICommand
{
public void Execute()
{
throw new NotImplementedException();
}
}
public class CommandManager
{
//you should use DI here to inject each concerete implementation of the command
private Dictionary<string, ICommand> _commands;
public CommandManager()
{
_commands = new Dictionary<string, ICommand>();
}
public void Execute(string key)
{
_commands[key].Execute();
}
}
The error your seeing is a result of your class1,2,3 objects being cast as their parent type because of the signature of the ExecutionMethod(xxx).
And not having an overridden method of Exec that takes a type of 'ParentClass' as the argument.
Probably the simplest method is to create an interface:
IDomainObject{}.
public class ParentClass : IDomainObject { }
public void ExecutionMethod(IDomainObject myObject)
{
Exec(myObject);
}
Using the interface in this way will prevent the downcast during the method call.
You need to use an interface here
Try changing ParentClass like this:
public interface IParentClass{}
Then make each of your classes implement it, like this:
public class class1 : IParentClass
{
}
public class class2 : IParentClass
{
}
Then in your MasterClass, try this:
public void ExecutionMethod(IParentClass myObject)
{
//some code
Exec(myObject);
//some code
}
public void Exec(IParentClass obj)
{
//some code
}
And then you can pass in any of your classes that implement the IParentClassinterface.
Now as an enhancement - if you want every implementation of IParentClass to have some methods and properties that you can invoke in your Exec method, do it like so:
public interface IParentClass
{
void DoTheThing();
}
This will force you to have this method in derived classes, so for example, class1 would look like this:
public class class1 : IParentClass
{
public void DoTheThing()
{
// things get done...
}
}
public class class2 : IParentClass
{
public void DoTheThing()
{
// things get done a different way...
}
}
And now in your Exec method, you can invoke like so:
public void Exec(IParentClass obj)
{
obj.DoTheThing();
}
Say I have a base class like this:
public abstract class MyBaseClass
{
protected void MyMethod(string myVariable)
{
//...
}
}
Then I inherit this class in a separate assembly:
public abstract class MyDerivedClass : MyBaseClass
{
static readonly string MyConstantString = "Hello";
protected void MyMethod()
{
MyMethod(MyConstantString);
}
}
I now want to make sure that any other class that inherits from MyDerivedClass does not have access to the MyBaseClass.MyMethod() method. (To clarify, I still want to be able to call MyDerivedClass.MyMethod() with no parameters)
I tried using protected internal but that didn't work.
Update: I'm trying to do this because the application I'm working on has a bunch of separate programs that use a base class. There are 5 different "types" of programs, each performs a specific, separate function but they all have some common code that I am trying to abstract into this base class. The common code is 99% the same, differing only slightly depending on what type of program is calling it. This is why I have my base class taking a string parameter in the example which then disappears in the derived base class, as that class knows it performs role x so it tells its base class accordingly.
Then I would instead of inheritance use composition in the MyDerivedClass. So all derived classes from this class does not know the methods from MyBaseClass. The class MyBaseClass would i make package visible so it is not possible to use it.
abstract class MyBaseClass
{
void MyMethod(string myVariable)
{
//...
}
}
abstract class MyDerivedClass
{
static readonly string MyConstantString = "Hello";
private MyBaseClass baseClass;
MyDerivedClass(MyBaseClass baseClass)
{
this.baseClass = baseClass;
}
protected void MyMethod()
{
baseClass.MyMethod(MyConstantString);
}
}
The class names should be changed of course.
This is not quite possible. And it may be a sign that your object design might be in trouble, but that's not a question for SO.
You can try a bit more underhanded approach, though:
public abstract class MyBaseClass
{
protected abstract string MyConstantString { get; }
protected void MyMethod()
{
//...
}
}
public abstract class MyDerivedClass : MyBaseClass
{
protected override sealed string MyConstantString => "Hello";
}
Or, more typically, just use the constructor to pass the required argument:
public abstract class MyBaseClass
{
private readonly string myString;
protected MyBaseClass(string myString)
{
this.myString = myString;
}
protected void MyMethod()
{
//...
}
}
public abstract class MyDerivedClass : MyBaseClass
{
protected MyBaseClass() : base("Hello") {}
}
Classes derived from MyDerivedClass have no way to change the argument in either case, the second approach is a bit nicer from inheritance perspective (basically, the type is a closure over an argument of its ancestor type).
You cannot stop inheriting classes from calling this method - you have made it protected so your intent is for it to be accessible to classes that inherit from it, whether directly, or via another sub-class.
If you want to keep the inheritance, the best you can do is to throw an error if the sub-class calls it in MyDerivedClass:
public abstract class MyBaseClass
{
protected void MyMethod(string myVariable)
{
Console.WriteLine(myVariable);
}
}
public abstract class MyDerivedClass : MyBaseClass
{
static readonly string MyConstantString = "Hello";
protected void MyMethod()
{
base.MyMethod(MyConstantString);
}
protected new void MyMethod(string myVariable)
{
throw new Exception("Not allowed");
}
}
public class SubDerivedClass : MyDerivedClass
{
static readonly string MyConstantString = "Hello";
public void Foo()
{
MyMethod(MyConstantString);
}
}
When Foo() is called in SubDerivedClass, it will call MyMethod in DerivedClass, which will throw the Exception.
I have event declared in abstract class:
public abstract class AbstractClass
{
public event Action ActionEvent;
}
public class MyClass : AbstractClass
{
private void SomeMethod()
{
//Want to access ActionEvent-- Not able to do so
if (ActionEvent != null)
{
}
}
}
I wanted to access this base class event in derived. Further I wanted to access this event in some other derived class of MyClass:
MyClass.ActionEvent += DerivedMethod()
Please help me understand how to work with event defined in abstract classes.
An often used pattern for this is something like the below (you'll see a lot of it in the classes in the System.Windows.Forms namespace).
public abstract class MyClass
{
public event EventHandler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
if (this.MyEvent != null)
{
this.MyEvent(this, e);
}
}
}
You would then use it in a derived class like this, optionally extending the behaviour:
public sealed class MyOtherClass : MyClass
{
public int MyState { get; private set; }
public void DoMyEvent(bool doSomething)
{
// Custom logic that does whatever you need to do
if (doSomething)
{
OnMyEvent(EventArgs.Empty);
}
}
protected override void OnMyEvent(EventArgs e)
{
// Do some custom logic, then call the base method
this.MyState++;
base.OnMyEvent(e);
}
}
This approach could be dangerous, see below for a better one
Events can only be raised (or checked for null apparently) from within the declaring class. This protection extends to derived classes.
Thus, the solution is to re-declare the event as an implementation of an abstract event in the base class. Then you can still use it via a base class reference as you want, and raise/use it in the derived class:
public abstract class AbstractClass
{
public abstract event Action ActionEvent;
}
public class MyClass : AbstractClass
{
public override event Action ActionEvent;
private void SomeMethod()
{
//Want to access ActionEvent-- Now you can!
if (ActionEvent != null)
{
}
}
}
Correct approach
MSDN Suggests that this approach may not be handled by the compiler correctly. Instead you should provide protected methods so derived classes can check for null, invoke the event, etc:
public abstract class AbstractClass
{
public event Action ActionEvent;
protected bool EventIsNull()
{
return ActionEvent == null;
}
}
public class MyClass : AbstractClass
{
private void SomeMethod()
{
//Want to access ActionEvent-- Now you can!
if (!EventIsNull())
{}
}
}
I have a BaseFormViewModel class which some of ViewModels inherit from. It inherits from another base class called BaseViewModel which just contains implementation of INotifyPropertyChanged interface.
The BaseFormViewModel contains several methods which I intend to be overriden in the derived classes:
public class BaseFormViewModel : BaseViewModel {
public BaseFormViewModel() {
_InitiateParameterAnswer = new Command(param => RaiseInitiateParameterAnswer(param));
}
protected Command _InitiateParameterAnswer;
public Command InitiateParameterAnswer {
get {
return _InitiateParameterAnswer;
}
}
protected virtual void RaiseInitiateParameterAnswer(object values) {
//Implementation
}
protected virtual Parameter SetAuditParameter(ParameterOption parameterOption, Guid parameterId, string remarks, string description, int weight) {
//Implementation
}
}
Here's one of the derived classes:
public class FSCGeneralProductionProcessMainViewModel : BaseFormViewModel {
public FSCGeneralProductionProcessMainViewModel() {
}
protected override void RaiseInitiateParameterAnswer(object values) {
//Implementation
}
protected override Parameter SetAuditParameter(ParameterOption parameterOption, Guid parameterId, string remarks, string description, int weight) {
//Implementation
}
However, whenever I invoke the InitiateParameterAnswer Command, the base class methods are being called. What am I missing here?
UPDATE:
Thanks for the feedbacks. With #Muds comment, I realised that I am calling each form (View) via Reflection and downcasting its DataContext (ViewModel) to BaseFormViewModel:
//Create an instance of the corresponding form using reflection
var instance = Activator.CreateInstance(Type.GetType(formName, true));
//Set the FormId
var viewModel = ((UserControl)instance).DataContext as BaseFormViewModel;
viewModel.FormId = formId;
viewModel..AuditId = auditId;
(The reason I have to is to set the value of base properties FormId and AuditId as illustrated above).
UPDATE 2:
The Command class is implemented as follows:
public class Command : ICommand {
public Command(Action action, bool canExecute = true) {
this.action = action;
this.canExecute = canExecute;
}
//ICommand Interface implementation here
}
UPDATE 3:
I instantiate the ViewModel inside the Form's constructor:
public partial class FSCStationProductionProcessMainView : UserControl {
public FSCStationProductionProcessMainView () {
InitializeComponent();
InitializeViewModel();
}
private void InitializeViewModel () {
this.DataContext = new FSCStationProductionProcessMainViewModel();
}
}
The simple answer would be to mark your BaseFormViewModel RaiseInitiateParameterAnswer method implementation as 'abstract' and force all derived classes to provide an implementation.
But in general this design seems overly complex:
Do you really need to so many levels of inheritence - it will be confusing and difficult in the long term to support and maintain.
Inheritence in my opinion should be used sparingly in object orientiated design.
As you can see in the code below, the DoStuff() method is getting called before the Init() one during the construction of a Child object.
I'm in a situation where I have numerous child classes. Therefore, repeating a call to the DoStuff() method directly after Init() in the constructor of each child wouldn't be an elegant solution.
Is there any way I could create some kind of post constructor in the parent class that would be executed after the child's constructor? This way, I could call to the DoStuff() method there.
If you have any other design idea which could solve my problem, I'd like to hear it too!
abstract class Parent
{
public Parent()
{
DoStuff();
}
protected abstract void DoStuff();
}
class Child : Parent
{
public Child()
// DoStuff is called here before Init
// because of the preconstruction
{
Init();
}
private void Init()
{
// needs to be called before doing stuff
}
protected override void DoStuff()
{
// stuff
}
}
If you have a complex logic for constructing your objects then consider FactoryMethod pattern.
In your case I would implement it as a simple
public static Parent Construct(someParam)
method that takes some parameter and based on it decides which child class to instantiate.
You can remove your DoStuff() method call from the constructor and call it inside Construct() on the new instance.
Also, you should avoid virtual/abstract method calls in the constructors. See this question for more details: Virtual member call in a constructor
Let me introduce a general solution using some C# features. Note that this solution does not require you to use a factory pattern or invoke anything after constructing the object, and it works on any class with just implementing an interface with a single method.
First we declare an interface that our classes will have to implement:
public interface IInitialize {
void OnInitialize();
}
Next we add a static extension class for this interface, and add the Initialize method:
public static class InitializeExtensions
{
public static void Initialize<T>(this T obj) where T: IInitialize
{
if (obj.GetType() == typeof(T))
obj.OnInitialize();
}
}
Now, if we need a class and all of its descendants to call an initializer right after the object is fully constructed, all we need to do is implement IInitialize and append a line in the constructor:
public class Parent : IInitialize
{
public virtual void OnInitialize()
{
Console.WriteLine("Parent");
}
public Parent()
{
this.Initialize();
}
}
public class Child : Parent
{
public Child()
{
this.Initialize();
}
public override void OnInitialize()
{
Console.WriteLine("Child");
}
}
public class GrandChild : Child
{
public GrandChild()
{
this.Initialize();
}
public override void OnInitialize()
{
Console.WriteLine("GrandChild");
}
}
The trick is that when a derived class calls the extension method Initialize, that will suppress any calls not made from the actual class.
How about this:
abstract class Parent
{
public Parent()
{
Init();
DoStuff();
}
protected abstract void DoStuff();
protected abstract void Init();
}
class Child : Parent
{
public Child()
{
}
protected override void Init()
{
// needs to be called before doing stuff
}
protected override void DoStuff()
{
// stuff
}
}
As others have mentioned, you should use a Factory Pattern.
public class Parent
{
public Parent()
{
}
public virtual void PostConstructor()
{
}
}
public class Child : Parent
{
public override void PostConstructor()
{
base.PostConstructor();
// Your code here
}
}
public void FactoryMethod<T>() where T : Parent
{
T newobject = new T();
newobject.PostConstructor();
}
I would strongly suggest use Factory like a pattern.
If it's possible:
1. Push all your childs and abstract class into separate assembly.
2. Declare ctors of childs like internal methods, so no one out of that assembly is can construct them just by calling ctor.
3. Implement the Factory class to construct for caller specified objects type, which obviuoly will forse calling of abstract DoStuff() method after actual creation of anobject, but before returning it to caller.
Good thing about this is that: It will give you also additional level of abstraction, so if in the future you will need some more functions call or any other type of logical complexity, what you will need, is just add them into your Factory class.
That is.
Regards
In WPF applications, you can postpone the invokation of DoStuff() with the help of Dispatcher:
abstract class Parent
{
public Parent()
{
Dispatcher.CurrentDispatcher.BeginInvoke(new Action(this.DoStuff));
}
private void DoStuff()
{
// stuff, could also be abstract or virtual
}
}
However, it is not guaranteed that DoStuff() will be called immediately after the constructor.
Correction: As per this answer, you can't determine when the base class's constructor is invoked during construction of the subclass.
E.g. This doesn't work:
public Child()
// DoStuff is called here after Init
// because of the overridden default constructor
{
Init();
base();
}
So, yes, as others have noted, if sequence of events matters, then the base class needs to be able to accommodate that by declaring abstract methods in order, or (better yet) by having the child class's implementation of DoStuff represent the sequence of events:
protected override void DoStuff()
{
Init();
base.DoStuff();
}
DoStuff is abstract. Just call Init from the top of DoStuff.
class MyBase
{
public MyBase()
{
//... do something
// finally call post constructor
PostConstructor<MyBase>();
}
public void PostConstructor<T>( )
{
// check
if (GetType() != typeof(T))
return;
// info
System.Diagnostics.Debug.WriteLine("Post constructor : " + GetType());
}
}
class MyChild : MyBase
{
public MyChild()
{
// ... do something
// ... call post constructor
PostConstructor<MyChild>();
}
}
How about...
public MyClass()
{
Dispatcher.UIThread.Post(RunAfterConstructor);
}
I also tried with Task.Run but that didn't work reliably.
Rather than using an abstract method, which would require you to implement the method in all descendant classes, you might try:
public class Parent
{
public Parent()
{
PostConstructor();
}
protected virtual void PostConstructor()
{
}
}
public class Child : Parent
{
protected override void PostConstructor()
{
base.PostConstructor();
/// do whatever initialization here that you require
}
}
public class ChildWithoutOverride
{
/// not necessary to override PostConstructor
}