Better Solutions to Implementing global entity properties in class - c#

I use classes to respond to PageMethods in some webpages. Asp.net engine automatically serializes the Class to JSON. I have few classes that have common properties like "RequestState"-Enum and "Error"-String which represent the State of the Request and the String describing the Error if any. For example look into this class below,
public class Contacts
{
public string Name { get; set; }
public string Country { get; set; }
public RequestState RequestState { get; set; }
public string Message { get; set; }
}
last two properties come in more than 4-5 classes. How do i abstract these properties so that they become common instead of declaring them in each class like how i do now. Interface could be possible i would like your ideas too for this, code snippet would be highly appreciated. thanks :)

public abstract class MessageBase
{
public RequestState RequestState { get; set; }
public string Message { get; set; }
}
public class Contacts : MessageBase
{
public string Name { get; set; }
public string Country { get; set; }
}

Related

Using empty base class just for polymorphism purpose?

I have my ResponseDto which includes a simple string property named Answer.
public string Answer { get; set; }
Now, the requirement came such that I could either be getting an answer as a string, or as an array of int.
I decided to create two classes for this:
public class AnswerType {
public string Answer { get; set; }
}
public class OptionAnswerType {
public int[] AnswerOptionIds { get; set; }
}
I could serialize / deserialize it accordingly.
But to still keep a single response property type, I thought about creating an empty base class:
public class BaseAnswerType { }
public class AnswerType : BaseAnswerType {
public string Answer { get; set; }
}
public class OptionAnswerType : BaseAnswerType {
public Guid[] AnswerOptionIds { get; set; }
}
and change my property in ResponseDto to:
public BaseAnswerType Answer { get; set }
through which via run time, I would be returning either of the two classes.
Is this a bad approach? Any alternate would be greatly appreciated.

create a List of different Type of Objects

