0

So I have this basic Interface:

public interface ISearchable
{
    List<AutocompleteResult> Search(string term);
}

And two classes which implement this interface:

First Class:

public class LandlordReader :ISearchable
{
    public List<AutocompleteResult> Search(string term)
    {
        ....
    }
}

Second Class:

public class CityReader : ISearchable
{
    public List<AutocompleteResult> Search(string term)
    {
       ....
    }
}

From this point on, I'm in the dark... In UnityConfig.cs I tried to register them by giving them a name:

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<ISearchable, CityReader>("City");
    container.RegisterType<ISearchable, LandlordReader>("Landlord");
}

In the MVC Controller I have no idea on what to specify (IN THE CONSTRUCTOR) - to tell it that I want one or another:

public LandlordController(ISearchable searcher)
{
    // I want the LandlordReader here, but in the CityController, I want the CityReader....
    this.searcher = searcher;
}

I'm quite a noob, regarding DI and UnityInjector, so a full working example would be good. I got lots of advices up to this point (use generic interface, use unity factory) - but trying them myself, none worked.

I know in ASP.NET Core there's an attribute you can directly specify in the constructor's parameter list, but I am currently working in MVC5.

Thanks. Finished a long post. Phew.

3
  • It would be a good idea to separate the interfaces. Even if they have the same signature, that doesn't mean they are the same interface. Use ICitySearchable and ILandlordSearchable and you are good to go. Commented Feb 12, 2020 at 9:35
  • Why? I am going to implement two interfaces in CityReader (ISearchable + ICityReader). Isn't it a better class design if I implement from multiple interfaces? Commented Feb 12, 2020 at 10:44
  • Just because they have the same signature doesn't mean they are doing the same thing. Searching for a Landlord and Searching for a City are not just different implementations, they are two completely different things. They should reside (or inherit or expand) different interfaces. Commented Feb 13, 2020 at 8:41

1 Answer 1

1

You can try one of these options if suitable in your scenario.

1

Using Named Serivce Injection

public static void RegisterTypes(IUnityContainer container)
{
    container.RegisterType<ISearchable, CityReader>("City");
    container.RegisterType<ISearchable, LandlordReader>("Landlord");
}

public LandlordController([Dependency("Landlord")]ISearchable searcher)
{
    this.searcher = searcher;
}

2

public interface ISearchable<T> where T : class
{
//..............
}

public class LandlordReader : ISearchable<LandlordReader>
{
    public List<AutocompleteResult> Search(string term)
    {
        ....
    }
}

public class CityReader : ISearchable<CityReader>
{
    public List<AutocompleteResult> Search(string term)
    {
       ....
    }
}
public static void RegisterTypes(IUnityContainer container)
{
        container.RegisterType<ISearchable, CityReader>("City");
        container.RegisterType<ISearchable, LandlordReader>("Landlord");
}

and then when using in the constructor do this

    private readonly ISearchable<CityReader> _cityReadersearcher;
    private readonly ISearchable<LandlordReader> _landLordsearcher;
    public LandlordController(ISearchable<CityReader> cityReadersearcher, 
     ISearchable<LandlordReader> landLordsearcher)
    {
         _cityReadersearcher= _cityReadersearcher;
        _landLordsearcher = landLordsearcher;
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it worked (I've used the 1st option) - the 2nd one, I've figured it out by myself but it seems a little bit hackish. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.