現在我正在通過Sanderson的「Pro ASP.Net MVC 2框架」一書(第107頁)的SportsStore練習,並且練習讓我使用LINQ-to-SQL實現由DB商店支持的Repository模式。我試圖找出如何使用Subsonic ORM實現這個存儲庫。Subsonic3和ASP.NET MVC:有沒有像DataContext.GetTable <T>?
基本信息庫的代碼如下:
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; }
}
}
}
代碼的LINQ到SQL特定部分涉及的DataContext和GetTable行了,我想。
亞音速是否有類似的機制呢?如果是這樣,
- 代碼是什麼樣子?
- 我將能夠使用可用的方法將System.Data.Linq的表像:
- InsertOnSubmit()
- 連接()
- DeleteOnSubmit()
- 的SubmitChanges()
- 等。
UPDATE 收到您的消息。令人印象深刻的模板,但我決定嘗試一些簡單的第一:
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>();
}
}
}
據我所知,沒有直接替代了DataContext的,但有一樣,可以輕易的其他選項。
我仍然計劃在更多時間檢查您的解決方案。這看起來更復雜,但更靈活/適應性更強。
謝謝!
很高興再次見到你。 ;)如果您想與我聯繫,請訪問我的網站(在我的個人資料中),然後點擊「與我聯繫」鏈接並給我發送消息。謝謝! – Pretzel 2010-10-28 16:25:21
發送的消息... – 2010-10-28 16:46:30
msg收到。檢查你的代碼。 – Pretzel 2010-10-28 19:44:54