Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using C# Interfaces to Expand existing Application

Tags:

c#

oop

asp.net

I'm about to start a re-development of my ASP.NET C# MVC commercial project which handles nominating for horse events in Australia.

I would like expand it to other types of events, eg, car racing, football, anything. At the moment, its very specific to this sport

I've been doing research into Interfaces and other OOP approaches but applying it to the real world is a little daunting.

This is my basic plan:

public interface INomination
{
    public void AddNomination(); 
    public void UpdateNomination();
    public void DeleteNomination();
}

public BaseNomination
{
    // Define properties
}

public HorseNomination : BaseNomination, INomination
{
    // Horse nomination specific stuff, like specifying the horse
    public bigint HorseID { get; set; }
}

Then, in the a NominationService, I could call this

public void HandleNomination(INomination nomination)
{
    nomination.Add();
}

Where my head starts to hurt is how to differentiate between a nomination that requires the user to specify the horse for the HorseNomination, or not, like CarNomination?

Do I need IHorseNomination, ICarNomination? If so, how do I, for example, handle it to render the type of View for user input?

like image 752
clayiam Avatar asked Mar 31 '16 00:03

clayiam


1 Answers

As what i have understand from question is you are stuck how you will call differenct Nominations i.e HorseNomination,CarNomination etc.

public void HandleNomination(INomination nomination)
{
   nomination.Add();
}

What you have to pass in this parameter is the object of the class for which you want to perform an action.

  // If you want to perform actions from HorseNomination 
    HandleNomination(new HorseNomination());

  // If you want to perform actions from CarNomination
    HandleNomination(new CarNomination());

But remember in order to pass objects you have to inherit interface for that class.

I hope i had understand your question and tried a litle bit to answered that.

like image 95
Salman Zahid Avatar answered Nov 03 '22 00:11

Salman Zahid