From edfa5cdffb6a47d1f38b633dfce6b273d394d92e Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Thu, 29 Apr 2021 22:13:29 +0300 Subject: [PATCH] Fail compilation with helpful message when trying to register class by supertype it does not inherit from --- CHANGES.md | 3 +++ source/poodinis/container.d | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 174e4e1..60ca11c 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,8 @@ Poodinis Changelog ================== +**Version 8.1.0-beta.4** +* ADD compilation failure when trying to register a class by a supertype it does not inherit from. (#31) + **Version 8.1.0-beta.3** * CHANGE injection initializers to be defined as a registration scope instead of via Container.register(). See initializedBy(). * ADD initializedOnceBy() to create singleton instances via injection initializer. diff --git a/source/poodinis/container.d b/source/poodinis/container.d index eca6446..a25d838 100644 --- a/source/poodinis/container.d +++ b/source/poodinis/container.d @@ -25,6 +25,7 @@ import std.string; import std.algorithm; import std.concurrency; import std.traits; +import std.meta : AliasSeq; debug { import std.stdio; @@ -194,6 +195,12 @@ synchronized class DependencyContainer { return newRegistration; } + public Registration register(SuperType, ConcreteType)(RegistrationOption options = RegistrationOption.none) + if (!is(SuperType == ConcreteType) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(Object, SuperType)) && !is(BaseTypeTuple!ConcreteType == AliasSeq!(SuperType))) { + pragma(msg, "Cannot register dependency: ", ConcreteType, " is not derived from ", SuperType); + static assert(0, "Cannot register dependency"); + } + private bool hasOption(OptionType)(OptionType options, OptionType persistentOptions, OptionType option) { return ((options | persistentOptions) & option) != 0; }