How to transfer the save method to ActiveRecord?

I need to intercept the Save method, perform some checks, change some properties, and then let it return normally again.

How can i do this?

Thanks! Alex

+4
source share
4 answers

I would recommend adding the following partial methods before their actual action:

OnSave(CancelEventArgs e); OnAdd(CancelEventArgs e); OnUpdate(CancelEventArgs e); OnDelete(CancelEventArgs e); 

This is not an event, but I would use CancelEventArgs in any case, it is friendly, people know it and know how to use it, and with it the actual action can be undone from partial methods.

These two should also be added to the list of existing ones that fire after their actual action:

 OnAdded(); OnUpdated(); 

I do not like the name OnAdded (), but if Add was added instead of Insert, we must stick to it.

And what is it ... With these particles, I think that we cover all the prerequisites and the subsequent actual methods of data storage, providing us with great flexibility to do whatever we want, our data.

I can implement this, but I'm always afraid to touch the tt files, because future updates will erase all my user changes! :)

Thanks! Alex

+4
source

In 2.x, there were BeforeInsert and BeforeUpdate methods that you could override.

I think you need to add the OnSaving method to the ActiveRecord.tt file. Put this next to the OnSaved () line:

  partial void OnSaving(); 

Then update the ActiveRecord.tt file. 2 changes that I see:

Update update method

  public void Update(IDataProvider provider) { <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#> if(String.IsNullOrEmpty(this.ModifiedBy)) this.ModifiedBy=Environment.UserName; <#}#> <#if(tbl.Columns.Any(x=>x.Name=="ModifiedOn")){#> this.ModifiedOn=DateTime.Now; <#}#> **OnSaving();** if(this._dirtyColumns.Count>0) _repo.Update(this,provider); OnSaved(); } 

and then update the add method

 public void Add(IDataProvider provider) { <#if(tbl.Columns.Any(x=>x.Name=="CreatedOn")){#> this.CreatedOn=DateTime.Now; <#}#> <#if(tbl.Columns.Any(x=>x.Name=="CreatedBy")){#> if(String.IsNullOrEmpty(this.CreatedBy)) this.CreatedBy=Environment.UserName; <#}#> <#if(tbl.Columns.Any(x=>x.Name=="ModifiedOn")){#> this.ModifiedOn=DateTime.Now; <#}#> <#if(tbl.Columns.Any(x=>x.Name=="ModifiedBy")){#> if(String.IsNullOrEmpty(this.ModifiedBy)) this.ModifiedBy=Environment.UserName; <#}#> **OnSaving();** var key=KeyValue(); if(key==null){ var newKey=_repo.Add(this,provider); this.SetKeyValue(newKey); }else{ _repo.Add(this,provider); } SetIsNew(false); OnSaved(); } 

Finally, you need to use partial classes to override the OnSave () method to update the values. I am doing something very similar, since I do not use the modifiedby and createdon convention (I have underscores there).

+1
source

You can use the partial class mechanism to create your own version of Save, which checks and calls the SubSonic generated Save. eg.

 namespace YourNameSpace { public partial class YourTable : IActiveRecord { public void MySave() { // insert your validation here this.Save() } } } 
0
source

Each ActiveRecord class comes with a partial "OnSave ()" method, so all you have to do is create a partial and then override the partial OnSave () - as in Linq To Sql:

 public partial class MyClass{ partial OnSave(){ //magic } } 
0
source

All Articles