如何跟踪对象中的状态? [英] How do I track states in an object?

查看:83
本文介绍了如何跟踪对象中的状态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个类似

Say I have a class like

public class Student
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime EnrollmentDate { get; set; }
    }



如何跟踪对象的不同状态,如新的,修改的,删除的,ucnahnged?

我我没有使用实体框架,而是使用dapper dot net。


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.

推荐答案

1.您可以使用Entity Framework来管理您的实体对象,并且所有状态都将自动管理;



2.您可以为州添加枚举,然后在您的班级中使用它,如下一个示例所示:

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:
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;
//...
}
    }


也许这会帮助你:

ObjectStateManager [ ^ ],其中
Maybe this'd help you out :
ObjectStateManager[^], which
Quote:

维护实体类型实例和关系实例的对象状态和身份管理。

Maintains object state and identity management for entity type instances and relationship instances.



或者更确切地说,是什么模式实现,是吗? :)

了解和实现C#中的状态模式 [ ^ ]



- KR


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

-KR


你见过 CSLA 吗?


这篇关于如何跟踪对象中的状态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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