class HeroClass
{
skill1
skill2
}
class Batman : HeroClass
{
skill3
skill4
}
class Robin : HeroClass
{
skill3
skill4
skill5
}
I'm a beginner in programming.
I want to make a List which can hold different Types of Objects.
If i create a list of Type HeroClass I am not able to access specific properties of object Type Batman or Robin
How can this be done?
Another question: as you can see class Batman and Robin have equal properties (3/4) would it be better to let "Robin" inherit from class "Batman"? These properties are totally same(polymorphism wouldn't make any sense) Is this a code smell if to classes have exact same properties?
Thank you a lot!!
The first issue:
If i create a list of Type "HeroClass" i am not able to access specific properties of object Type "Batman" or "Robin"
You can access specific properties by casting object, for example:
var heroList = new List<HeroClass>();
heroList.Add(new Batman { skill3 = "skill3" });
heroList.Add(new Robin { skill5 = "skill5" });
Console.WriteLine(((Batman)heroList[0]).skill3);
Live demo here
The second issue:
These properties are totally same(polymorphism wouldn't make any sense) Is this a code smell if to classes have exact same properties?
Yes, you should use Interface Segregation Principle (ISP) of SOLID
That clients should not be forced to implement interfaces they don't use. Instead of one fat interface many small interfaces are preferred based on groups of methods, each one serving one sub module
For example
interface IHero
{
string skill1 { get; set; }
string skill2 { get; set; }
}
interface IHero1234: IHero
{
string skill3 { get; set; }
string skill4 { get; set; }
}
interface IHero12345: IHero1234
{
string skill5 { get; set; }
}
class BaseHero : IHero
{
public string skill1 { get; set; }
public string skill2 { get; set; }
}
class Batman : IHero1234
{
public string skill1 { get; set; }
public string skill2 { get; set; }
public string skill3 { get; set; }
public string skill4 { get; set; }
}
class Robin : IHero12345
{
public string skill1 { get; set; }
public string skill2 { get; set; }
public string skill3 { get; set; }
public string skill4 { get; set; }
public string skill5 { get; set; }
}
Link live demo here

C# - Receive object as parent interface and typecast one of child class

New ASP.NET core (to the whole web thing actually). So, making controllers and also started implementing Interfaces for scalability (my code base is pretty big, and already feeling the heat implementing Interfaces too late down the line). I have a couple of classes like this -
public interface IParent {
string IParent1 { get; set; }
string IParent2 { get; set; }
string IParent3 { get; set; }
}
public class ChildClass1: IParent {
public string IParent1 { get; set; }
public string IParent2 { get; set; }
public string IParent3 { get; set; }
string ChildClass11 { get; set; }
string ChildClass12 { get; set; }
string ChildClass13 { get; set; }
}
public class ChildClass2 : IParent {
public string IParent1 { get; set; }
public string IParent2 { get; set; }
public string IParent3 { get; set; }
string ChildClass21 { get; set; }
string ChildClass22 { get; set; }
string ChildClass23 { get; set; }
}
I need to send an object which is of type ChildClass1 or ChildClass2 from my Angular/Typescript website (the json model is either ChildClass1 or ChildClass2), and one of my controllers will receive this -
[HttpPost, DisableRequestSizeLimit]
public async Task<IActionResult> Post(IParent parent)
{
if (parent is ChildClass1)
{
// Do something
}
if (parent is ChildClass2)
{
// Do something else
}
// Rest of the controller
...
}
Now the issue is, the arg parent of the controller is only receiving whatever properties it has. I.e. properties of childclasss are not deserialized and doesn't even exist (because the parent doesn't have them I think).
Can someone kindly suggest how to solve this, please? I would like the parent arg to hold full ChildClass1 or ChildClass2 variables and then typecast and perform their related operation.
Apologies if somethings doesn't make sense. Please let me know if you need more information. Many thanks in advance!

Reusing C# class in MVC C#

I have an application that can contain a minimum of one "person" up to a maximum of fours "persons". I am using view models and manually mapping in the controller to the domain model.
I am completely lost as to how to include more than one "person" in the app. I've read up on using for but can't wrap my head around it.
Right now, I am just adding the data from the "person" class manually such that
Code:
public class SomeClass
{
public Guid SomeClassId {get; set;}
public string BorrowerFirst { get; set; }
public string BorrowerMI { get; set; }
public string BorrowerLast { get; set; }
public Suffix? BorrowerSuffix { get; set; }
... some more fields ...
}
and so on in the master class.
What I'd like to do is use a class such as:
Code:
public class Applicant
{
public string BorrowerFirst { get; set; }
public string BorrowerMI { get; set; }
public string BorrowerLast { get; set; }
public Suffix? BorrowerSuffix { get; set; }
}
can be reused in another class multiple times.
How can I separate that to strip that and instead use a named class consisting of first, middle and last names and allowing up to four "person" instances in my master class?
Have you tried inheritance?
public class Person
{
public Guid SomeClassId {get; set;}
public string BorrowerFirst { get; set; }
public string BorrowerMI { get; set; }
public string BorrowerLast { get; set; }
public Suffix? BorrowerSuffix { get; set; }
}
and the Applicant class:
public class Applicant : Person
{
//Only extra properties and methods here.
public string FullName
{
get
{
return this.BorrowerFirst + " " + this.BorrowerMI + " " + this.BorrowerLast;
}
}
}
You can then have a vendor as well:
public class Vendor: Person
{
//Only extra properties and methods here.
}

Not sure how to implement IEnumerable on this class

Continuing to develop the API I have mentioned in previous posts, I have come across the following situation:
I need to be able to access a list of responses returned by the
webservice.
Problem is I am unsure how to implement IEnumerable on this class.
...
public class ResponseBodyResponse
{
public ResponseListResponse ResponseList { get; set; }
public class ResponseListResponse
{
public ResponseInfoResponse ResponseInfo { get; set; }
public class ResponseInfoResponse
{
public string RequestId { get; set; }
public string RequestType { get; set; }
public DateTime RequestDate { get; set; }
public string RequestStatus { get; set; }
public string Error { get; set; }
public string Memo { get; set; }
}
public ResponseListResponse()
{
ResponseInfo = new ResponseInfoResponse();
}
}
public ResponseBodyResponse()
{
ResponseList = new ResponseListResponse();
}
...
Before anyone asks I did get a copy of the xsd files, however generating the classes using xsd.exe resulted in a ridiculous mishmash of files with conflicting class names causing over 1000 ambiguous naming errors.
You really should return a concrete collection such as a list or an array from a web service instead of an implementation IEnumerable<T>, even though lists and arrays (and other colections) do implement it. Its not IEnumerable<T> that is the key for the serialization.
Aside, the nested class structure makes your code hard to consume.
Since I'm not sure of your intent with your above code, here is an example
public class Road
{
public Car[] Cars { get; set; } // this can be also `List<Car>`
}
public class Car
{
// stuff
}

Categories