Interesting Enterprise Patterns

A few years ago I purchased a great book “Patterns of Enterprise Application Architecture” http://www.amazon.com/exec/obidos/tg/detail/-/0321127420/qid=1116425213/sr=8-1/ref=pd_csp_1/104-9673008-1327946?v=glance&s=books&n=507846 that I have referred to time and time again on projects. This book, written by Martin Fowler, continues of the heels of the Gang of Four book and contains many patterns that can be applied in almost any enterprise application. In this book the patterns that stand out the most on my .NET applications have been the Domain Model and Mapper patterns. 

The Domain Model pattern best represents your business data and business rules and mostly comes in two forms (1) a direct representation of the database and (2) a more complex version of an object with sub objects inside of it. In case (1) you would create a class that has properties and business rules, which can be in the property set methods or stand alone methods, that are closely related to a particular table or maybe stored procedure. You code up the class and the class represents that particular set of data. The example below represents the store table in the pubs database and how it would be implemented using the Domain Model pattern. 

            class Store 

            { 

                        private int storeId; 

                        private string name; 

                        private string address; 

                        private string city; 

                        private string state; 

                        private string zip; 

   

                        public int StoreId 

                        { 

                                    get { return storeId; } 

                                    set { storeId = value; } 

                        } 

  

                        public string Name 

                        { 

                                    get { return name; } 

                                    set { name = value; } 

                        } 

                        public string Address 

                        { 

                                    get { return address; } 

                                    set { address = value; } 

                        } 

                        public string City 

                        { 

                                    get { return city; } 

                                    set { city = value; } 

                        } 

                        public string State 

                        { 

                                    get { return state; } 

                                    set { state = value; } 

                        } 

  

                        public string Zip 

                        { 

                                    get { return zip; } 

                                    set { zip = value; } 

                        } 

  

            } 

In case (2) you might have a class that contains other classes that together represent an object graph that relates to a business entity. Continuing on the example from pubs, this would be the relationship between the store and the orders for that store. The sample below shows a newer version of the Store class modified to contain a list of Sales and a new Sales class. The List type is a generic type that holds only Sales entries. To learn more about Generics visit http://msdn2.microsoft.com/library/512aeb7t(en-us,vs.80).aspx and also look for an upcoming blog on Generics sometime in the near future. 

            class Store 

            { 

                        private int storeId; 

                        private string name; 

                        private string address; 

                        private string city; 

                        private string state; 

                        private string zip; 

                        private List = new List(); 

  

                        public int StoreId 

                        { 

                                    get { return storeId; } 

                                    set { storeId = value; } 

                        } 

  

                        public string Name 

                        { 

                                    get { return name; } 

                                    set { name = value; } 

                        } 

                        public string Address 

                        { 

                                    get { return address; } 

                                    set { address = value; } 

                        } 

                        public string City 

                        { 

                                    get { return city; } 

                                    set { city = value; } 

                        } 

                        public string State 

                        { 

                                    get { return state; } 

                                    set { state = value; } 

                        } 

  

                        public string Zip 

                        { 

                                    get { return zip; } 

                                    set { zip = value; } 

                        } 

  

                        public List StoreSales 

                        { 

                                    get { return storeSales; } 

                                    set { storeSales = value; } 

                        } 

            } 

  

            class Sales 

            { 

                        private int storeId; 

                        private int orderNumber; 

                        private DateTime orderDate; 

                        private int quantity; 

                        private string paymentTerms; 

                        private int titleId; 

  

                        public int StoreId 

                        { 

                                    get { return storeId; } 

                                    set { storeId = value; } 

                        } 

  

                        public int OrderNumber 

                        { 

                                    get { return orderNumber; } 

                                    set { orderNumber = value; } 

                        } 

  

                        public DateTime OrderDate 

                        { 

                                    get { return orderDate; } 

                                    set 

                                    { 

                                                //in this property you might put logic to limit the date to   current or future dates only 

                                                orderDate = value; 

                                    } 

                        } 

  

                        public int Quantity 

                        { 

                                    get { return quantity; } 

                                    set { quantity = value; } 

                        } 

  

                        public string PaymentTerms 

                        { 

                                    get { return paymenterms; } 

                                    set { paymentTerms = value; }00 

                        } 

  

                        public int TitleId 

                        { 

                                    get {return titleId;} 

                                    set {titleId = value;} 

                        } 

            } 

So what you can see here is that the Store class contains a list of sales that are directly related to that store. All of the logic associated with the store or the sales would be contained inside of these two classes so that when any new business rules or data changes were made you would go to one place to do them. 

In my next blog I am going to cover the Mapper pattern and how it is implemented continuing with the example from the pubs database.

Leave a Reply

Your email address will not be published. Required fields are marked *