Class per command or not - best practices [closed] - c#

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
Let's say that I have two commands CreateCarCommand and UpdateCarCommand and they obviously share some properties.
What is the best practice, should I create separate classes per command like
public class CreateCarCommand { ... }
public class UpdateCarCommand { ... }
or should UpdateCarCommand inherit from CreateCarCommand
public class CreateCarCommand { ... }
public class UpdateCarCommand : CarCommand { ... }

I would have two separate command classes - and if you have a lot of common properties, use a CarCommandBase base class and make CreateCarCommand and UpdateCarCommand inherit from that base class:
public class CarCommandBase
{
// define common properties and possibly methods here
}
public class CreateCarCommand : CarCommandBase
{
// custom code
}
public class UpdateCarCommand : CarCommandBase
{
// custom code
}

Related

How can I create a reference that can contain both a stack and a list without needing a cast in c#? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'd like to be able to have a reference to a collection in a superclass that will need to contain either a stack or a list in a subclass, and I am having trouble understanding how to make that work, any ideas?
Something like:
public class Group
{
Collection<Human> group;
}
public class PeopleStack : Group
{
public PeopleStack()
{
this.group = new Stack<Human>();
}
}
public class Crowd : Group
{
public Crowd()
{
this.group = new List<Human>();
}
}
The Stack is defined as
public class Stack<T> :
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.ICollection
and the List is
public class List<T> :
System.Collections.Generic.ICollection<T>,
System.Collections.Generic.IEnumerable<T>,
System.Collections.Generic.IList<T>,
System.Collections.Generic.IReadOnlyCollection<T>,
System.Collections.Generic.IReadOnlyList<T>,
System.Collections.IList
So you can use one of the strong (generic) types IEnumerable<Human> or IReadOnlyCollection<Human> for group.
And of course that should be protected etc, but I suppose you know that.

Why would an empty class serve as the base class for several generic classes? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm looking over some scripts from a Unity project called 2D GameKit, & I'm trying to learn from piecing together the purpose of each. In one of them that serves as a mechanism for persistent storage, I noticed that several generic classes were derived from an empty base class. Why?
Here is a sample of the code:
public class Data
{
}
public class Data<T> : Data
{
public T value;
public Data(T value)
{
this.value = value;
}
}
public class Data<T0, T1> : Data
{
public T0 value0;
public T1 value1;
public Data(T0 value0, T1 value1)
{
this.value0 = value0;
this.value1 = value1;
}
}
So that you can check (know / compare) that a certain object is definitely data. If you don't have a base class, when you want to check if an object is certainly a type of data before processing it, you will need to compare with Data<T> AND Data<T0, T1> and also any other data types that is created in the future

Force Method to be called after overloaded constructor is called from the Super-class [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm working on a unity3d project where I have an abstract object that I will be calling from other objects. I want all my subclasses to call a certain virtual method after they finish constructing. Essentially the base constructor will be called, then the overloaded constructor, and finally my method, but I want this behavior defined from the base object.
You can force your subclass to implement an abstract method called Initialize() and then choose the order of calls in your base class :
public abstract class MyBaseClass
{
public abstract void Initialize();
public MyBaseClass()
{
// Code of the constructor of the base class
// Calling the subclass
Initialize();
// Finally call the special method
MySpecialMethod();
}
private void MySpecialMethod()
{
// Some code here
}
}
public class MySubclass : MyBaseClass
{
public override void Initialize()
{
// Some code here
}
}

OOP Method Usage [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I would like to ask if I have a class Person and Food,
and I want to have method of "eat".
Should I put eat method in class Person or method eaten in class Food ?
Thank you.
It depends that you say Person.Eat(food) or Food.EatenBy(person)
If you preffer Person.Eat(food)
public class Person
{
public void Eat(Food food)
{
//...
}
}
If you preffer Food.EatenBy(person)
public class Food
{
public void EatenBy(Person person)
{
//...
}
}

How can i create dynamic variables on the fly in c# using Reflection? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm creating a simulator of ecosystems where species can be used to simulate various diseases, my problem is that I start using 4 species but if I need more ... I need more variables to store, my question is, Is there any way through Reflection to let me create dynamic variables during the execution of an event in my program? Thank you! i'm using Windows Presentation Foundation and C#
The normal way to handle this is to have a base class for your disease species and then use a collection to hold them all:
public abstract class DiseaseBase
{
public abstract void Spread();
}
public class Anthrax : DiseaseBase
{
public override void Spread()
{
GetPostedToPolitician();
}
}
public class BirdFlu : DiseaseBase
{
public override void Spread()
{
Cluck();
SneezeOnHuman();
}
}
public class SwineFlu : DiseaseBase
{
public override void Spread()
{
//roll in mud around other piggies
}
}
public class ManFlu : DiseaseBase
{
public override void Spread()
{
//this is not contagious
//lie in bed and complain
//get girlfriend to make chicken soup
//serve chicken soup with beer and baseball/football/[A-Za-z0-9]+Ball
}
}
public List<DiseaseBase> DiseaseCollection = new List<Disease>();
So everything gets stored in the collection as the base class (DiseaseBase), and with the appropriate use of abstract methods in the base and/or interfaces you can always handle each disease instance as the base object.

Categories

Resources