【问题标题】:Subsonic3 and ASP.NET MVC: Is there something like DataContext.GetTable<T>?Subsonic3 和 ASP.NET MVC:有没有类似 DataContext.GetTable<T> 的东西?
【发布时间】:2010-10-28 15:54:09
【问题描述】:

现在,我正在通过 Sanderson 的“Pro ASP.Net MVC 2 Framework”一书(第 107 页)的 SportsStore 练习,该练习让我使用 LINQ-to- 实现由数据库存储支持的存储库模式SQL。我正在尝试弄清楚如何使用 Subsonic ORM 实现相同的存储库。

基本仓库代码如下:

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-To-SQL 的部分是涉及 DataContext 和 GetTable 的那一行。

Subsonic 有类似的机制吗?如果是这样,

  • 代码是什么样的?
  • 我能否使用 System.Data.Linq 表中提供的方法,例如:
    • InsertOnSubmit()
    • 附加()
    • DeleteOnSubmit()
    • 提交更改()

更新 收到你的消息。令人印象深刻的模板,但我决定先尝试一些更简单的东西:

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 没有直接替代品,但还有其他同样简单的选项。

当我有更多时间时,我仍然计划检查您的解决方案。它看起来更复杂,但更灵活/适应性更强。

谢谢!

【问题讨论】:

    标签: asp.net-mvc linq-to-sql subsonic subsonic3 datacontext


    【解决方案1】:

    椒盐卷饼 - 我们又见面了 :)

    我使用 subsonic 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 模板的副本交给你,那么你就可以在没有我的消息的情况下通过消息咆哮来关注它:)

    【讨论】:

    • 很高兴再次见到你。 ;) 如果您想联系我,请访问我的网站(在我的 SO 个人资料中),然后单击“联系我”链接并向我发送消息。谢谢!
    • 我做了一个快速的基本说明指南。充满了漏洞,但可能会让你在这个过程中一瘸一拐。我已将链接通过电子邮件发送给您...
    猜你喜欢
    • 2014-02-09
    • 1970-01-01
    • 2020-03-11
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多