2

I have 2 variables

  • Date-Time-Modified which is a Date-Time variables
  • Is-Deleted variable which is a boolean.

These two variables are found in each class I have and I need to have these variables initialized each time I insert, edit or delete an object from my database. Is there a way to do this?

Sayse
  • 42,633
  • 14
  • 77
  • 146
Jeff
  • 35
  • 4
  • Just my two-cents. I've always found that going down the path you are about to, problematic. Considering building a real Auditing database or a implement what you are about to do completely serverside via views. – Aron Jun 03 '14 at 07:52
  • You should also look at how to implement soft-delete using entity framework http://stackoverflow.com/a/18985828/150342 – Colin Jun 03 '14 at 09:21

3 Answers3

4

Use a base class. Add the 2 needed properties (DateTime, IsDeleted). Every derived class now holds this properties. You can set them individually or iterate over the base type to assign a value. You can also use events to trigger the update automatically. But i think you just want to create an entry and then set its DateTime property to the actual date time.

You also could delegate this task to the database. It will take care of setting this attributes.

An example for my lazy friend ;)

abstract class DatabaseEntryBase
{
    public DatabaseEntryBase()
    {
        // You can initialize properties to a default value here
        this.IsDeleted = false;
    }

    public DateTime ModifiedTime { get; set; }
    public bool IsDeleted { get; set; }
}

class Entry : DatabaseEntryBase
{}

static void Main()
{
    //-- Do your SQL stuff --//

    var newEntry = new Entry(); 
    newEntry.ModifiedTime = DateTime.Now;
    newEntry.IsDeleted = false;
}

If you need all instances to hold the same values (e.g. multiple deletes) push them into a collection (here EntryCollection of type List<DatabaseEntryBase>) and iterate over them instead:

public void SetAllItems()
{
    foreach (DatabaseEntryBase entry in EntryCollection)
    {
        entry.ModifiedTime = DateTime.Now;
        entry.IsDeleted = [...];
    }
}

Using an interface will accomplish the same! It could be named ITaggable and defines this two properties as a contract.

BionicCode
  • 1
  • 4
  • 28
  • 44
2

Have a base class that has these members and then have the other classes extend that one.

Then have a method on the base that initalises them. Then call that method on the inserts and such.

 base.InitialiseVariables()
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
  • I have already did that what I need is to have the current date and time each time I do a CRUD operation – Jeff Jun 03 '14 at 07:33
  • That is a very good idea. Is there any other way to just initialize them without the need of calling a method? – Jeff Jun 03 '14 at 07:36
  • @user3702061 - you could call that method from the constructor of the class. – Hans Kesting Jun 03 '14 at 07:43
  • @HansKesting `constructor` is called when an instance is created for a class, and not while calling other methods using that instance. – Bharadwaj Jun 03 '14 at 07:47
0

Is there any other way to just initialize them without the need of calling a method?

Probably another approach for this is Aspect-Oriented Programming. For example, PostSharp.

Code will look something like this:

[UpdateTimestamp]
public void InsertProduct(Product product)
{
    // Your logic here
}
artplastika
  • 1,972
  • 2
  • 19
  • 38