Alternative way to return concrete objects [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a function which takes an object parameter say entity and returns concrete objects like below. The concrete objects inherits from IEntity.
public IEntity GetEntity(object entity)
{
if(entity is A) { .... return new Customer(); }
else if(entity is B) {... return new Invoice(); }
.......
}
This obviously works but I was wondering if this is the best approach or is there any other alternative and recommended way from architecture perspective?
Thanks!

You could use generics:
public IEntity GetEntity<T>(T entity)
where T : IEntity, new()
{
return new T();
}

Related

Class per command or not - best practices [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 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
}

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

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)
{
//...
}
}

Can I cast abstract and interface classes (If i can than how will be cast)? [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 am trying to verify whether can i cast abstract and interface class . I have tried the following but can't get it so I assume I'm going about it the wrong way.
Your question is a bit vague. There can be quite a few possibilities to your question. I have tried to give them here. See if it fits your needs. If not then please comment
Building of class structure
public abstract class MyAbClass
{
public abstract void MyM1();
}
public interface IMyInterface
{
void MyM2();
}
public class MyConcretClass1 : MyAbClass
{
public override void MyM1()
{
//Your implementation here
}
}
public class MyConcretClass2 : IMyInterface
{
public void MyM2()
{
//Your implementation here
}
}
Possibilities
//Following are the possibilities
//This can be done in some other class and passed to some other method
//maybe a factory pattern
MyAbClass cls1 = new MyConcretClass1();
//Will call method of MyConcretClass1
cls1.MyM1();
if (cls1 is MyConcretClass1)
{
//Do casting here
}
var cls2 = cls1 as MyConcretClass1;
if (cls2 != null)
{
//Do your stuff here
}
//This can be done in some other class and passed to some other method
//maybe a factory pattern
IMyInterface cls3 = new MyConcretClass2();
//Will call method of MyConcretClass2
cls3.MyM2();
if (cls3 is MyConcretClass2)
{
//Do casting here
}
var cls4 = cls3 as MyConcretClass2;
if (cls4 != null)
{
//Do your stuff here
}
Hope this helps

Categories

Resources