All Questions
Tagged with inheritance c#
84 questions
0
votes
2
answers
103
views
Modeling value object when fields' existence depends on state of other fields
I am practicing tactical DDD and having trouble as exemplified below. Fundamentally, whether some fields of the value object should be nullable depends on another field of the same value object. ...
-2
votes
3
answers
191
views
Class inheritance design with protected methods and run time base handlers
Using C#, I got a class called BaseConfigurations which handles CRUD operations. This class also contains a protected method which is used inside the CRUD handling methods:
public class ...
1
vote
4
answers
780
views
What's the alternative to trying to inherit static methods?
I know you can't inherit static methods, and it seems the consensus is that if you feel like you need to, you're doing something wrong.
However, I don't know what the alternative is in my case. Please ...
0
votes
1
answer
1k
views
Why are sealed classes not allowed to be mocked?
Using Moq framework, it is not allowed to mock a class in C# which is sealed. Same goes for many other frameworks as well. But why is it not allowed?
15
votes
6
answers
6k
views
Why is it necessary to mark classes as not inherited from? Can't an optimizer automatically detect that virtual calls are unnecessary?
In C# and C++, an apparent trend is in place to reduce / avoid inheritance:
C#: "Sealing types can improve performance."
https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/...
3
votes
2
answers
1k
views
Is inheritance constraint on Generics redundant? If not what is its recommended use?
I've recently found myself using a generics with constraint that a type should inherit from a specific base class, but now I've just realised that this is redundant and unnecessary because the ...
1
vote
1
answer
261
views
How do I ensure that only service models are used as properties?
I work on a system that utilizes a form of n-tier architecture in that we have data and service models. The service models derive from the data models:
public class FooDataModel {
public int ...
3
votes
3
answers
831
views
Use inheritance to make single child, smaller
We have a relatively big class. One of my colleagues thinks we must split this class into a base & child class to make it smaller and cleaner.
On the other hand, I believe when we know this parent ...
-4
votes
1
answer
993
views
What's the best way to design a state machine with different object inheritance
I have a class A and class B, both derive from the same parent class. Each object has a state machine inside it that defines it's behaviour. The behaviour is different though depending on if the ...
0
votes
1
answer
150
views
How can I have two interfaces implement one interface?
I have device interface classes written in c# for electronic variable loads. They are mostly for DC devices. We got a new one that does loading for both AC and DC devices. I also have an interface ...
0
votes
4
answers
464
views
C# refactoring with inheritance
I have two classes which contains almost same method. How can I refactor this with inheriting one class by other class.
class A{
public void run(){
// task 1
// task 2
// task 3
}
...
3
votes
5
answers
273
views
Should I create different classes for the same technical object but different business usages
I know my question sounds a bit weird so I will try to explain my issue with examples.
In my application, I have two types of business objects : Shops and Customers for example.
Both of them can be ...
4
votes
2
answers
308
views
OOP - How to identify inheritance abusage?
In looking for a simple heuristic to see when inheritance can be abused, I came up with the following hypothesis:
If subclass B overrides method foo, but does not call base.foo(), it seems like ...
3
votes
4
answers
728
views
Extension points via inheritance vs via delegate fields
In C#/.NET, I have a class that I want to provide extension points for. I can do this either using inheritance:
public class Animal {
public virtual void Speak() { }
}
public class Dog : Animal {
...
0
votes
1
answer
426
views
favor composition over inheritance: practical problems
In a C# application, I've got a behaviour that I would like to be available in different classes that not necessarily share the same ancestor. What better opportunity to 'favour composition over ...
0
votes
3
answers
2k
views
How to inherit a static function in a class?
Probably the answer is you can't. However, I would like to have a work-around to solve my problem.
Objective
I am trying to create a program in which I try to avoid nulls as much as possible.
...
3
votes
2
answers
785
views
Is making your ORM DAO a subclass of your Domain Model object a viable strategy?
I am trying to separate my domain model and associated business logic from my persistence model, particularly such that the domain model has no dependency on the ORM. One apparent way to do that was ...
0
votes
1
answer
234
views
How can I use disparate concrete classes that share similar functionality to create an interface that abstracts those similarities?
I've run into this issue a few times. I have preexisting framework objects that do not share a common ancestor. An example of this would be a class representing a an environment variable and a class ...
2
votes
3
answers
2k
views
How to use a child class property in a function written for base class
in C#, suppose I have written this function
public void TryToTurnOn( Device d )
{
// some common logic
// some more common logic
}
I then have a class that inherit Device that is ...
2
votes
3
answers
14k
views
Should we inherit from base class and implement interface in this scenarios?
I am new to object oriented design and learning about interfaces and design patterns. In this example, I am trying to create class for cars. My question:
Is it good practice to use base class and ...
3
votes
1
answer
123
views
Redesign Issue (Multiple Inheritance without Multiple Inheritance)
I am currently working on a game engine for work and have hit a wall on my idea. I have multiple objects (called prefabs) that have very common properties such as Position, Rotation, and Scale. There ...
0
votes
3
answers
356
views
Base class responsibility
can base class contain protected methods that are not to be used by all descendant classes?
I have this situation:
EntityA: has some methods that handle activities
EntityB: has some methods that ...
9
votes
1
answer
14k
views
C# design to force the virtual method to be called from the override, or something similar
We can do something like this:
// in base class
protected virtual void Init(){
//do stuff that all objects need to do
}
//in derived class
protected override void Init(){
base.Init(); // if ...
0
votes
1
answer
6k
views
Workaround for casting method parameter interface to desired concrete type in polymorphic design?
Let's say that I have a group of report types to be created and all of them inherit from base abstract class:
abstract class Report
{
public abstract void GenerateReport(IReportData data);
...
0
votes
1
answer
238
views
Polymorphism/Inheritance design concerns
I have a FtpServerDataSeriesProvider that is used to provide data series from Ftp server files:
public interface class FtpServerDataSeriesProvider
{
IEnumerable<DataSeries> GetDataSeries();...
1
vote
1
answer
698
views
Alternatives to inheritance for components with different behaviours?
I have been learning a lot of inheritance in C#. But am wondering if there are other ways that can be cleaner for component based setups.
I have objects that are all pretty much the same but they ...
-1
votes
2
answers
4k
views
C#, Static classes and Inheritance
As a beginner I'll try to explain my problem as good as I can:
I'm currently trying to program a "simple" ECS. My basic idea is that I have a base "Entity class" which includes all sorts of functions ...
2
votes
2
answers
930
views
Is there a best practice to forward method overrides while enforcing method execution?
Sometimes i have code along the lines of this:
public abstract class A
{
protected abstract void DoSomething();
}
public abstract class B : A
{
/// <...
0
votes
3
answers
5k
views
C# Implementing multiple, disparate interfaces in a single base class
(This is probably a dupe, but if so I haven't seen it yet, although it may be Best practice for encapsulating a parameter that requires multiple interfaces to be implemented, but that question doesn't ...
0
votes
3
answers
2k
views
Are objects that can pass more than one IS-A test really polymorphic?
A number of tutorials on polymorphism state that "Any object that can pass more than one IS-A test is considered to be polymorphic." I wonder what they mean by that, and if that's even a true ...
0
votes
1
answer
437
views
Creating viewmodels out of inheritance tree
I have a domain interface ITransformation with several implementing classes such as RemoveRowsTransformation and AddColumnTransformation. The inheritance tree models the command pattern. The ...
1
vote
2
answers
610
views
Options for derived classes of two abstract base classes
Let's say there are classes D1, D2, etc. describing different types of an abstract class D.
Let's say there are SenderReceiver classes describing different ways of communicating for each D1, D2, etc.:...
1
vote
2
answers
5k
views
share method logic along classes without inheriting from abstract class
In some languages (e.g. C#) a class can only ever have 1 base class which seems like a problem for what I'm trying to do. I will give you an example of what i'm trying to do, hopefully this will make ...
1
vote
2
answers
419
views
How to combine these fill and create methods or utilize generics in factory?
Domain object "Contragent"
Let's say I have an hierarchy of classes:
public class BaseContragent
{
public int Id { get; set; }
}
public class PersonContragent : BaseContragent
{
public string ...
2
votes
1
answer
110
views
What is the best practice for finding usage for other source code utilization / functionality with no documentation [closed]
I have traditionally not utilized much of other peoples code libraries in my code, but now I am starting to use open source projects more, and am needing an efficient way to understand quickly how to ...
3
votes
3
answers
2k
views
Wrapping Controller / ApiController to remove boilerplates
We are aiming to reduce code noise that would be common for all Controllers such as basic CRUD.
public interface IGenericController<T, Y> where T : BaseMaster
{
IEnumerable<T> Get();
...
0
votes
1
answer
3k
views
Reuse base() constructor argument in this() constructor
Let's say I have two classes Base and Derived : Base.
Derived shall be able to use a DerivedComponent : BaseComponent,
just like all other derivates of Base use their own derivate specific component.
...
0
votes
2
answers
382
views
Question on members in derived classes (new vs override)
I'm working through the Head First Design Patterns book and am currently on the Decorator Pattern chapter. Since the book examples are written in Java, I'm adapting the, to C# as I go.
This example ...
6
votes
2
answers
3k
views
Is it ok to have an empty abstract class to make concrete classes polymorphic
BEFORE:
I have an interface that has one method definition
public interface IDockable
{
void Dock(DockerContainerConfig config);
}
Everything is ok for my first implementation
public class ...
1
vote
1
answer
136
views
Code reuse on function write objects data to file
I have a few base, derived classes and need to dump their data to file. The difference is only a field, but I don't know how to reuse code of base class. Sorry if noob question :p
Those classes look ...
1
vote
1
answer
1k
views
Inheritance is better or composition design pattern in this scenario?
Design and implement Cash Register:
Given a number of items you will be required to calculate the total bill.
Items are charged for in a couple of different ways:
A given price for each item, e.g. ...
2
votes
1
answer
2k
views
Adding behaviour to a part of a third party class
I am working with an effectively third-party library. This library has a base class that's used as the interface (C++ style), ComponentBase. ComponentBase has a number of methods and setters that are ...
1
vote
4
answers
294
views
Abilities interacting with aspects of other abilities - how to implement efficiently
In my game I want characters to be able to cast spells which deal a certain amount of damage of a certain type to other characters in the game.
I also want abilities that negate an amount of damage ...
4
votes
2
answers
2k
views
Applying composition over inheritance to Vehicle classes
I have a car maintenance garage program that has an abstract Vehicle class that has several derived classes like Car MotorCycle, etc. Each of those derived vehicles in turn is either a fuel or an ...
-3
votes
2
answers
192
views
Do you say "this" on protected members? [closed]
Background
I have never had a formal class in OOP/OOD although I understand how the protected members work. Being accessible by any instance/derived class, they are thereby less private (more ...
3
votes
2
answers
283
views
How do I deal with unneeded inherited members?
Consider the following UML diagram:
In a nutshell:
ChildClass1 and ChildClass2 use all of the members of ParentClass.
ChildClass3only uses Member1 and Member2 and the value of Member5 is a constant ...
0
votes
3
answers
18k
views
What to do if I need more than one base class in C#? [closed]
Let's say I have a grid with square fields. For the fields I have an abstract Field class. This class has several subclasses, for example EmptyField or RoadField. Some of these fields can be connected ...
8
votes
7
answers
2k
views
A property that can represent both a single date and a date range: How to properly model that?
I work in a system that can represent a "shipping estimate" in two ways:
A specific date: The item is guaranteed to ship at that date
A day interval: The item will be shipped "X to Y" days from today
...
2
votes
1
answer
3k
views
Creating multiple instances of an implementing class
I currently have an abstract class and multiple classes extending it. My problem is, that there has to be a way to create a variable number of instances of an extending class. The number of instances ...
3
votes
1
answer
5k
views
How to avoid having nested generic in class
I'm working on a side project, and I turned on all rules for code analysis in Visual Studio, and I got the warning notice:
Warning CA1006 Consider a design where 'Vote<T>.CalculateWinner(...