Note that there are some explanatory texts on larger screens.

plurals
  1. POLinq to SQL Repository question
    primarykey
    data
    text
    <p>I am working on a similar project like NerdDinner (www.nerddinner.com). I have similar tables, but I have used a generic repository (copied from here <a href="http://www.codeproject.com/KB/architecture/linqrepository.aspx" rel="nofollow noreferrer">http://www.codeproject.com/KB/architecture/linqrepository.aspx</a>). It seems working without any problem. Here are the codes.</p> <hr> <h2>IRepository.cs</h2> <pre><code>using System; using System.Collections.Generic; using System.Text; using System.Data.Linq; using System.Linq; namespace Listing.Repository { public interface IRepository&lt;T&gt; where T : class { /// &lt;summary&gt; /// Return all instances of type T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; IEnumerable&lt;T&gt; GetAllRows(); /// &lt;summary&gt; /// Return all instances of type T that match the expression exp. /// &lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; IEnumerable&lt;T&gt; GetAllRowsWhere(Func&lt;T, bool&gt; exp); /// &lt;summary&gt;Returns the single entity matching the expression. Throws an exception if there is not exactly one such entity.&lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt;&lt;returns&gt;&lt;/returns&gt; T Single(Func&lt;T, bool&gt; exp); /// &lt;summary&gt;Returns the first element satisfying the condition.&lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt;&lt;returns&gt;&lt;/returns&gt; T First(Func&lt;T, bool&gt; exp); /// &lt;summary&gt; /// Mark an entity to be deleted when the context is saved. /// &lt;/summary&gt; /// &lt;param name="entity"&gt;&lt;/param&gt; void Delete(T entity); /// &lt;summary&gt; /// Adds new entity.. /// &lt;/summary&gt; /// &lt;param name="entity"&gt;&lt;/param&gt; void Insert(T entity); void Update(T entity); /// &lt;summary&gt; /// Create a new instance of type T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; T New(); /// &lt;summary&gt;Persist the data context.&lt;/summary&gt; void SaveAll(); } } </code></pre> <hr> <h2>Repository.cs</h2> <pre><code>using System; using System.Data; using System.Data.Linq; using System.Data.Linq.Mapping; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; namespace Listing.Repository { public class Repository&lt;T&gt; : IRepository&lt;T&gt; where T : class { protected Listing.Data.IDataContextFactory _dataContextFactory; /// &lt;summary&gt; /// Return all instances of type T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public IEnumerable&lt;T&gt; GetAllRows() { return GetTable; } /// &lt;summary&gt; /// Return all instances of type T that match the expression exp. /// &lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public IEnumerable&lt;T&gt; GetAllRowsWhere(Func&lt;T, bool&gt; exp) { return GetTable.Where&lt;T&gt;(exp); } /// &lt;summary&gt;See _vertexRepository.&lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt;&lt;returns&gt;&lt;/returns&gt; public T Single(Func&lt;T, bool&gt; exp) { return GetTable.SingleOrDefault(exp); } /// &lt;summary&gt;See _vertexRepository.&lt;/summary&gt; /// &lt;param name="exp"&gt;&lt;/param&gt;&lt;returns&gt;&lt;/returns&gt; public T First(Func&lt;T, bool&gt; exp) { return GetTable.First(exp); } /// &lt;summary&gt;See _vertexRepository.&lt;/summary&gt; /// &lt;param name="entity"&gt;&lt;/param&gt; public virtual void Delete(T entity) { _dataContextFactory.Context.GetTable&lt;T&gt;().DeleteOnSubmit(entity); } /// &lt;summary&gt; /// Create a new instance of type T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public virtual T New() { T entity = Activator.CreateInstance&lt;T&gt;(); GetTable.InsertOnSubmit(entity); return entity; } /// &lt;summary&gt; /// Adds an insance T. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public virtual void Insert(T entity) { GetTable.InsertOnSubmit(entity); } /// &lt;summary&gt; /// Update entity. /// &lt;/summary&gt; /// &lt;returns&gt;&lt;/returns&gt; public virtual void Update(T entity) { _dataContextFactory.Context.Refresh(System.Data.Linq.RefreshMode.KeepCurrentValues, entity); } /// &lt;summary&gt;See _vertexRepository.&lt;/summary&gt; public void SaveAll() { _dataContextFactory.SaveAll(); } public Repository(Listing.Data.IDataContextFactory dataContextFactory) { _dataContextFactory = dataContextFactory; } #region Properties private string PrimaryKeyName { get { return TableMetadata.RowType.IdentityMembers[0].Name; } } private System.Data.Linq.Table&lt;T&gt; GetTable { get { return _dataContextFactory.Context.GetTable&lt;T&gt;(); } } private System.Data.Linq.Mapping.MetaTable TableMetadata { get { return _dataContextFactory.Context.Mapping.GetTable(typeof(T)); } } private System.Data.Linq.Mapping.MetaType ClassMetadata { get { return _dataContextFactory.Context.Mapping.GetMetaType(typeof(T)); } } #endregion } } </code></pre> <hr> <h2>IDataContext.cs</h2> <pre><code>using System; using System.Linq; namespace Listing.Data { public interface IDataContextFactory { System.Data.Linq.DataContext Context { get; } void SaveAll(); } public class DataContext : IDataContextFactory { #region IDataContextFactory Members private System.Data.Linq.DataContext dt; public System.Data.Linq.DataContext Context { get { if (dt == null) dt = new Listing.Data.ListingDataContext(); return dt; } } public void SaveAll() { dt.SubmitChanges(); } #endregion } } </code></pre> <hr> <p>Now I am in a stage where I have dropped a two functions DistanceBetween and NearestDinners(mine one's name is different), to my dbml file. I want to use these functions to do some calculation. </p> <p>Here are the codes used in nerddinner project </p> <pre><code>public IQueryable&lt;Dinner&gt; FindByLocation(float latitude, float longitude) { var dinners = from dinner in FindUpcomingDinners() join i in db.NearestDinners(latitude, longitude) on dinner.DinnerID equals i.DinnerID select dinner; return dinners; } </code></pre> <p>How would I achieve similar with my generic Repository. Help will be appreciated. </p> <p>Regards Paraminder</p>
    singulars
    1. This table or related slice is empty.
    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