Table of Contents [Hide/Show]
Business Component Layer About: Business Component Layer Component Layer Roles: .netTiers Business Component Layers Things To Consider: Service Layer ExceptionHandling Configuration: Service API Example: Workflow Pipeline: Architecture Example: Processors: Logical Process Flow: Processor Example: Custom Business Rule:
1 AccountService accountsService = new AccountsService(); 2 TList<Accounts> accountList = accountsService.GetAll();
1 <exceptionHandling> 2 <exceptionPolicies> 3 <add name="NoneExceptionPolicy"> 4 <exceptionTypes> 5 <add type="System.Exception, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 6 postHandlingAction="None" name="Exception"> 7 <exceptionHandlers> 8 <add logCategory="Exceptions" eventId="100" severity="Error" 9 title="netTiers.Petshop Exception Handling" 10 formatterType="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.XmlExceptionFormatter, 11 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0, Culture=neutral, 12 PublicKeyToken=null" priority="0" 13 type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging.LoggingExceptionHandler, 14 Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging, Version=2.0.0.0, 15 Culture=neutral, 16 PublicKeyToken=null" 17 name="Logging Handler" /> 18 </exceptionHandlers> 19 20 </add> 21 </exceptionTypes> 22 23 </add> 24 </exceptionPolicies> 25 </exceptionHandling>
1 //Create a new Service Workspace object to work with; 2 AccountService accountsService = new AccountsService(); 3 4 //Create an entity to use in examples; 5 Account accountEntity = new Account(); 6 accountEntity.AccountName = "MyAccountName"; 7 accountEntity.CreatedDate = DateTime.Now; 8 9 //GetAll() 10 TList<Accounts> accountList = accountsService.GetAll(); 11 12 //Find() 13 TList<Accounts> accountList = accountsService.Find("IsActive = 1"); 14 15 //GetPaged() 16 TList<Accounts> accountList = 17 accountsService.GetPaged("IsActive = 1 AND AccountName LIKE 'smi%'"); 18 19 //GetByFk() 20 TList<Accounts> accountList = accountsService.GetByCustomerId(25); 21 22 //GetIX() 23 TList<Accounts> accountList = 24 accountsService.GetByAccountCreatedDate(new DateTime("1/1/2006")); 25 26 27 //Get() 28 Accounts account = accountsService.Get(new AccountsKey(23)); 29 30 //Insert() 31 accountsService.Insert(accountEntity); 32 Response.Write(accountEntity.AccountId); // is now populated 33 34 //Delete() 35 bool result = accountsService.Delete(accountEntity); 36 37 //Delete() 38 bool result = accountsService.Delete(23); 39 40 //Update() 41 accountEntity.AccountName = "MyAccountName 2"; 42 accountsService.Update(accountEntity); 43 44 //GetByManyToManyl() 45 TList<Customers> accountList = accountsService.GetCustomers_From_AccountsReceivable(); 46 47 //GetCustomProcedureName() 48 TList<Accounts> accountList = accountsService.GetByAccountMaturationDate(); 49 50 51 //DeepLoadByIdl() using PK 52 Account account = accountsService.DeepLoadByAccountId( 53 id, false, DeepLoadType.IncludeChildren, typeof(Customers), typeof(TList<ChartOfAccounts>)); 54 55 //DeepLoadByIdl() using FK 56 TList<Account> account = accountsService.DeepLoadByCustomerId( 57 id, false, DeepLoadType.IncludeChildren, typeof(Customers), typeof(TList<ChartOfAccounts>)); 58 59 //already instatiated objects//DeepLoad 60 accountsService.DeepLoad( 61 myAccountEntity, false,DeepLoadType.IncludeChildren, typeof(Customers), typeof(TList<ChartOfAccounts>)); 62 63 // is now filled 64 Response.Write(accountsService.CustomerIdSource.LastName); 65 66 // is now filled 67 Response.Write(accountsService.ChartOfAccountsCollection.Count); 68 69 //DeepSave 70 accountsService.DeepSave( 71 myAccountEntity, false, DeepSaveType.IncludeChildren, typeof(Customers), typeof(TList<ChartOfAccounts>)); 72
1ordersService.ProcessorList.Add(new InventoryProcessor(o)); 2ordersService.ProcessorList.Add(new VerifyEmployeeProcessor(o.EmployeeIdSource)); 3ordersService.ProcessorList.Add(new EmployeeOrderProcessor(o)); 4ordersService.ProcessorList.Add(new BillingProcessor(o)); 5ordersService.ProcessorList.Add(new ShippingProcessor(o)); 6ordersService.ProcessorList.Add(new OrderNotificationProcessor(o));
1 //Execute Processor List 2 ServiceResult result = ordersService.Execute();
1if (result.HasErrors) 2...{ 3 ShowErrors(result.Error); 4 ShowExceptions(result.Exceptions); 5}
1 ordersService.GetAll(); 2 3 ordersService.Save(o);
1public class InventoryProcessor : ProcessorBase 2 3...{ 4 private Entities.Orders order; 5 private GenericProcessorResult genericProcessorResult; 6 7 8 /**//// <summary> 9 /// Inventory Processor 10 /// </summary> 11 /// <param name="order">Order to process through Inventory</param> 12 13 public InventoryProcessor(Entities.Orders order) 14 ...{ 15 if (order == null) 16 throw new ArgumentNullException("order"); 17 18 this.order = order; 19 } 20 21 /**//// <summary> 22 /// Process the IProcessResult 23 /// </summary> 24 /// <returns></returns> 25 public override IProcessorResult Process() 26 ...{ 27 try 28 ...{ 29 /**////Add custom validation rules for this processor 30 order.AddInventoryRules(); 31 32 ProductsService products = new ProductsService(); 33 34 //check stock 35 foreach (OrderDetails item in order.OrderDetailsCollection) 36 ...{ 37 item.ProductIDSource = 38 products.Get(new ProductsKey(item.ProductID)); 39 } 40 41 order.Validate(); 42 43 if (!order.IsValid) 44 ProcessResult.AddBrokenRulesList(typeof(Entities.Orders),order.BrokenRulesList); 45 } 46 catch(Exception exc) 47 ...{ 48 if (DomainUtil.HandleException(exc, "NoneExceptionPolicy")) 49 throw; 50 } 51 52 return ProcessResult; 53 } 54 55 /**//// <summary> 56 /// ProcessResult of this current process to check Inventory on the Order 57 /// </summary> 58 public override IProcessorResult ProcessResult 59 ...{ 60 get 61 ...{ 62 if (genericProcessorResult == null) 63 ...{ 64 genericProcessorResult = new GenericProcessorResult(); 65 } 66 return genericProcessorResult; 67 } 68 } 69}
1 /**//// <summary> 2 /// Add Extra Custom Validation Rules 3 /// </summary> 4 public void AddInventoryRules() 5 ...{ 6 ValidationRules.AddRule(InventoryRuleCheck, 7 new ValidationRuleArgs("UnitsInStock")); 8 } 9 10 11 /**//// <summary> 12 /// Check Inventory 13 /// </summary> 14 /// <param name="target"></param> 15 /// <param name="e"></param> 16 /// <returns></returns> 17 public bool InventoryRuleCheck(object target, ValidationRuleArgs e) 18 ...{ 19 foreach(OrderDetails detail in OrderDetailsCollection) 20 ...{ 21 if (detail.ProductIDSource == null) 22 continue; 23 24 if (detail.ProductIDSource.UnitsInStock < detail.Quantity) 25 ...{ 26 e.Description = 27 string.Format("{0} - we do not have that much stock for this.", 28 detail.ProductIDSource.ProductName); 29 return false; 30 } 31 } 32 return true; 33 }
1using System; 2using System.Collections.Generic; 3using System.Text; 4using Northwind.Data; 5using Northwind.Entities; 6using Northwind.Entities.Validation; 7using Northwind.Services; 8using Northwind.Services.Processors.Orders; 9 10namespace NorthwindWebConsole 11...{ 12 class Program 13 ...{ 14 15 static void Main(string[] args) 16 ...{ 17 /**////Create an Orders Workspace 18 OrdersService service = new OrdersService(); 19 20 /**////Create a simulated Order 21 Orders o = new Orders(); 22 ProductsService products = new ProductsService(); 23 TList<Products> plist = products.GetAll(); 24 25 /**////Create a fake order to test our validation. 26 for(int i=0;i<10;i++) 27 ...{ 28 OrderDetails detail = new OrderDetails(); 29 detail.ProductID = plist[i].ProductID; 30 detail.ProductIDSource = plist[i]; 31 32 /**////Should trigger an invalid quantity on final loops 33 detail.Quantity = Convert.ToInt16(i * i); 34 o.OrderDetailsCollection.Add(detail); 35 } 36 37 /**////For this business process, we want to verify Inventory 38 service.ProcessorList.Add(new InventoryProcessor(o)); 39 40 /**////An object holding the results of the pipeline request. 41 ServiceResult result = service.Execute(); 42 43 if (result.HasErrors) 44 Console.WriteLine(result.Error); 45 46 Console.ReadLine(); 47 } 48 } 49}