简体   繁体   English

SubSonic 动态模式:SelectList

[英]SubSonic Dynamic Schema: SelectList

Scenario设想

I am using SubSonic for my latest project.我正在为我的最新项目使用 SubSonic。 First things first;首先要做的事; this project is constrained to using .net 3.0 and SubSonic has been GREAT.该项目仅限于使用 .net 3.0 并且 SubSonic 非常棒。 I love it.我喜欢它。

However, I'm having some trouble with customization.但是,我在自定义方面遇到了一些麻烦。 The customer has requested that we use two SQL Server 2005 databases, one of which will replicate the other, minus some columns.客户要求我们使用两个 SQL Server 2005 数据库,其中一个将复制另一个,减去一些列。

At first glance this seems like a no brainer.乍一看,这似乎很简单。 However, SubSonic grabs all the columns by default, causing SQLExceptions.但是,SubSonic 默认抓取所有列,导致 SQLExceptions。 I would like to elegantly limit the select list on a table by table basis.我想优雅地逐表限制 select 列表。

For instance, the following code will do the trick, but I would rather not need to do so every time I new up a query:例如,下面的代码可以解决问题,但我宁愿每次新查询时都不需要这样做:

Query q = Post.Query().WHERE(Post.Columns.PageId, page_id);
if(UsingReplicatedDB)
    q.SetSelectList(ReplicatedPostColumnList);
return q.ExecuteReader();

The Question问题

The above method bloats my code and does not work for the builtin FetchByX methods generated by SubSonic.上述方法使我的代码膨胀,并且不适用于 SubSonic 生成的内置FetchByX方法。 Is there a way to elegantly set the default SelectList on a table by table basis?有没有办法在一个表格的基础上优雅地设置默认的 SelectList ?

Notes笔记

I have tried removing columns from the Schema at runtime in my partial classes, but columns looking like ColNameColumn look for specific indices in the Columns collection so my plan was foiled.我曾尝试在运行时在我的部分类中从架构中删除列,但是看起来像ColNameColumn的列会在Columns集合中查找特定索引,因此我的计划被挫败了。

I know, I know我知道我知道

Please no "You are an idiot, why would you do this?"请不要“你是个白痴,你为什么要这样做?” answers.答案。 I understand there are most likely better ways and this seems a bit hacky, but this is the point I'm at.我知道很可能有更好的方法,这似乎有点老套,但这就是我要说的。 I need a solution not badgering.我需要一个不纠缠的解决方案。

I don't think there's an easy way to get this to work.我不认为有一个简单的方法可以让这个工作。 You could create two SubSonic providers pointing to each DB, and then based on the situation, use the SubSonic objects to populate a separate model.您可以创建两个指向每个 DB 的 SubSonic 提供程序,然后根据情况,使用 SubSonic 对象填充单独的 model。 But SubSonic doesn't work the way you want since it's tied so closely to the db schema.但是 SubSonic 并不能按照您想要的方式工作,因为它与 db 架构紧密相关。

You do have a pretty sticky situation here.你在这里确实有一个非常棘手的情况。 Even if you do find a way to get your query to fetch the columns based on which database version you are using you now have to figure out which database you are using everywhere in your Bussiness Model or UI so that you don't reference columns which do not exist.即使您确实找到了一种方法来让您的查询根据您正在使用的数据库版本获取列,您现在也必须弄清楚您在业务 Model 或 UI 中的任何地方都在使用哪个数据库,这样您就不会引用哪些列不存在。

I would recommend using two SubSonic Providers and then having the Generated class each implement an Interface that contains Properties for the columns that are common to the two classes as well as the Subsonic Methods that you plan on using to interact with your Database such as Save();.我建议使用两个 SubSonic 提供程序,然后让生成的 class 每个实现一个接口,该接口包含两个类共有的列的属性以及您计划用来与数据库交互的 Subsonic 方法,例如 Save( );.

Sometimes when you want to customize something to work how you want it to work you need to write some custom code:)有时,当您想自定义某些东西以使其按您希望的方式工作时,您需要编写一些自定义代码:)

if you're open to using 3.0 (which is about to be released) I'd be happy to work with you on some TT template fixes that would do just this (ignore the rep columns).如果您愿意使用 3.0(即将发布),我很乐意与您合作解决一些 TT 模板修复程序,这些修复程序可以做到这一点(忽略 rep 列)。 Ping me at myname at gmail - I've been wanting to do this:).在 gmail 的 myname 上联系我 - 我一直想这样做:)。

3.0 would also allow you to use Linq and our neq Query tool - but I'd want to be sure it won't break what you're doing. 3.0 还允许您使用 Linq 和我们的 neq 查询工具 - 但我想确保它不会破坏您正在做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM