Developing an Application Architecture Part 1: Giving Your Data Some Brains
来源:互联网 发布:volatile java用法 编辑:程序博客网 时间:2024/06/08 08:26
Developing an Application Architecture
Overview
This series of articles will walk through the creation of a simple application architecture that can be utilized in programs of nearly any size. The code posted in these articles is for illustrative purposes of concepts and is not intended for direct production use.
Part 1: Giving Your Data Some Brains
(continued)
Every application is made up of a set of objects, and most of these objects will expose a set of properties that will be manipulated. The use of properties rather than directly exposing data members provides two benefits. First, there is the ability to implement implicit operations within the getter and setters. Second, it provides a common basis for tools such are reflection even when there are no implicit operations. Start with a simple set of classes that represent an invoice.
public class Invoice { Invoice() { Items = new List<InvoiceLineItem>(); } public Guid InvoiceID { get; set; } public string InvoiceNumber { get; set; } public string InvoiceDate { get; set; } public ICollection<InvoiceLineItem> Items { get; private set; } public decimal ItemsTotalPrice { get; set; } public decimal ItemsTotalTax { <get; set; } public decimal ItemsTotalWeight { <get; set; } public decimal Shipping { get; set; } public decimal ShippingRate { get; set; } public decimal TaxRate { get; set; } public decimal InvoiceTotal { get; set; } } public class InvoiceLineItem { public Guid ItemID { get; set; } public Guid InvoiceID { get; set; } public int LineNumber { get; set; } public decimal Quantity { get; set; } public bool Taxable { get; set; } public decimal UnitPrice { get; set; } public decimal Weight { get; set; } public decimal ExtendedPrice { get; set; } }
By using the .NET 2.0 capability of automatic properties, you have eliminated the need to manually implement the mechanics of the get and set operations. However, this savings comes at the cost of not being able to place any logic in them. As soon as you want to put in an operation on either the get or the set, you have to revert back to:
private decimal m_ExtendedPrice;public decimal ExtendedPrice{ get { return m_ExtendedPrice; } set { m_ExtendedPrice = value; }}
You are now up to 6 lines (even with the compressed formatting) for each property. This bloats out source code up to well over 110 lines for just the properties, instead of the current 19. To reduce this, you can write a simple class that encapsulates the syntax of a property.
public class Field<DATA_TYPE> { private DATA_TYPE m_Value; public DATA_TYPE Value { get { return m_Value; } set { m_Value = value; } } }
For (temporary) simplicity, you will re-implement your invoicing classes using this helper with [read only ] fields rather than properties.
public class Invoice{ Invoice() { Items = new List<InvoiceLineItem>(); } public readonly Field<Guid> InvoiceID = new Field<Guid>(); public readonly Field<string> InvoiceNumber = new Field<string>(); public readonly Field<string> InvoiceDate = new Field<string>(); public ICollection<InvoiceLineItem> Items { get; private set; } public readonly Field<decimal> ItemsTotalPrice = new Field<decimal>(); public readonly Field<decimal> ItemsTotalTax = new Field<decimal>(); public readonly Field<decimal> ItemsTotalWeight = new Field<decimal>(); public readonly Field<decimal> Shipping = new Field<decimal>(); public readonly Field<decimal> ShippingRate = new Field<decimal>(); public readonly Field<decimal> TaxRate = new Field<decimal>(); public readonly Field<decimal> InvoiceTotal = new Field<decimal>();}public class InvoiceLineItem{ public readonly Field<Guid> ItemID = new Field<Guid>(); public readonly Field<Guid> InvoiceID = new Field<Guid>(); public readonly Field<int> LineNumber = new Field<int>(); public readonly Field<decimal> Quantity = new Field<decimal>(); public readonly Field<bool> Taxable = new Field<bool>(); public readonly Field<decimal> UnitPrice = new Field<decimal>(); public readonly Field<decimal> Weight = new Field<decimal>(); public readonly Field<decimal> ExtendedPrice = new Field<decimal>();}
This will require that you specifically use the Value property of a field when setting or retrieving information. At first glance, it looks like you have made the class more complicated to declare, and also more complicated to use. But, as you start to expand the capabilities of your Field class, you will begin to see significant benefits. First, add the ability to validate and assignment to a Field instance. For this example, you will create the base class, and then a specific validator to ensure that a decimal quantity is within a range.
public class Validator<DATA_TYPE> { public virtual void Validate(DATA_TYPE value) { return; } } public class RangeValidator : Validator<decimal> { public RangeValidator(decimal min, decimal max) { m_Min = min; m_Max = max; } public override void Validate(decimal value) { if ((value < m_Min) || (value > m_Max)) throw new ArgumentException(); } private readonly decimal m_Min; private readonly decimal m_Max;}
About the Author
Sr Software Architect with 30 years experience developing high-performance / high-reliability software systems.
- Developing an Application Architecture Part 1: Giving Your Data Some Brains
- Developing Database Applications with an O-R Mapping Framework Part 1
- Developing Workflows in VS: Part 5 - Code Your Workflow
- Developing Workflows in VS: Part 3 - Five Steps for Developing Your Workflow
- Enterprise Library: Data Access Application Block使用向导,Part 1
- Some Tips of Gift Giving
- 【学习ARToolkit小记之七】 开发第一个ARToolkit程序(Developing your First Application)(续1)
- Developing Workflows in VS: Part 2 - Planning Your Workflow: Two Things to Keep in Mind
- Developing Workflows in VS: Part 4 - Design and Bind Your Forms
- Developing Workflows in VS: Part 6 - Deploy and Debug your workflow
- Creating plug-ins for virtual application patterns, Part 1: An introduction
- 【学习ARToolkit小记之六】 开发第一个ARToolkit程序(Developing your First Application)
- Developing a Web Application
- Securing Your PeopleSoft Application Environment: An Oracle Red Paper
- An application for Apple Watch to control your Tesla Car
- Is Your Drinking Water Giving You Diabetes?
- Giving something up for your spouse
- EU4-32: Giving your friend advice
- Solaris常用软件怎么安装?
- 一点编程感受
- jsp的乱码问题
- 命令模式(Command)
- static 修饰符
- Developing an Application Architecture Part 1: Giving Your Data Some Brains
- 如何在网页中播放rm视频文件
- mysql创建外键错误的解决方法
- pixbuf
- EA与Rose UML建模工具比较摘录
- /subsystem:windows 与 /subsystem:console
- Apache Commons项目简介之BeanUtils
- IT英语 E-mail 格式范例
- 全面掌握c#中的拖放操作