NetTiers学习总结3--实体层 下载本文

第三次 实体层 Entity Layer

You can think of an Entity as the Reflection of a Database-table exposed in Object Oriented fashion. The Word Entity is the same which is commonly used and mostly for a Database-Table. So in the .Net environment an object reflecting a Database-table cascadingly, Exposing its Columns as class-Properties, Carefully matching the Data Types between a

database-column and a .Net-Type(Let say string for varchar and Int16 for smallint).

Now think of a Database-Table 'USERS' where, definitely, each row of table is representing a single User's complete information(like her ID, Name, Age and Address) and on the other hand in .Net imagine an Object 'User' (with properties ID, Name, Age and Address). It actually is representing One User from database-table 'Users' or One Row of database-table.

.netTiers' architecture provides us an environment where we can Control the database entities by passing in these .Net entities in special methods. In fact those special methods which Control these .Net entities are called Controller Objects. We'll be studying about them in next articles. for now we should make sure that we know that we can call some dedicated methods from Controller objects to Retrieve an Entity or List of Entities. We can pass in an Entity into Controller behaviors to perform *CRUD actions.

?

CRUD::Create, Read, Update, Delete

(I've recorded a Video over Entities and Controllers, Discussing Entity, Its Providers, Entity Relations and Entity State) Download Link Basics of Entity, Provider, Relationships and EntityStates

如何定义实体

.netTiers uses the notion of the entity along with the TableModule & Data Transfer Object (DTO) Patterns in order to expose your database as entities. Meaning, for every table in your database, an entity will be generated for that table. The DTO allows you to pass the lightweight entities through the many tiers while still maintaining the loosely coupled open ended architecture of the Entity Layer, since it doesn't depend on any DataProvider.

.netTiers will also attempt to discover all of the relationships that your table has with other tables in the database and will create child

properties of those relationships. This will build out your entire entity domain. Currently the relationship types supported are one to one, one

to many, and many to many relationships. These relationships make it easy for you to intuitively work with your entities and now have a logical object graph. There are several ways to create a certain relationship, but we'll discuss the rules in the Database Model section.

Example: Customer Entity

///An example object graph of a customer entity looks like this. /// Customer Parent /// Order 1:1

/// OrderDetails //1:M

/// ProductCollection //1:M /// CustomerDemographics //1:M

/// CustomerDemographicsCollection_From_CustomerCustomerDemo //M:M

EntityBase

The entities all inherit from two parent classes, the user class called EntityBase.cs which inherits from EntityBaseCore.generated.cs, the generated class. As mentioned earlier, every .generated class will be generated over and over again, do not modify these classes as your work will be lost when you regenerate your code.

These base classes implement the base behavior across all entities. The EntityBase class provides you with exclusive access to modify the behavior across all entities. You can override our default implementation and these changes will not get overwritten. This is a way for you to make changes and extend the behavior while at the same time, safeguarding your work.

EntityBase The EntityBase classes provide behavior to manage state using the EntityState property.

What is Entity State?

Entity State provides a way to track the current status of an entity in it's entity lifecycle, which differs from the CLR object lifecycle. There are 4 main EntityStates, found in the EntityState enumeration, Unchanged, Added, Changed, and Deleted. You do not have to manually keep track of state, when you modify a property, or create a new entity, or read an entity from the database .netTiers will automatically change the state and keep track for you.

1 ///

2 /// List of possible state for an entity. 3 /// 4 public enum EntityState 5 6 { /// 7 /// Entity is read in from the database, unchanged 8 /// 9 Unchanged=0, 10 11 /// 12 /// Entity is new and not yet in the database 13 /// 14 Added=1, 15 16 /// 17 /// Entity has been modified 18 /// 19 Changed=2, 20 21 /// 22 /// Entity has been deleted 23 /// 24 Deleted=3 25 }

Entity LifeCycle

Assuming you have no data in your database, the very first thing you will do is add data to the database. In order to do this, you will have to create a new entity. Let's use the Customer entity that we've generated from the Northwind database, and create a new Customers entity and walk through the different states of the entity, which as mentioned earlier is different than the object lifecycle.

1 ///STAGE 1: Added 2 ///Create a new entity, whose EntityState is EntityState.Added 3 Customers customer = new Customers(); 4 customer.Address = \; 5 customer.City = \; 6 customer.Region = \; 7 customer.Phone = \; 8 Response.Write(customer.EntityState); // EntityState.Added; 9 10 ///Persist 11 DataRepository.CustomersProvider.Save(customer); 12 13 14 ///STAGE 2: Unchanged 15 /// The EntityState has been set to EntityState.Unchanged 16 ///Once we persist the entity, it will refresh the entity from the database 17 ///If there is an identity column in your table that the entity represents 18 /// then the new identity of the inserted value is returned as well. 19 Response.Write(customer.CustomerID); 20 Response.Write(customer.EntityState); // EntityState.Unchanged; 21 22 23 ///STAGE 3: Changed 24 /// By modifying a property the entity will automatically 25 /// change state to an EntityState.Changed state. 26 customer.Region = \; 27 Response.Write(customer.EntityState); // EntityState.Changed; 28 DataRepository.CustomersProvider.Save(customer); 29 30 31 ///STAGE 4: Deleted