2010-10-28 13 views
0

現在我正在通過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的,但有一樣,可以輕易的其他選項。

我仍然計劃在更多時間檢查您的解決方案。這看起來更復雜,但更靈活/適應性更強。

謝謝!

回答

1

椒鹽脆餅 - 我們再次見面:)

我使用亞音速3,但與通用倉庫模式中使用它。這意味着我只有一個「工廠」來分配實體。它看起來有點像這樣:

public class SubSonicRepository<T> : IRepository<T> where T : new() 
{ 
    private readonly IQuerySurface _db; 

    public SubSonicRepository() 
     : this(SubsonicFrameworkObjectContextPerRequest.CurrentDatabase) 
    { 
    } 

    public SubSonicRepository(IQuerySurface db) 
    { 
     _db = db ?? DB.CreateDB(); 
    } 

    // lots of stuff omitted!! 

    private IQueryable<T> GetAll() 
    { 
     var result = _db.GetQuery<T>(); 
     return result; 
    } 

    T IRepository<T>.GetByKey(object key) 
    { 
     ITable tbl = _db.FindTable(typeof(T).Name); 

     var result = _db.Select.From(tbl) 
           .Where(tbl.PrimaryKey.Name).IsEqualTo(key) 
           .ExecuteSingle<T>(); 
     return result; 
    } 
} 

...有很多其他的代碼如下。 _repository在每個控制器(或服務層)中實例化,上面提到的場景以稍微不同的方式發生。

現在,如果我可以給你,讓我的T4模板副本,那麼你可以遵循它沒有我的消息通過消息咆哮:)

+0

很高興再次見到你。 ;)如果您想與我聯繫,請訪問我的網站(在我的個人資料中),然後點擊「與我聯繫」鏈接並給我發送消息。謝謝! – Pretzel 2010-10-28 16:25:21

+0

發送的消息... – 2010-10-28 16:46:30

+0

msg收到。檢查你的代碼。 – Pretzel 2010-10-28 19:44:54

相關問題