DI, concrete class with abstract, generic parent












0















We are using .NET Core's Dependency Injection and have the following class structure



public interface IValidator<TEntity, TMessageKey> {
ValidationResult<TMessageKey> Validate(TEntity entityToValidate);
}

public abstract class BaseValidator<TEntity, TMessageKey> : IValidator<TEntity, TMessageKey> {
// code omitted
}

public class ConcreteValidator : BaseValidator<ConcerteEntity, MyMessageKey>
{
// code omitted
}


of course there may be many concrete validators and I want to batch register them.



How would I achieve that?



Basically I want to register multiple closed generic types to one open generic type.



Every thing I tried failed so far.




  • Registering BaseValidator<,> to `ConcreteValidator

  • Registering IValidator<,> to BaseValidator<,>


I already read multiple articles/posts about it and I'm not even sure if it is even possible with .net core's DI



EDIT:
Here's my last try (I'm using NetCore.AutoRegisterDi extension)



var data = services.RegisterAssemblyPublicNonGenericClasses(Assembly.GetExecutingAssembly())
.Where(x => x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(BaseValidator<,>));

foreach(var d in data.TypesToConsider)
{
var type = typeof(BaseValidator<,>);
var genericType = type.MakeGenericType(d.BaseType.GenericTypeArguments);
services.AddScoped(genericType, d);
}









share|improve this question

























  • Ok, after some more researching, I came to the conclusion that I was way overthinking it. Since we inject the concrete class, all we have to do ist services.AddScoped(typeof(ConcreteValidator), typeof(ConcreteValidator)

    – Arikael
    Nov 14 '18 at 9:51











  • If your classes depend on a ConcreteValidator instead of an abstraction, you don't need the IValidator<T> abstraction at all. On the other hand, it will cause strong coupling between the consumer and the validator, which might make it more difficult to (unit) test and maintain your application.

    – Steven
    Nov 14 '18 at 13:10
















0















We are using .NET Core's Dependency Injection and have the following class structure



public interface IValidator<TEntity, TMessageKey> {
ValidationResult<TMessageKey> Validate(TEntity entityToValidate);
}

public abstract class BaseValidator<TEntity, TMessageKey> : IValidator<TEntity, TMessageKey> {
// code omitted
}

public class ConcreteValidator : BaseValidator<ConcerteEntity, MyMessageKey>
{
// code omitted
}


of course there may be many concrete validators and I want to batch register them.



How would I achieve that?



Basically I want to register multiple closed generic types to one open generic type.



Every thing I tried failed so far.




  • Registering BaseValidator<,> to `ConcreteValidator

  • Registering IValidator<,> to BaseValidator<,>


I already read multiple articles/posts about it and I'm not even sure if it is even possible with .net core's DI



EDIT:
Here's my last try (I'm using NetCore.AutoRegisterDi extension)



var data = services.RegisterAssemblyPublicNonGenericClasses(Assembly.GetExecutingAssembly())
.Where(x => x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(BaseValidator<,>));

foreach(var d in data.TypesToConsider)
{
var type = typeof(BaseValidator<,>);
var genericType = type.MakeGenericType(d.BaseType.GenericTypeArguments);
services.AddScoped(genericType, d);
}









