Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to implement Repository Pattern with interface, base and concrete
    primarykey
    data
    text
    <p>I have almost completed implementing my repository pattern by having a <code>IRepository&lt;T&gt;</code> interface, a <code>NewsRepository</code> class and a <code>News</code> entity. The problem I ran into was trying to abstract out common methods to a base Repository class. </p> <p>I could not find a way to abstract the Get method in the <code>NewsRepository</code> as it contains a specific Linq expression.</p> <p><strong>My questions are</strong>:</p> <p><strong>1)</strong> How do I abstract to a base class the <code>public T Get(int id)</code> method please? The only way I have done it so far is by passing in <code>Expression&lt;Func&lt;T,bool&gt;&gt;</code> instead of an int, but then that deosn't really abstract out common behaviour as each sub-class will still need to pass in an expression that is almost identical in each case ie <code>n =&gt; n.id == id</code>.</p> <p><strong>2)</strong> How do I pass into the base class on the Update method the sub-class GetViolations and map methods please? I imagine the solution is possibly by using delegates, but I couldn't get the syntax to compile</p> <p>This is a simplified set of the code - in practice I have a Save method which does Update and Insert rather than just the Update shown here.</p> <pre><code>public interface IRepository&lt;T&gt; { T Get(int id); void Update(T item); } public class NewsRepository : IRepository&lt;News&gt; { private Table&lt;News&gt; _newsTable; public NewsRepository(string connectionString) { _newsTable = new DataContext(connectionString).GetTable&lt;News&gt;(); } public News Get(int id) { return _newsTable.SingleOrDefault(n =&gt; n.NewsId == id); } public void Update(News item) { var errors = item.GetRuleViolations(); if (errors.Count &gt; 0) throw new RuleException(errors); News dbNews = _newsTable.SingleOrDefault(n =&gt; n.NewsId == item.NewsId); map(dbNews, item); _newsTable.Context.SubmitChanges(); } private void map(News dbNews, News news) { dbNews.Title = news.Title; dbNews.Article = news.Article; } } public class Repository&lt;T&gt; where T : class { protected Table&lt;T&gt; _table; public Repository(Table&lt;T&gt; t) { _table = t; } //How do i do this??! - This doesn't compile due to T no having a NewsId public T Get(int id) { return _table.SingleOrDefault(n =&gt; n.NewsId == id); } //This seems to be a solution, but it's not really abstracting common behaviour as each //sub-class will still need to pass in the same linq expression... public T Get(Expression&lt;Func&lt;T,bool&gt;&gt; ex) { return _table.SingleOrDefault(ex); } public void Update(T item) { //How is it possible to pass in the GetRuleViolations and map functions to this method? var errors = item.GetRuleViolations(); if (errors.Count &gt; 0) throw new RuleException(errors); T dbNews = _table.SingleOrDefault(n =&gt; n.NewsId == item.NewsId); map(dbNews, item); _table.Context.SubmitChanges(); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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