SubSonic 3的N层“脏列”集合在更新时始终为空 [英] N Tiers with SubSonic 3, Dirty Columns collection is always empty on update

查看:90
本文介绍了SubSonic 3的N层“脏列”集合在更新时始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在做什么,而不是为我工作。



我有一个用SubSonic 3 ActiveRecord模板生成的DAL,我有一个服务层(业务层)



说我在Service层上有一个方法,例如public void UpdateClient(Client client);我在GUI中创建
,我创建了一个Client对象,用ID填充了一些数据,并将其传递给service方法,但此方法从未起作用,脏列集合(跟踪哪些列被更改以便使用更有效的更新语句) )总是空的。



如果我试图从GUI内的数据库中获取对象,然后将其传递给服务方法,则它也不起作用。



我发现唯一可行的方案是,如果我从数据库中查询对象并在GUI内的同一上下文中调用Update(),这将破坏我创建的整个服务层。 / p>

但是对于插入和删除一切正常而言,我想知道这是否与对象跟踪有关,但是我知道SubSonic不会这样做。



请咨询。
谢谢。
Adel。

解决方案

这似乎是一个功能,而不是错误。如果对象是从数据库中加载的,则Subsonic仅将列标记为脏列。因此,您实际上无法保存并反对,对其属性添加更改,然后再次保存。

  bool _MyProp; 
public bool MyProp
{
get {return _MyProp; }
set
{
if(_MyProp!= value){
_MyProp = value;
var col = tbl.Columns.SingleOrDefault(x => x.Name == MyProp);
if(col!= null){
if(!_ dirtyColumns.Any(x => x.Name == col.Name)&& ** _ isLoaded **){
_dirtyColumns.Add(col);
}
}
OnChanged();
}
}
}

查看成员变量_isLoaded的方式除非对象确实是从数据库加载的,否则为false。 Subsonic永远不会将我的属性添加到脏列列表中。



您的存储库Save()方法必须看起来像这样:

  public MyObject Save(Myobject myObject)
{
myObject.Save();
myObject = MyObject.SingleOrDefault(x => x.Id == myObject.Id);
返回myObject;
}


Here is what I am doing, and not working for me.

I have a DAL generated with SubSonic 3 ActiveRecord template, I have a service layer (business layer if you well) that have mixture of facade and some validation.

Say I have a method on the Service layer like public void UpdateClient(Client client); in my GUI i create a Client object fill it with some data with ID and pass it to the service method and this never worked, the dirty columns collection (that track which columns are altered in order to use more efficient update statement) is always empty.

If I tried to get the object from database inside my GUI then pass it to the service method it's not working either.

The only scenario I find working is if I query the object from the database and call Update() on the same context all inside my GUI and this defeats the whole service layer I've created.

However for insert and delete everything working fine, I wonder if this have to do something with object tracking but what I know is SubSonic don't do that.

Please advice. thanks. Adel.

解决方案

It seems to be a feature and not a bug. Subsonic will only mark a column as dirty if the object is loaded from DB. So you can't really save and object, add changes to its properties and then save it again. Go figure.

bool _MyProp;
public bool MyProp
{
    get { return _MyProp; }
    set
    {
        if(_MyProp!=value){
            _MyProp=value;
            var col=tbl.Columns.SingleOrDefault(x=>x.Name=="MyProp");
            if(col!=null){
                if(!_dirtyColumns.Any(x=>x.Name==col.Name) && **_isLoaded**){
                    _dirtyColumns.Add(col);
                }
            }
            OnChanged();
        }
    }
}

See how the member variable _isLoaded is false unless the object really is loaded from DB. Subsonic never adds my property to the list of dirty columns.

Your repository Save() method would have to look something like this:

public MyObject Save(Myobject myObject)
{
    myObject.Save();
    myObject= MyObject.SingleOrDefault(x => x.Id == myObject.Id);
    return myObject;
}

这篇关于SubSonic 3的N层“脏列”集合在更新时始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