如何跟踪业务对象的更改?

时间:2021-04-05 20:10:23

I get the concept of creating a business object or entity to represent something like a Person. I can then serialize the Person using a DTO and send it down to the client. If the client changes the object, it can have an IsDirty flag on there so when it gets sent back to the server I know to update it.

我得到了创建业务对象或实体来表示类似Person的概念。然后我可以使用DTO序列化Person并将其发送给客户端。如果客户端更改了对象,它可以在那里有一个IsDirty标志,所以当它被发送回服务器时,我知道要更新它。

But what if I have an Order object? This has the main header informaton, customer, supplier, required date, etc. Then it has OrderItems which is a List< OrderItem>, being the items to be ordered. I want to be able to use this business object on my UI. So I have some textboxes hooked up to the location, supplier, required date, etc and a grid hooked up to OrderItems. Since OrderItems is a List I can easily add and delete records to it. But how do I track this, especially the deleted items. I don't want the deleted items to be visible in my grid and I shouldn't be able to iterate over them if I used foreach, because they have been deleted. But I still need to track the fact there was a deletion. How do I track the changes. I think I need to use a unit of work? But then the code seems to become quite complex. So then I wonder why not simply use DataTables and get the change tracking for free? But then I read how business objects are the way to go.

但是如果我有一个Order对象呢?这有主标题信息,客户,供应商,所需日期等。然后它有OrderItems,它是List ,是要订购的项目。我希望能够在我的UI上使用此业务对象。所以我有一些文本框连接到位置,供应商,所需日期等,并且网格连接到OrderItems。由于OrderItems是一个List,我可以轻松添加和删除记录。但是我该如何跟踪这个,特别是删除的项目。我不希望删除的项目在我的网格中可见,如果我使用foreach,我不应该迭代它们,因为它们已被删除。但是我仍然需要跟踪删除的事实。如何跟踪更改。我想我需要用一个工作单位?但随后代码似乎变得相当复杂。那么我想知道为什么不简单地使用DataTables并免费获得更改跟踪?但后来我读到了业务对象是如何实现的。

I’ve found various examples on simple Person examples, bnut not header-detail examples like Orders.

我在简单的Person示例中找到了各种示例,bnut不是像Orders这样的标题详细示例。

BTW using C# 3.5 for this.

BTW使用C#3.5。

3 个解决方案

#1


6  

Firstly, you can use an existing framework that addresses these issues, like CSLA.NET. The author of this framework has tackled these very issues. Go to http://www.rockfordlhotka.net/cslanet/ for this. Even if you don't use the full framework, the concepts are still applicable.

首先,您可以使用解决这些问题的现有框架,例如CSLA.NET。该框架的作者已经解决了这些问题。请访问http://www.rockfordlhotka.net/cslanet/。即使您不使用完整框架,这些概念仍然适用。

If you wanted to roll your own, what I've done in the past was to instead of using List for my collections, I've used a custom type derived from BindingList. Inhereting from BindingList allows you to override the behaviour of add/remove item. So you can for example have another internal collection of "delteted" items. Every time the overriden Remove method is called on your collection, put the item into the "deleted" collection, and then call the base implementation of the Remove method. You can do the same for added items or changed items.

如果你想自己动手,我过去所做的就是不使用List作为我的集合,我使用了从BindingList派生的自定义类型。通过BindingList,您可以覆盖添加/删除项的行为。因此,您可以拥有另一个“delteted”项目的内部集合。每次在集合上调用overriden Remove方法时,将该项放入“deleted”集合中,然后调用Remove方法的基本实现。您可以对添加的项目或更改的项目执行相同的操作。

#2


2  

You're spot on about needing a unit of work, but don't write one. Use NHibernate or some other ORM. That is what they're made for. They have Unit of Works built in.

你需要一个单位的工作,但不要写一个。使用NHibernate或其他一些ORM。这就是他们的目标。他们有内置的工作单位。

