_:ndb360da320034a699821741898c1a9c1xb2
<http://schema.org/text>
" <p>1. What is the difference of using UnitOfWork (Class that comes from MyProject.Data - class library) vs IUnitOfWork?<br> <br> -&gt; UnitOfWork is class with methods defined in it while IUnitofWork is interface like below</p> <pre class='x_language-csharp'><code>public interface IUnitOfWork {     IGenericRepository&lt;Blog&gt; BlogRepository { get; }     IGenericRepository&lt;Post&gt; PostRepository { get; }     void Save(); }</code></pre> <p>UnitOfWork Class<br> <br> </p> <pre class='x_language-csharp'><code>public class UnitOfWork : IUnitOfWork {     private readonly BloggingContext _bloggingContext;     private IGenericRepository&lt;Blog&gt; _blogRepository;     private IGenericRepository&lt;Post&gt; _postRepository;     public UnitOfWork(BloggingContext bloggingContext)     {         _bloggingContext = bloggingContext;     }     public IGenericRepository&lt;Blog&gt; BlogRepository     {         get         {             return _blogRepository = _blogRepository ?? new GenericRepository&lt;Blog&gt;(_bloggingContext);         }     }     public IGenericRepository&lt;Post&gt; PostRepository     {         get         {             return _postRepository = _postRepository ?? new GenericRepository&lt;Post&gt;(_bloggingContext);         }     }     public void Save()     {         _bloggingContext.SaveChanges();     } }</code></pre> <p>2. Why they often use interface(base) when doing dependency injection in constructor instead of class.<br> <br> -&gt; May be you are referring this line of code in configuration services</p> <pre class='x_language-csharp'><code>services.AddScoped&lt;IUnitOfWork, UnitOfWork&gt;();</code></pre> <p>Here we are actually registering the services for UnitOfWork which we have created using above class and Interface, to know more details about it, read this</p> <p><a href='https://stackoverflow.com/questions/38138100/what-is-the-difference-between-services-addtransient-service-addscope-and-servi'>https://stackoverflow.com/questions/38138100/what-is-the-difference-between-services-addtransient-service-addscope-and-servi</a></p> <p>3. How IUnitOfWork works like class even its interface (interface is only declaration why it gives me implementation), as i run the api it gives me the result i wanted, but im wondering how interface has implementation based on my given example.<br> <br> -&gt; You are missing some info here, only IUnitOfWork cannot provide your implementation, I cannot say what you have missed without complete project code, but you are right IUnitOfWork cannot provide you complete implementaion it is just interface.</p> "
<https://qawithexperts.com/questions/323/aspnet-core---dependency-injection>