⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 order.cs

📁 微软的行业应用解决方案示例
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace HardwareDistributor.Business
{
    public class Order : DataObject
    {
        #region Constants
        public const string CUSTOMERID_COLUMN = "CustomerId";
        public const string DELIVERYDATE_COLUMN = "DeliveryDate";
        public const string ORDERID_COLUMN = "OrderId";
        public const string ORDERSTATE_COLUMN = "OrderState";
        public const string SIGNATURE_COLUMN = "Signature";
        public const string DISPLAYID_COLUMN = "DisplayId";
        #endregion


        #region Fields
        private Customer _customer;
        private Nullable<int> _customerId = null;
        private Nullable<int> _displayId = null;
        private Nullable<DateTime> _deliveryDate = null;
        private List<OrderDetail> _details;
        private Nullable<Guid> _orderId = null;
        private Nullable<OrderState> _orderState = null;
        private byte[] _signature = null;
        #endregion


        #region Constructor(s) & Dispose
        /// <summary>
        /// Create a new Order
        /// </summary>
        public Order() : base()
        {
        }


        /// <summary>
        /// Create an unmodified Order
        /// </summary>
        /// <param name="orderId">Id of the Order</param>
        /// <param name="customerId">Customer Id of the Order</param>
        /// <param name="deliveryDate">Delivery date of the Order</param>
        /// <param name="orderState">State of the Order</param>
        public Order(Nullable<Guid> orderId, 
                     Nullable<int> customerId, 
                     Nullable<DateTime> deliveryDate, 
                     Nullable<OrderState> orderState,
                     byte[] signature,
                     Nullable<int> displayId)
        {
            _orderId = orderId;
            _customerId = customerId;
            _deliveryDate = deliveryDate;
            _orderState = orderState;
            _objectState= ObjectState.Unchanged;
            _signature = signature;
            _displayId = displayId;
        }
        #endregion


        #region Properties
        /// <summary>
        /// Customer that placed the order
        /// </summary>
        public Customer Customer
        {
            get
            {
                // Used to only load the Customer object when it is first used
                if ((_customer == null) && (_customerId != null))
                {
                    SetCustomer();
                }
                return _customer;
            }
            set
            {
                // set actually changes CustomerId
                if (value != null)
                {
                    CustomerId = value.CustomerId;
                }
                else
                {
                    CustomerId = null;
                }
            }
        }

        /// <summary>
        /// Displayable Id
        /// </summary>
        public Nullable<int> DisplayId
        {
            get
            {
                return _displayId;
            }
            set
            {
                // If needed put additional editing before the change value.
                _displayId = ChangeValue<Nullable<int>>(_displayId, value,
                                                         DISPLAYID_COLUMN);
            }
        }


        /// <summary>
        /// Unique Customer Id
        /// </summary>
        public Nullable<int> CustomerId
        {
            get
            {
                return _customerId;
            }
            set
            {
                // If needed put additional editing before the change value.
                _customerId = ChangeValue<Nullable<int>>(_customerId, value,
                                                         CUSTOMERID_COLUMN);
            }
        }


        /// <summary>
        /// Date the order will be delivered
        /// </summary>
        public Nullable<DateTime> DeliveryDate
        {
            get
            {
                return _deliveryDate;
            }
            set
            {
                // Ensure delivery date is in the future.
                if (value.Value.Date <= DateTime.Now.Date)
                {
                    throw new ApplicationException("Delievry date must be in the future.");
                }

                _deliveryDate = ChangeValue<Nullable<DateTime>>(_deliveryDate, 
                                                                value,
                                                                DELIVERYDATE_COLUMN);
            }
        }


        /// <summary>
        /// The associated order detail
        /// </summary>
        public List<OrderDetail> OrderDetails
        {
            get
            {
                if (_details == null)
                {
                    // retrieve line items\
                    if (OrderId != null)
                    {
                        _details = Services.GetOrderDetails(OrderId.Value);
                    }
                    else
                    {
                        // return empty list since order is new
                        _details = new List<OrderDetail>();
                    }
                }

                return _details;
            }
        }


        /// <summary>
        /// Unique Order Id
        /// </summary>
        public Nullable<Guid> OrderId
        {
            get
            {
                return _orderId;
            }
            set
            {
                bool changable = _orderId == null;
                _orderId = ChangeKey<Nullable<Guid>>(_orderId, value,
                                                    ORDERID_COLUMN);

                if (changable && (_orderId != null))
                {
                    foreach (OrderDetail currentDetail in OrderDetails)
                    {
                        currentDetail.OrderId = _orderId;
                    }
                }
            }
        }


        /// <summary>
        /// Current state of the order as it
        /// moves through the production process
        /// </summary>
        public Nullable<OrderState> OrderState
        {
            get
            {
                return _orderState;
            }
            set
            {
                // If needed put additional editing before the change value.
                _orderState = ChangeValue<Nullable<OrderState>>(_orderState, 
                                                                value,
                                                                ORDERSTATE_COLUMN);
            }
        }

        public byte[] Signature
        {
            get
            {
                return _signature;
            }
            set
            {
                // If needed put additional editing before the change value.
                _signature = ChangeValue<byte[]>(_signature, value, 
                                                 SIGNATURE_COLUMN);
            }
        }

        public string DisplayIdCustomer
        {
            get { return DisplayId + ": " + Customer.CustomerName; }
        }

        #endregion


        #region Methods
        /// <summary>
        /// Load a Image file into the signature.
        /// </summary>
        /// <param name="signaturePath">
        /// file and path to image holding the signature
        /// </param>
        public void LoadSignature(string signaturePath)
        {
            FileStream fs = null;
            byte[] imageData;
            
            //Open Gif file
            fs = File.Open(signaturePath, FileMode.Open);

            try
            {
                //Create byte array equal to the size of the Gif file
                imageData = new byte[fs.Length];

                //Write Gif image data to the byte array
                fs.Read(imageData, 0, imageData.Length);

                // capture signature and put in order
                this.Signature = imageData;
            }
            finally
            {
                fs.Close();
                fs.Dispose();
            }
        }


        /// <summary>
        /// Sets the customer object to point to a specific customer in the 
        /// cache of Customers.
        /// </summary>
        private void SetCustomer()
        {
            int count;

            // get customer Id - set in a local storage for quick access
            if (CustomerId != null)
            {
                var customerQuery = from currentCustomer in GlobalCache.Instance.Customers
                                    where currentCustomer.CustomerId == CustomerId
                                    select currentCustomer;
                count = customerQuery.Count();
                if (count == 0)
                {
                    throw new ApplicationException("Could not find customer for CustomerId, " +
                                                   CustomerId.ToString() + ".");
                }
                else
                {
                    if (count > 1)
                    {
                        throw new ApplicationException("CustomerId, " +
                                                       CustomerId.ToString() + ", not unique.");
                    }
                }
                _customer = customerQuery.ElementAt(0);
            }
            else
            {
                _customer = null;
            }
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -