Your DI framework is killing your code
31–40 of 59 posts
Re: Your DI framework is killing your code
#32Re: Your DI framework is killing your code
#33Yet another time it comes up. But now, after functional programming being commonplace, we at least know the answer to the "why" question. Consider the "classical OOP" method signature: email.send() vs "the anemic way" emailSender.send(message: Email) If there is only one way to send email (SMTP), things works fine. But let's assume we have to implement another way (e.g. Mailgun API). In the "anemic" case everything i…
An email doesn't send itself nor does it interact with the transport. The transport (MUA/MTA for example) is responsible for delivering it via whatever method it happens to support. The container delivers the transport to the code. The consuming code doesn't care what the transport and method does, merely introduce the two and let them make sweet email love.
The article stinks of circa 2002 purist babble.
Re: Your DI framework is killing your code
#34Re: Your DI framework is killing your code
#35Yet another time it comes up. But now, after functional programming being commonplace, we at least know the answer to the "why" question. Consider the "classical OOP" method signature: email.send() vs "the anemic way" emailSender.send(message: Email) If there is only one way to send email (SMTP), things works fine. But let's assume we have to implement another way (e.g. Mailgun API). In the "anemic" case everything i…
Spot on. You replace your "anemic way" with first-class functions and namespacing, it ends up being the same thing. With currying, you do away with OO and parameter initialization altogether. @curry def SMTPSender(host, message): ... emailSender = SMTPSender(host='localhost') # partially evaluated fn with `host` fixed emailSender(message=message)
emailSender = SMTPSender(host='localhost') # in container
emailSender.send(message) # in implementation
The consumer of the SMTPSender only interacts with the send() method.Re: Your DI framework is killing your code
#36Earlier quoted context omitted.
For example?
Couldn't you just add a new SendMethod optional parameter defaulted to the method that was used prior to the refactoring? No breaking API changes, no extensive manual refactoring of the entire codebase, as far as I can tell. This is how I always did refactoring for years, it's immediately obvious to support developers wtf is going on, and there's no requirement to have any "complicated" architecture on every single o…
Adding an optional parameter to an already existing method is not always a backwards-compatible change. For example, in C# this will effectively change method signature and break API.
Re: Your DI framework is killing your code
#37Say you have a class CustomerHandler (stupid example, pseudo-C#) with a constructor like this:
public CustomerHandler(Guid customerId, ILogger logger, ISomeExtraDependency whatever) { ... }
Now, let's say another class needs to create CustomerHandlers for specific customers, but doesn't want to care how they are created. Autofac, the .NET DI framework I'm most familiar with, let's you inject factory delegates: public RequestProcessor(Func
(Every type not mentioned in the Func is auto-injected.) This is very convenient because you can change the dependencies of CustomerHandler without messing up the rest of the code.Of course, even in the absence of such an auto-factory feature, you can still create a dedicated CustomerHandlerFactory class and inject that. That's not quite as convenient, but it's still a lot better to have only one extra place that has to know CustomerHandler's dependencies than having them all over your codebase.
Re: Your DI framework is killing your code
#38Yet another time it comes up. But now, after functional programming being commonplace, we at least know the answer to the "why" question. Consider the "classical OOP" method signature: email.send() vs "the anemic way" emailSender.send(message: Email) If there is only one way to send email (SMTP), things works fine. But let's assume we have to implement another way (e.g. Mailgun API). In the "anemic" case everything i…
Re: Your DI framework is killing your code
#39Earlier quoted context omitted.
For example?
A 'sending mechanism' property on the email object that defaults to SMTP?
Re: Your DI framework is killing your code
#40Yet another time it comes up. But now, after functional programming being commonplace, we at least know the answer to the "why" question. Consider the "classical OOP" method signature: email.send() vs "the anemic way" emailSender.send(message: Email) If there is only one way to send email (SMTP), things works fine. But let's assume we have to implement another way (e.g. Mailgun API). In the "anemic" case everything i…