How to implement an interface to guarantee a specific contract?

3

I need to make an interface implement a list of a certain type:

public class ITrade<T>
{
    public int date { get; set; }
    public double price { get; set; }
    public double amount { get; set; }
    public string type { get; set; }
}

interface ITrades<T>
{
    List<ITrade<T>> Trades { get; set; }
}

But I want ITrade to take a concrete class as a parameter:

    public class Trade
    {
       public int date { get; set; }
       public double price { get; set; }
       public double amount { get; set; }
       public int tid { get; set; }
       public string type { get; set; }
    }

The idea is this: Trade is in a template file and I want every template to have at least the properties of ITrade . That is, I need to force the elements of the list:

List<ITrade<T>> Trades { get; set; }

have at least the properties of ITrade plus some more properties. I can not put this in ITrade because I want to separate a general interface that can work with multiple models and I want to keep the models separate.

    
asked by anonymous 01.04.2018 / 20:47

2 answers

4

There are several problems in the code (not to mention C # does not have templates , has generics ) and the question does not detail much the need, but I think this would be the solution:

using System;
using System.Collections.Generic;

public class Program {
    public static void Main() {
        var trade = new Trading();
        trade.Trades.Add(new Trade() {Tid = 1, Date = DateTime.Now, Price = 100M, Amount = 10M, Type = "um"});
        trade.Trades.Add(new Trade() { Tid = 2, Date = DateTime.Now, Price = 200M, Amount = 20M, Type = "dois"});
    }
}

public interface ITrade {
    DateTime Date { get; set; }
    decimal Price { get; set; }
    decimal Amount { get; set; }
    string Type { get; set; }
}

interface ITrades {
    List<ITrade> Trades { get; set; }
}

public class Trade : ITrade {
    public int Tid { get; set; }
    public DateTime Date { get; set; }
    public decimal Price { get; set; }
    public decimal Amount { get; set; }
    public string Type { get; set; }
}

public class Trading {
    public List<ITrade> Trades { get; set; } = new List<ITrade>();
}

See running on .NET Fiddle . And no Coding Ground . Also I placed GitHub for future reference .

So I understood the first class should be an interface. And if it is not generic you do not have to use the mechanism of genericity.

Implemented the second class as I thought it should just to test. I really doubt if this is important.

I have changed the types of data that are wrong. And I improved the names to stay in the C # standard.

I made the implemented class conform to the interface and achieve the goal described in the question.

There are other possible more modeling issues that I have not addressed.

My advice would be to start doing more basic things, learn the features you are trying to use before using them. The result will be much better.

    
01.04.2018 / 22:33
0

So friend, interface is not to force properties of classes and methods, to make other trade models have the same properties, you have to create an abstract class and inherit it in the others, and an interface with the method later.

As the example below:

public abstract class TradeBase {
   DateTime Date { get; set; }
   decimal Price { get; set; }
   decimal Amount { get; set; }
   string Type { get; set; }
}

public interface ITrade {

}    


public class TradeA:TradeBase {
   decimal PropriedadeApenasDoTradeA{ get; set; }
}


E uma classe para receber as traders, ai voce pode passar qualquer um que herde da tradebase

public class Tradding
{
    public List<TradeBase> Traders { get; set; }
}
    
02.04.2018 / 14:40