Pregunta

I am designing a DAL.dll for a web application. The Scenario is that on the web user gets the entity and modifies some fields and click save. My problem is that how to make sure only the modifield field to be saved. For Example, an entity:

public class POCO{
    public int POCOID {get;set;}
    public string StringField {get;set;}
    public int IntField {get;set;}
}

and my update interface

//rows affected
int update (POCO entity);

When only the IntField is modified, because StringField is null, so I can ignore it. However, when only the StringField is modifield, because IntField is 0 - default(int), I cannot determine if it should be ignored or not.

Some limitations: 1. stateless, no session. so cannot use "get and update", context, etc. 2. to be consistent to data model, cannot use nullable "int?"

¿Fue útil?

Solución 2

I don't really understand how you want to work stateless, but update only changed properties. It will never work when stateless, since you will need a before-after comparison, or anything else to track changes (like events on property setters). Special "virgin" values are not a good solution, since I think your user wants to see the actual IntField value.

Also make your database consistent with your application data - if you have standard, not-nullable int values, make the DB column int not null default 0! It is really a pain to have a database value which can't be represented by the program, so that the software "magically" turns DB null into 0. If you have a not-nullable int in your application, you can't distinguish between DB null and zero, or you have to add a property like bool IsIntFieldNull (no good!).

To reference a common Object-relational mapper, NHibernate: it has an option called "dynamic-update" where only changed properties/columns are updated. This requires, however, a before-after check and stateful sessions, and there's debate on whether it helps performance, since sending the same DB query every time (with different parameter values) is better than sending multiple different queries - opposed to unneccessary updates and network load. By default, NHibernate updates the whole row, after checking if any change has been done. If you only have ID, StringField and IntField, dynamic-update instead of full-row update might in fact be a good solution.

Mapping nullable DB columns to not-nullable application data types, such as int, is a common mistake when implementing NHibernate, since it creates self-changing DAL objects.

However, when working with ORM or writing your own DAL, make sure you have proper database knowledge!

Otros consejos

Just a tip, if negtive number is not allow in your business requirement, you can use -1 to indicate this value does not apply.

Options


Many ORMs (Object-relational mapping) provide this type of functionality. You define your object to work with say "Entity Framework" or "NHibernate". These ORM's take care of reading and writing to the database. Internally, they have their own mechanisms to keep track of what has been modified.


Look into Delta<\T> (right now it's an ODATA thing, so it may not be useful to use, but you can learn from it)


Make your own. Have some type of base class that all your other objects inherit from, and somehow when you set fields it records those somewhere else.


I highly recommend not relying on null or magic numbers (-1) to keep track of this. You will create a nightmare for yourself.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top