This is a migrated thread and some comments may be shown as answers.

Overriding OpenAccess Base methods

1 Answer 111 Views
Development (API, general questions)
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Shawn Krivjansky
Top achievements
Rank 1
Shawn Krivjansky asked on 18 Jun 2010, 01:01 AM
I have just started using the new ORM Visual Designer.  It is pretty awesome.  I have a number of questions, but one that is eating at me at the moment.

How would one go about adding their own logic (and extending what happens) when say a .SaveChanges occurs (or .Delete, or .Add) to the context??  Just for example...  What if I wanted to update a table of my own in the database when any UPDATE (.SaveChanges) happens...  What would I change...where?

Is this possible?

I understand at the field/property level you can do things to overide/extend what happens in the actual persisted generated business object classes, but the methods I am referring to (.SaveChanges, .Delete, .Add) are at the OpenAccessContextBase class which I don't have easy access to.

Any advice? 

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander
Telerik team
answered on 22 Jun 2010, 02:03 PM
Hi Shawn Krivjansky,

I believe the best way is just to derive from the generated context class and extend your custom context the way you would like:
public class MyEntityContext : NorthwindEntityDiagrams
{
    new public void SaveChanges()
    {
        //add any custom logic here
        base.SaveChanges();
    }
}
MyEntityContext context = new MyEntityContext();
 
context.Add(new Category() { CategoryName = "Food" });
context.SaveChanges();

You can even use the same method names, just make sure you call the relevant base method at the end. There will be no polymorphic behavior since the base context methods are not virtual but this should not be a problem. You can do this for any method that you may need to customize.
Hope that helps.

Greetings,
Alexander
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Development (API, general questions)
Asked by
Shawn Krivjansky
Top achievements
Rank 1
Answers by
Alexander
Telerik team
Share this question
or