tags:

views:

484

answers:

1
+2  A: 

Just add the related tables to your repository interface, just as you have for Product and then create the concrete implementations in your repository class, again just as you have for Product.

I've used the same pattern on my app, I have two repositories, each handling 5-10 tables. There are two distinct groups of tables which are related, hence two repositories.

I would change the SQLRepository constructor thus:

    public SqlProductsRepository(string connectionString)
    {
        DataContext dc = new DataContext(connectionString);
        productsTable = dc.GetTable<Product>();
    }

You can then extend it easily thus e.g.:

    private Table<Order> ordersTable;

    public SqlProductsRepository(string connectionString)
    {
        DataContext dc = new DataContext(connectionString);
        productsTable = dc.GetTable<Product>();
        ordersTable = dc.GetTable<Order>();
    }

    IQueryable<Order> Orders
    {
        get { return from o in ordersTable select o; }
    }

EDIT - Answering comment

Here's an example of how to deliver subordinate objects (related tables) via this method:

[Table(Name="Projects")]
public class Project
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public Guid ID { get; set; }
    [Column]
    public String Name { get; set; }
    [Column]
    public bool Active { get; set; }

    [Association(ThisKey="ID", OtherKey = "ProjectID")]
    private EntitySet<ProjectDate> _projectDates = new EntitySet<ProjectDate>();
    public IQueryable<ProjectDate> ProjectDates
    {
        get { return _projectDates.AsQueryable(); }
    }
}

And the ProjectDate class for completeness

[Table(Name="ProjectDates")]
public class ProjectDate
{
    [Column(IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public Guid ID { get; set; }
    [Column]
    public Guid ProjectID { get; set; }
    [Column]
    public DateTime TargetDate { get; set; }
    [Column(CanBeNull = true)]
    public DateTime? ActualDate { get; set; }
    [Column(CanBeNull=true, IsDbGenerated = true)]
    public DateTime? Created { get; set; }

    private EntityRef<Project> _project;
    [Association(ThisKey = "ProjectID", Storage = "_project", OtherKey = "ID")]
    public Project Project
    {
        get { return _project.Entity; }
        set { _project.Entity = value; ProjectID = _project.Entity.ID; }
    }
}
Lazarus
Thanks for that.Sorry, I'm being stupid. How would I create the relationship between the 2 tables within the main object (e.g. so you could access an orders collection for a product as, for example, Product.Orders
Mad Halfling
See the additions above
Lazarus
thanks a lot for that, I've got some UI code to write but then I'll get on and try that.
Mad Halfling
Mad Halfling
There doesn't seem to be parity between the database data types and the C# data types. If I recall correctly I've had to use decimal for SQL floats but that may actually be overkill. It seems to throw the error at the AsQueryable call only because the LINQ isn't executed until then, most Linq errors not caught at design time will only emerge at the code line where you first try to access the data even though the actual error may be in the LINQ query itself.
Lazarus
Just thought, there must be a table about the different data types somewhere and lo and behold http://msdn.microsoft.com/en-us/library/ms131092.aspx. SQL Float is a C# double!
Lazarus
Thx for that.Niiiiiiiiiiiiiiiice, oh dear lord, what _are_ they doing?????Changed it into an int as that was more appropriate (the background context of the data is a little strange for this - don't worry why)
Mad Halfling
One more question (sorry to keep asking them). If I have created these objects as IQueryable, do I have to create methods (I'm thinking on the project object, in your example) to add, update, and delete entries from that list, or can I set up write access to the subordinate objects directly?
Mad Halfling