Right way to use Interfaces in Large Scale Blazor Server Web Applications
Hello there, iam currently working on a <see title> app for something iam not allowed to leak now iam wondering how to use Interfaces correctly? I used them to make Contracts for Models or similar. But today i looked through some Templates we could potentionally use and i often see in the pages with code behind files the term
but iam not at all sure what to use that for or how its supposed to work as Interfaces dont have any kind of function bodys anyone can give a conclusion or so? ive also searched about it in StackExchange and StackOverflow but both times likely 0 results for this exact use case.
Edit: please ping me when you have a solution or similar i doubt i will watch this actively as its pretty a big server i cant read every message
11 Replies
Interfaces don't have function bodies but they can have function definitions. Classes which implement the interface then provide a function body
e.g.
this lets another class work against an
IEmailSender
and not have to worry about which specific email specification it's using internallybut wouldnt that use both function bodys if the interface is implemented in both classes?
no. PopEmailSender and ImapEmailSender are both IEmailSenders, but a given IEmailSender is not both of those two
in the same way that a given animal is not a dog and a cat and a horse simultaneously
@Dropps
ah i see
but what when you execute that function bdoy with the interface itself i.e.
when you do that, the runtime still 'knows' what the actual type of
_interface
is
and thus knows which function to go to
your code deliberately limits itself to only knowing that it's IEmailSender
, but .NET itself still knows what it really isyes but multiple classes implement that interface
each called SendEmail
yes - but why would that be a problem
@Dropps
in case of using sharding servers where everything exists more than once and gets processed nearly to realtime
running the same application with multiple servers (backend too) to increase performance
this kind of polymorphism will make absolutely zero performance difference
also... you still control what actually gets sent into that class
functionally it's as if you passed in an actual instance of the concrete type
i don't see the problem
multiple servers run the same apps connected to the same cluster of api servers and co
now when we send things throught that interface it would possibly get processed by all servers at the same and in the example above send N emails for N in servercluster
ok
i still don't say why that would make a difference
you're already passing some concrete type in for that interface lol