share|improve this question

























  • Ok, after some more researching, I came to the conclusion that I was way overthinking it. Since we inject the concrete class, all we have to do ist services.AddScoped(typeof(ConcreteValidator), typeof(ConcreteValidator)

    – Arikael
    Nov 14 '18 at 9:51











  • If your classes depend on a ConcreteValidator instead of an abstraction, you don't need the IValidator<T> abstraction at all. On the other hand, it will cause strong coupling between the consumer and the validator, which might make it more difficult to (unit) test and maintain your application.

    – Steven
    Nov 14 '18 at 13:10














0












0








0








We are using .NET Core's Dependency Injection and have the following class structure



public interface IValidator<TEntity, TMessageKey> {
ValidationResult<TMessageKey> Validate(TEntity entityToValidate);
}

public abstract class BaseValidator<TEntity, TMessageKey> : IValidator<TEntity, TMessageKey> {
// code omitted
}

public class ConcreteValidator : BaseValidator<ConcerteEntity, MyMessageKey>
{
// code omitted
}


of course there may be many concrete validators and I want to batch register them.



How would I achieve that?



Basically I want to register multiple closed generic types to one open generic type.



Every thing I tried failed so far.




  • Registering BaseValidator<,> to `ConcreteValidator

  • Registering IValidator<,> to BaseValidator<,>


I already read multiple articles/posts about it and I'm not even sure if it is even possible with .net core's DI



EDIT:
Here's my last try (I'm using NetCore.AutoRegisterDi extension)



var data = services.RegisterAssemblyPublicNonGenericClasses(Assembly.GetExecutingAssembly())
.Where(x => x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(BaseValidator<,>));

foreach(var d in data.TypesToConsider)
{
var type = typeof(BaseValidator<,>);
var genericType = type.MakeGenericType(d.BaseType.GenericTypeArguments);
services.AddScoped(genericType, d);
}









share|improve this question
















We are using .NET Core's Dependency Injection and have the following class structure



public interface IValidator<TEntity, TMessageKey> {
ValidationResult<TMessageKey> Validate(TEntity entityToValidate);
}

public abstract class BaseValidator<TEntity, TMessageKey> : IValidator<TEntity, TMessageKey> {
// code omitted
}

public class ConcreteValidator : BaseValidator<ConcerteEntity, MyMessageKey>
{
// code omitted
}


of course there may be many concrete validators and I want to batch register them.



How would I achieve that?



Basically I want to register multiple closed generic types to one open generic type.



Every thing I tried failed so far.




  • Registering BaseValidator<,> to `ConcreteValidator

  • Registering IValidator<,> to BaseValidator<,>


I already read multiple articles/posts about it and I'm not even sure if it is even possible with .net core's DI



EDIT:
Here's my last try (I'm using NetCore.AutoRegisterDi extension)



var data = services.RegisterAssemblyPublicNonGenericClasses(Assembly.GetExecutingAssembly())
.Where(x => x.BaseType.IsGenericType && x.BaseType.GetGenericTypeDefinition() == typeof(BaseValidator<,>));

foreach(var d in data.TypesToConsider)
{
var type = typeof(BaseValidator<,>);
var genericType = type.MakeGenericType(d.BaseType.GenericTypeArguments);
services.AddScoped(genericType, d);
}






generics dependency-injection .net-core






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:44







Arikael

















asked Nov 14 '18 at 9:22









ArikaelArikael

6061527




6061527













  • Ok, after some more researching, I came to the conclusion that I was way overthinking it. Since we inject the concrete class, all we have to do ist services.AddScoped(typeof(ConcreteValidator), typeof(ConcreteValidator)

    – Arikael
    Nov 14 '18 at 9:51











  • If your classes depend on a ConcreteValidator instead of an abstraction, you don't need the IValidator<T> abstraction at all. On the other hand, it will cause strong coupling between the consumer and the validator, which might make it more difficult to (unit) test and maintain your application.

    – Steven
    Nov 14 '18 at 13:10



















  • Ok, after some more researching, I came to the conclusion that I was way overthinking it. Since we inject the concrete class, all we have to do ist services.AddScoped(typeof(ConcreteValidator), typeof(ConcreteValidator)

    – Arikael
    Nov 14 '18 at 9:51











  • If your classes depend on a ConcreteValidator instead of an abstraction, you don't need the IValidator<T> abstraction at all. On the other hand, it will cause strong coupling between the consumer and the validator, which might make it more difficult to (unit) test and maintain your application.

    – Steven
    Nov 14 '18 at 13:10

















Ok, after some more researching, I came to the conclusion that I was way overthinking it. Since we inject the concrete class, all we have to do ist services.AddScoped(typeof(ConcreteValidator), typeof(ConcreteValidator)

– Arikael
Nov 14 '18 at 9:51





Ok, after some more researching, I came to the conclusion that I was way overthinking it. Since we inject the concrete class, all we have to do ist services.AddScoped(typeof(ConcreteValidator), typeof(ConcreteValidator)

– Arikael
Nov 14 '18 at 9:51













If your classes depend on a ConcreteValidator instead of an abstraction, you don't need the IValidator<T> abstraction at all. On the other hand, it will cause strong coupling between the consumer and the validator, which might make it more difficult to (unit) test and maintain your application.

– Steven
Nov 14 '18 at 13:10





If your classes depend on a ConcreteValidator instead of an abstraction, you don't need the IValidator<T> abstraction at all. On the other hand, it will cause strong coupling between the consumer and the validator, which might make it more difficult to (unit) test and maintain your application.

– Steven
Nov 14 '18 at 13:10












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296726%2fdi-concrete-class-with-abstract-generic-parent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296726%2fdi-concrete-class-with-abstract-generic-parent%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python