IDictionary is the base interface of the generic set of key/value pairs. Each element has a key-value pair in the keyValuepair object.
Each pair must have a unique key. Whether to allow null reference of key (Nothing in Visual Basic) is different. This value can be a null reference (Nothing in Visual Basic) without being unique.IDictionaryThe interface allows enumeration of key and value, but this does not mean any specific sorting order.
C #ForeachStatement (in Visual BasicFor EachIn C ++For each) The type of each element in the collection. BecauseIDictionaryEach element of is a key/value pair, so the element type is neither the key type nor the value type. InsteadKeyValuePairType.
The following is a simple example program.
Entity class:
Namespace Domain
{
// Order
Public class Order
{
Public int OrderID {get; set ;}
/// <Summary>
/// Order Date
/// </Summary>
Public DateTime OrderDate {get; set ;}
/// <Summary>
/// Order address
/// </Summary>
Public string OrderAdress {get; set ;}
/// <Summary>
/// Order phone number
/// </Summary>
Public string OrderTel {get; set ;}
}
}
// Order Details
Namespace Domain
{
Public class OrderDetail
{
Public int DetailID {get; set ;}
Public int ProductID {get; set ;}
/// <Summary>
/// Quantity
/// </Summary>
Public decimal Quantity {get; set ;}
/// <Summary>
/// Unit price
/// </Summary>
Public decimal Price {get; set ;}
}
}
// Store data
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using Domain;
Namespace Service
{
Public static class OrderStock
{
Private static IList <Order> m_orderList = new List <Order> (); // defines the generic field of a List.
Public static IList <Order> OrderList // defines the generic attribute of a List.
{
Get
{
Return m_orderList;
}
Set
{
M_orderList = value;
}
}
Private static IDictionary <Order, IList <OrderDetail> m_orders = new Dictionary <Order, IList <OrderDetail> ();
// Defines a generic field of Dictionary,
Public static IDictionary <Order, IList <OrderDetail> Orders/defines a generic attribute of a Dictionary.
{
Get
{
Return m_orders;
}
Set
{
M_orders = value;
}
}
}
}
Service Interface
Using System;
Using Domain;
Namespace Service
{
Public interface IOrderService
{
// Delete
Void Delete (Domain. Order entity );
// Query all
System. Collections. Generic. IList <Domain. Order> LoadAll ();
// Save
Object Save (Domain. Order entity );
// Update
Void Update (Domain. Order entity );
// Query by ID
Order Get (object id );
// Obtain the next ID
Object GetNextID ();
}
Namespace Service
{
Public interface IOrderDetailService
{
Void Delete (System. Collections. Generic. KeyValuePair <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> entity );
System. Collections. Generic. KeyValuePair <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> Get (object id );
Object GetNextDetailID (object mainID );
Object GetNextID ();
System. Collections. Generic. IDictionary <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> LoadAll ();
Object Save (Domain. Order entity, System. Collections. Generic. IList <Domain. OrderDetail> detail );
Void Update (System. Collections. Generic. KeyValuePair <Domain. Order, System. Collections. Generic. IList <Domain. OrderDetail> entity );
}
}
Service implementation
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using Domain;
Namespace Service