Castle Windsor resolving and generics
Asked Answered
D

1

7

I have the following:

public interface ISubject { ... }

public class Subject<T> : ISubject { ... }

public class MyCode<T> {
    ...
    pulic void MyMethod()
    {
        var item = container.Resolve<ISubject>(); //????? how do I pass in T
    }
    ... 
 }

In this case how do i do the resolve.

Cheers Anthony

Delorasdelorenzo answered 16/5, 2009 at 13:15 Comment(0)
H
14

vdhant - That's not how containers are meant to be used.

You want to use ISubject, right?. Then if you passed T you're breaking your abstraction, because your caller must know that ISubject, is actually a Subject, and more than that, its a Subject<T> and that it requires a concrete T.

No container will allow that, but it's a design problem, not tool problem.

One thing to fix your design, would be to make it explicit - change ISubject to ISubject<T>

Then you could register open generic type ISubject<> and bind it to open generic type Subject<>.

container.Register(Component.For(typeof(ISubject<>))
                            .ImplementedBy(typeof(Subject<>)));

Then you'd be able to do

var fooSubject = container.Resolve<ISubject<Foo>>();

You didn't provide any context so I may be off the track with the answer, but one thing is for sure - you have a design problem.

Halfhour answered 16/5, 2009 at 13:48 Comment(7)
Cool so what i means is I need to provide a generic version of the interface and then I can do what I want to do... sounds good to me. CheersDelorasdelorenzo
Happy to help @EhsanSajjadHalfhour
@KrzysztofKozmic can you help me in understanding, how to integrate wcf all service contracts, or do i need to add all one by one like IUserService , IAuthenticateService etc??Sayette
@EhsanSajjad try WithServiceAllInterfaces or if you need more control, a custom predicateHalfhour
what i need is bind WCF services in web application asp.net mvc, i have researched but not found any proper example, though found some which were incompleteSayette
@EhsanSajjad Perhaps creating a specific question would help? We're hijacking comments here.Halfhour
@KrzysztofKozmic ok sure i will post questionSayette

© 2022 - 2024 — McMap. All rights reserved.