How to implement an interface to secure a specific contract?

Asked

Viewed 106 times

3

Need to get an interface to 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 receive as a parameter a concrete class:

    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 on the list:

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

have at least the properties of ITrade a few more properties. I can’t 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.

  • Did you understand that the solution I posted is what you need? You commented on my answer and then deleted it. I don’t know if it was because you realized it was exactly what you wanted or if you still didn’t understand the solution. Your comment exactly conforms my answer.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

4

There are several problems in the code (not to mention that C# does not have templates, has Generics) And the question doesn’t go much into 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>();
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

From what I understand the first class should be an interface. And if it is not generic there is no reason to use the genericity mechanism.

I implemented the second class as I thought I should just for testing. I’m actually wondering if this is important.

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

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

There are other possible more modeling problems that I haven’t addressed.

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

0

So buddy, interface is not to force class properties, but 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 afterwards.

As an 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; }
}
  • Properties are actually methods. I’m not saying your answer is wrong, but it is misleading.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.