c# - Implementation of Dependency injection -


i working on web api project. calling repository responsible database interaction. repository interacts third party data source.

i want implement dependency injection (di) repository layer inject dependency of third party data source,but how can achieve since there no interfaces in third party dll?

i use unity framework.

the third party dll includes 1 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);     } } 

how 3rd party component relate repository? standalone service that's internally used repository? if so, sounds 3rd party component dependency should abstracted.

you creating interface of own reflects business operations intend perform, , service implement. might match service implementation exactly, doesn't need to:

public interface imovieservice {     int create(moviedata movie);     list<moviedata> getalldata();     moviedata getdatabyid(int id);     void update(moviedata movie); } 

then can abstract dependency behind implementation of interface:

public class movieservice : imovieservice {     private moviedatasource datasource = new moviedatasource();      // methods delegate datasource object } 

now have interface can use dependency injection, , can mock testing when want test repository without relying on 3rd party component.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

visual studio 2010 - Connect to informix database windows form application -

android - Associate same looper with different threads -