c# - How do I specify an interface return type while still remaining dynamic? -
i'm flipped upside down here
what i'm trying create bit of factory works on different types of tickets. i've create iticket interface has couple signatures
getopen(); getbyid(int id);
now, these methods need return instance or list of instances of same ticket type, thought i'd have interface this
list<iticket> getopen(); iticket getbyid(int id);
so in practice, hoping create factory method this:
public static list<iticket> getopen(iticket ticket) { return ticket.getopen(); }
ok, example, went class mocrequest , implemented list getopen() method this:
public list<mocrequest> getopen() { //implementation grabs bunch of moctickets out of database , converts them instances of mocticketclass , returns them }
i error saying isn't valid implementation because return type mocrequest instead of iticket. thought mocrequest iticket @ stage, in hindsight guess it's bit circular doesn't work. return type needs mocrequest can still used.
i've experimented making interface more iticket<t>
still have specify ticket class type in factory kind of defeats purpose.
so objective here create single static method in factory handle stuff. going right way? how make work? or still stuck making multiple methods handle them? tia
mocrequest
may iticket
, doesn't make list<mocrequest>
list<iticket>
.
if return list<mocrequest>
list<iticket>
expected, user of api might use resulting list , add iticket
not mocrequest
. list<mocrequest>
in case? break, right?
therefore, compiler insists on returning list<iticket>
.
in general, it's not adviseable return list<t>
, because it's not evident whether you're returning copy of internal or reference actual internal list. better declare return type ienumerable<iticket>
, means return tickets, not writeable list object. (the user of api free insert tickets list of his/her own.)
for more info on how return enumeration, we'll need more information tickets after being fetched db. in list or construct list etc.?
Comments
Post a Comment