Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In DDD, you're missing the bigger picture here by referencing the concret classes. You are not interfacing between the Repository and "Services layer" by best practices. If you must have DataContext injected into the Repository, I would recommend refactoring to:</p> <pre><code>public interface IRepository { IList&lt;Orders&gt; GetNewOrders(); } public Repository : IRepository { private IDataContext _dataContext; public Repository(IDataContext dataContext) { _dataContext = dataContext; } public IList&lt;Orders&gt; GetNewOrders() { // perform your actions on _dataContext here } } </code></pre> <p>The better solution would be to let the Repository handle the DataContext on its own - keeping the seperation of concert valid by masking the underlying requirements: </p> <pre><code>public interface IRepository { IList&lt;Orders&gt; GetNewOrders(); } public Repository : IRepository { private IDataContext _dataContext; public Repository(String connectionString) { _dataContext = new DataContext(connectionString); } public IList&lt;Orders&gt; GetNewOrders() { // perform your actions on _dataContext here } } </code></pre> <p>If you must keep control of the DataContext (or another class) yourself (perhaps you want to keep a static reference around, or change settings based on an WebRequest, etc), you you will need to use a "Factory".</p> <p>The factory would look something like this: </p> <pre><code>public static class DataContextFactory { public static IDataContext GetInstance() { // return either a static instance, // or threaded instance, a GlobalContext instance // or whatever your preference is here // } } </code></pre> <p>That way, you have full control over how the instance of DataContext is controlled outside and away from your "Services" layer. So, you would use this DataContextFactory like the following:</p> <pre><code>public interface IRepository { IList&lt;Orders&gt; GetNewOrders(); } public Repository : IRepository { public IList&lt;Orders&gt; GetNewOrders() { using (var dataContext = DataContextFactory.GetInstance()) { // dataContext is now your IDataContext to work with } } } </code></pre> <h2>"How to access the IRepository?" you may ask?</h2> <p>Your services layer would do something like:</p> <pre><code>public void GetNewOrdersForServices() { // Not recommended! // IRepository repo = new Repository() // // The following is recommended instead; because, it removes the // the Concret reference from your Services layer completely! // IRepository repo = ServiceLocator.InstanceOf&lt;IRepository&gt;(); IList myList = repo.GetNewOrders(); } </code></pre> <p>Or, you would inject it into the constructor of your service using your favorite flavor of Inversion of Control container like so:</p> <pre><code>public class OrderService { private IRepository _repo; public OrderService(IRepository repo) { _repo = repo; } public void GetNewOrdersForServices() { IList myList = _repo.GetNewOrders(); } </code></pre> <p>If you are not fimilar with the service locator concepts, check out Castle Windsor as it Encapsulates just about all your needs. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload