C
C#3y ago
Dropps

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
[Inject]
public InterfaceName _interface { get; set; }

...

_interface.SomeMethod()
[Inject]
public InterfaceName _interface { get; set; }

...

_interface.SomeMethod()
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
Becquerel
Becquerel3y ago
Interfaces don't have function bodies but they can have function definitions. Classes which implement the interface then provide a function body e.g.
public interface IEmailSender
{
void SendEmail(string text, string address);
}

public class PopEmailSender : IEmailSender
{
void SendEmail(string text, string address)
{
// ...
}
}

public class ImapEmailSender : IEmailSender
{
void SendEmail(string text, string address)
{
// ...
}
}
public interface IEmailSender
{
void SendEmail(string text, string address);
}

public class PopEmailSender : IEmailSender
{
void SendEmail(string text, string address)
{
// ...
}
}

public class ImapEmailSender : IEmailSender
{
void SendEmail(string text, string address)
{
// ...
}
}
this lets another class work against an IEmailSender and not have to worry about which specific email specification it's using internally
Dropps
DroppsOP3y ago
but wouldnt that use both function bodys if the interface is implemented in both classes?
Becquerel
Becquerel3y ago
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
public void DoSomething(IEmailSender sender)
{
// This could be PopEmailSender or ImapEmailSender.
// I don't know which and don't care which.
// When somebody calls this method, they
// have to choose which they give me.
sender.SendEmail("hello", "[email protected]");
}

public void Blah()
{
// Choose an implementation, treating it as the interface.
IEmailSender sender = new PopEmailSender();

// Give it to someone who needs it.
DoSomething(sender);
}
public void DoSomething(IEmailSender sender)
{
// This could be PopEmailSender or ImapEmailSender.
// I don't know which and don't care which.
// When somebody calls this method, they
// have to choose which they give me.
sender.SendEmail("hello", "[email protected]");
}

public void Blah()
{
// Choose an implementation, treating it as the interface.
IEmailSender sender = new PopEmailSender();

// Give it to someone who needs it.
DoSomething(sender);
}
@Dropps
Dropps
DroppsOP3y ago
ah i see but what when you execute that function bdoy with the interface itself i.e.
_interface.SendEmail(blah , blah)
_interface.SendEmail(blah , blah)
Becquerel
Becquerel3y ago
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 is
Dropps
DroppsOP3y ago
yes but multiple classes implement that interface each called SendEmail
Becquerel
Becquerel3y ago
yes - but why would that be a problem @Dropps
Dropps
DroppsOP3y ago
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
Becquerel
Becquerel3y ago
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
Dropps
DroppsOP3y ago
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
Becquerel
Becquerel3y ago
ok i still don't say why that would make a difference you're already passing some concrete type in for that interface lol
Want results from more Discord servers?
Add your server