Right now I'm working my way thru the SportsStore exercise of Sanderson's "Pro ASP.Net MVC 2 Framework" book (page 107) and the exercise has me implementing a Repository pattern backed by a DB store using LINQ-to-SQL. I'm trying to figure out how to implement this same repository with the Subsonic ORM.
The basic repository code is as follows:
using System.Data.Linq;
namespace DomainModel.Concrete
{
public class SqlProductsRepository : IProductsRepository
{
private Table<Product> productsTable;
public SqlProductsRepository(string connectionString)
{
productsTable =
(new DataContext(connectionString)).GetTable<Product>();
}
public IQueryable<Product> Products
{
get { return productsTable; }
}
}
}
The LINQ-To-SQL specific part of the code is the line involving DataContext and GetTable, I think.
Does Subsonic have a similar mechanism to this? If so,
- what would the code look like?
- would I be able to use the methods available in System.Data.Linq's Table like:
- InsertOnSubmit()
- Attach()
- DeleteOnSubmit()
- SubmitChanges()
- etc.
UPDATE Got your message. Impressive Template, but I decided to try something simpler first:
using SubSonic.Repository;
namespace DomainModel.Concrete
{
public class SqlProductsRepository : IProductsRepository
{
private SimpleRepository repo;
private IQueryable<Product> products;
public IQueryable<Product> Products
{
get { return products; }
}
public DbProductsRepository(string subsonicDatastore)
{
repo = new SimpleRepository(subsonicDatastore, SimpleRepositoryOptions.RunMigrations);
this.Refresh();
}
public void SaveProduct(Product product) { SaveProduct(new List<Product>() { product }); }
public void SaveProduct(IEnumerable<Product> productList)
{
var newProducts = from product in productList
where product.ID == 0
select product;
var oldProducts = from product in productList
where product.ID > 0
select product;
// If it's a new Product, just add it to the Repo
repo.AddMany<Product>(newProducts);
// If it's old, just update it.
repo.UpdateMany<Product>(oldProducts);
// Refresh internal list of products, in case table has changed.
this.Refresh();
}
public void DeleteProduct(Product product) { DeleteProduct(new List<Product>() { product }); }
public void DeleteProduct(IEnumerable<Product> productList)
{
repo.DeleteMany<Product>(productList);
this.Refresh();
}
private void Refresh()
{
products = repo.All<Product>();
}
}
}
As far as I can tell, there is no drop-in replacement for DataContext, but there are other options that are just as easy.
I still plan on checking out your solution when I get more time. It seems more complex, but much more flexible/adaptable.
Thanks!