Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Say I have a class like
C#
public class Student
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime EnrollmentDate { get; set; }
    }

How do I track different states to the object like if its New, modified, deleted, ucnahnged?
I am not using Entity framework but dapper dot net.
Posted
Comments
BillWoodruff 17-Mar-14 3:35am    
Are you saying you wish to track the state of each instance of the Student class ?
zeego 17-Mar-14 6:18am    
Yes Billy, exactly.
BillWoodruff 17-Mar-14 9:53am    
Are you satisfied with the solutions posted here so far ? I would approach this in a different way, but I have not used 'Dapper, or the 'Entity Framework in .NET. I'm reluctant to post a reply and code if I don't understand the context you are working in.

If you wish to see a different approach, just ask. And, please call me 'Bill :)
zeego 18-Mar-14 1:11am    
Thanks for your reply Bill. I would like to see your solution also, sorry about calling you Billy.

1.You could use Entity Framework to manage your entity objects, and all states will be managed automatically;
OR
2.You could add an enum for state then to use it in your class like in the next example:
C#
public enum ObjectStates
{
Unchanged,
New,
Modified,
Deleted
}

public class Student
    {
 
public ObjectStates State
{
get;
private set;
}
public int ID 
{ get; 
set
{
this.State= ObjectStates.Modified;}
}
//... in all properties in the similar way!

public void Delete()
{
this.State = ObjectStates.Deleted;
//..
}
public void Save()
{
this.State = ObjectsStated.Unchanged;
//...
}
public Student()
{
this.State = ObjectStates.New;
}

public Student(...some params)
{
this.State = ObjectStates.Unchanged;
//...
}
    }
 
Share this answer
 
v2
Comments
zeego 17-Mar-14 2:53am    
Thanks Raul. That seems to be a nice idea. Can you tell me how would I track changes to individual attributes of a class? Also, would this approach mean peppering all the save, update, delete methods with state attributes?
Raul Iloc 17-Mar-14 3:09am    
If you are talking about class attributes (see http://msdn.microsoft.com/en-us/library/sw480ze8(v=vs.71).aspx) they are designed to be used at runtime by the .NET platform and not to be modified at runtime.
zeego 17-Mar-14 5:45am    
Thanks for your reply Raul. I am talking about individual attributes like FirstName is changed & lastName is not changed. By your tracking methodology I would be able to track an object as whole. How would I go about tracking individual attributes?
Raul Iloc 17-Mar-14 7:48am    
Your question was unclear.
So in this case is you could have a set of "state properties", one for each property (like FirstNameState, LastNameState) and in the set part of the main property to set also the value for the state like in my solution.
Maybe this'd help you out :
ObjectStateManager[^], which
Quote:
Maintains object state and identity management for entity type instances and relationship instances.

Or rather, what pattern implementation, huh ? :)
Understanding and Implementing State Pattern in C#[^]

-KR
 
Share this answer
 
Have you met CSLA?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900