4

I am working on a Web API project. I am calling a repository which is responsible for the database interaction. The repository interacts with a third party data source.

I want to implement dependency injection (DI) into the repository layer to inject the dependency of the third party data source,but how can I achieve this since there are no interfaces in that third party DLL?

I use the Unity framework.

The third party DLL includes just one class:

using System;
using System.Collections.Generic;

namespace MoviesLibrary
{
    public class MovieDataSource
    {
        public MovieDataSource();
        public int Create(MovieData movie);
        public List<MovieData> GetAllData();
        public MovieData GetDataById(int id);
        public void Update(MovieData movie);
    }
}

1 Answer 1

6

How does this 3rd party component relate to the repository? Is it a standalone service that's internally used by the repository? If so, it sounds like the 3rd party component itself is another dependency which should be abstracted.

You could do this by creating an interface of your own which reflects the business operations you intend to perform, and which this service would implement. It might match the service implementation exactly, but doesn't need to:

public interface IMovieService
{
    int Create(MovieData movie);
    List<MovieData> GetAllData();
    MovieData GetDataById(int id);
    void Update(MovieData movie);
}

Then you can abstract the dependency behind an implementation of this interface:

public class MovieService : IMovieService
{
    private MovieDataSource dataSource = new MovieDataSource();

    // methods simply delegate to the dataSource object
}

Now you have an interface which you can use for dependency injection, and which you can mock for testing when you want to test the repository without relying on the 3rd party component.

Sign up to request clarification or add additional context in comments.

4 Comments

I am bit confused.i don't have sound knowledge with DI and repository.I don't understand this line-How does this 3rd party component relate to the repository? Is it a standalone service that's internally used by the repository? The layers(webapi->repository->External DLL) i am using is not appropriate?
@little: If the 3rd party component is the repository implementation then it might not need to be injected/mocked at all. I thought you had a database repository which just needed a separate service component as a dependency to use for some of its functionality. But if the whole repository just delegates to the 3rd party component then there's really nothing to unit test, only integration test, so there's no need to inject it.
thanks david,just a small question,can i use repository between webapi and external data source layer?
@little: I'm not sure what you mean by that. Are you asking if you can use the repository pattern to abstract a data source? Yes, you absolutely can do that. Are you asking if you can share that implementation with other applications? Again, yes.

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.