Business objects are indeed "the way to go" for most applications. You're diving into a deep area and there will be much learning to do. Look into DDD.

对于大多数应用程序来说,业务对象确实是“可行的方法”。你潜入了一个很深的区域,会有很多学习要做。看看DDD。

I'd also strongly advise against code like that in your code-behind. Look into the MVP pattern.

我也强烈建议您在代码隐藏中使用类似的代码。查看MVP模式。

I'd also (while I was bothering to learn lots of new, highly critical things) look into SOLID.

我也(在我忙着学习许多新的,非常关键的东西的时候)看看SOLID。

You may want to check out JP Boodhoo's nothing but .net course as it covers a lot of these things.

你可能想看看JP Boodhoo,除了.net课程,因为它涵盖了很多这些东西。

#3


-1  

The data objects don't track changes. The change tracking occurs on the DataContext and objects that you've retrieved through the DataContext. So in order to track changes you need to do the following:

数据对象不跟踪更改。更改跟踪发生在DataContext和您通过DataContext检索的对象上。因此,为了跟踪更改,您需要执行以下操作:

public class FooDataContext : DataContext
{
   public Table<Order> Orders;   
}

public class Order
{
    [DbColumn(Identity = true)]
    [Column(DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id { get; set; }

    [DbColumn(Default = "(getutcdate())")]
    [Column(DbType = "DateTime", CanBeNull = false, IsDbGenerated = true)]
    public DateTime DateCreated { get; set; }

    [Column(DbType = "varchar(50)", CanBeNull = false, IsDbGenerated = false)]
    public string Name { get; set; }
}

Now in your codebehind you can do something like:

现在在您的代码隐藏中,您可以执行以下操作:

public void UpdateOrder(int id, string name)
{
   FooDataContext db = new FooDataContext();
   Order order = db.Orders.Where(o=>o.Id == id).FirstOrDefault();

   if (order == null) return;

   order.Name = name;

   db.SubmitChanges();
}

I wouldn't recommend directly using the data context in the code behind, but this is a good way to get started with Linq To SQL. I would recommend putting all your database interactions in an external project and call from the GUI to the classes that encapsulate this behavior.

我不建议直接在后面的代码中使用数据上下文,但这是开始使用Linq To SQL的好方法。我建议将所有数据库交互放在外部项目中,并从GUI调用封装此行为的类。

I would recommend creating a Linq To Sql (dbml) file if you're new to Linq To Sql.

如果您是Linq To Sql的新手,我建议您创建一个Linq To Sql(dbml)文件。

Right click on your project in solution explorer, and select Add New Item. Select Linq To SQL file, and it will then let you connect to your database and select the tables.

在解决方案资源管理器中右键单击您的项目,然后选择“添加新项”。选择Linq To SQL文件,然后它将允许您连接到数据库并选择表。

You can then look at the generated code, and get some great ideas on how Linq To Sql works and what you can do with it.

然后,您可以查看生成的代码,并获得有关Linq To Sql如何工作以及如何使用它的一些好主意。

Use that as a guideline on working with Linq to SQL and that will take you far...

使用它作为使用Linq to SQL的指南,这将带你走远...

#1


6  

Firstly, you can use an existing framework that addresses these issues, like CSLA.NET. The author of this framework has tackled these very issues. Go to http://www.rockfordlhotka.net/cslanet/ for this. Even if you don't use the full framework, the concepts are still applicable.

首先,您可以使用解决这些问题的现有框架,例如CSLA.NET。该框架的作者已经解决了这些问题。请访问http://www.rockfordlhotka.net/cslanet/。即使您不使用完整框架,这些概念仍然适用。

If you wanted to roll your own, what I've done in the past was to instead of using List for my collections, I've used a custom type derived from BindingList. Inhereting from BindingList allows you to override the behaviour of add/remove item. So you can for example have another internal collection of "delteted" items. Every time the overriden Remove method is called on your collection, put the item into the "deleted" collection, and then call the base implementation of the Remove method. You can do the same for added items or changed items.

如果你想自己动手,我过去所做的就是不使用List作为我的集合,我使用了从BindingList派生的自定义类型。通过BindingList,您可以覆盖添加/删除项的行为。因此,您可以拥有另一个“delteted”项目的内部集合。每次在集合上调用overriden Remove方法时,将该项放入“deleted”集合中,然后调用Remove方法的基本实现。您可以对添加的项目或更改的项目执行相同的操作。

#2


2  

You're spot on about needing a unit of work, but don't write one. Use NHibernate or some other ORM. That is what they're made for. They have Unit of Works built in.

你需要一个单位的工作,但不要写一个。使用NHibernate或其他一些ORM。这就是他们的目标。他们有内置的工作单位。

Business objects are indeed "the way to go" for most applications. You're diving into a deep area and there will be much learning to do. Look into DDD.

对于大多数应用程序来说,业务对象确实是“可行的方法”。你潜入了一个很深的区域,会有很多学习要做。看看DDD。

I'd also strongly advise against code like that in your code-behind. Look into the MVP pattern.

我也强烈建议您在代码隐藏中使用类似的代码。查看MVP模式。

I'd also (while I was bothering to learn lots of new, highly critical things) look into SOLID.

我也(在我忙着学习许多新的,非常关键的东西的时候)看看SOLID。

You may want to check out JP Boodhoo's nothing but .net course as it covers a lot of these things.

你可能想看看JP Boodhoo,除了.net课程,因为它涵盖了很多这些东西。

#3


-1  

The data objects don't track changes. The change tracking occurs on the DataContext and objects that you've retrieved through the DataContext. So in order to track changes you need to do the following:

数据对象不跟踪更改。更改跟踪发生在DataContext和您通过DataContext检索的对象上。因此,为了跟踪更改,您需要执行以下操作:

public class FooDataContext : DataContext
{
   public Table<Order> Orders;   
}

public class Order
{
    [DbColumn(Identity = true)]
    [Column(DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
    public int Id { get; set; }

    [DbColumn(Default = "(getutcdate())")]
    [Column(DbType = "DateTime", CanBeNull = false, IsDbGenerated = true)]
    public DateTime DateCreated { get; set; }

    [Column(DbType = "varchar(50)", CanBeNull = false, IsDbGenerated = false)]
    public string Name { get; set; }
}

Now in your codebehind you can do something like:

现在在您的代码隐藏中,您可以执行以下操作:

public void UpdateOrder(int id, string name)
{
   FooDataContext db = new FooDataContext();
   Order order = db.Orders.Where(o=>o.Id == id).FirstOrDefault();

   if (order == null) return;

   order.Name = name;

   db.SubmitChanges();
}

I wouldn't recommend directly using the data context in the code behind, but this is a good way to get started with Linq To SQL. I would recommend putting all your database interactions in an external project and call from the GUI to the classes that encapsulate this behavior.

我不建议直接在后面的代码中使用数据上下文,但这是开始使用Linq To SQL的好方法。我建议将所有数据库交互放在外部项目中,并从GUI调用封装此行为的类。

I would recommend creating a Linq To Sql (dbml) file if you're new to Linq To Sql.

如果您是Linq To Sql的新手,我建议您创建一个Linq To Sql(dbml)文件。

Right click on your project in solution explorer, and select Add New Item. Select Linq To SQL file, and it will then let you connect to your database and select the tables.

在解决方案资源管理器中右键单击您的项目,然后选择“添加新项”。选择Linq To SQL文件,然后它将允许您连接到数据库并选择表。

You can then look at the generated code, and get some great ideas on how Linq To Sql works and what you can do with it.

然后,您可以查看生成的代码,并获得有关Linq To Sql如何工作以及如何使用它的一些好主意。

Use that as a guideline on working with Linq to SQL and that will take you far...

使用它作为使用Linq to SQL的指南,这将带你走远...