This series of articles will start from the instance and end with the instance. This article explains how to use WCF and WF in our project. We will find that using WCF + WF will create a level that other technologies cannot reach. Finally, I will host the program on the cloud.
Microsoft. NET's 3 W (WPF, WCF, and WF) strategy is as follows. WCF is responsible for communication, WPF is responsible for interface display, and WF is responsible for processing business logic, such.
This series of articles mainly uses all the technologies, but mainly describes how to use WCF and WF to implement the middle layer of the system. A friend who has seen the bright sword knows that Li Yunlong often wins battles. He does not know a lot about the theory of war, but from practical experience. Therefore, the articles in this series take actual practice as the core. In actual practice, I will take out my own strengths and reveal my own swords.
In this series of articles, in addition to the two main technologies of WCF and WF, many other technologies will be used. The two technologies of WCF and WF will be used to the end, it also involves other technologies such as WPF, Asp.net MVC, Asp.net web form, NH, EF, and some common skills. I will conduct drills and comparisons in practice to find the most suitable technology in practice.
All right, the cowhide is exhausted and the question is coming to the fore today. This is the first article in this series. I want to start with a simple demo. This example shows how to use WCF and WF in a project. How does WCF transmit data and how does WF process business logic. Instance business scenarios are very common warehouse receiving orders: Entering warehouse receiving materials and updating the inventory quantity.
System Architecture
First, I will talk about the system architecture. It can be divided into four layers.
Level 1: Data Persistence Layer: In this example, I use EF.
Layer 2: business logic layer: Obviously, this layer is implemented using WF.
Layer 3: service layer: Obviously, this layer is implemented using WCF.
Layer 4: interface layer: Here I use Asp.net MVC. In subsequent articles, I will continue to use the MVC, webform, WPF, and SL interfaces.
The architecture diagram is as follows:
Database Structure:There are only two types of tables. Enterstock indicates the data warehouse receiving record table, and stock indicates the Data Warehouse table.
Data Persistence Layer Design:As mentioned above, I used EF4 for database access. You can check the basic usage of EF on the Internet. Here I will mainly talk about things worth explaining. Since EF is used to generate a model, how can we perform dataannotation verification in MVC. You can see the following code design. Take the database and table as an example:
public partial class StockAutoMetadata{ [DisplayName("Material ID")] [Required] public System.Guid MaterialID { get; set; } [DisplayName("Material Name")] [Required] [StringLength(50)] public string MaterialName { get; set; } [DisplayName("Quantity")] [Required] public int Quantity { get; set; }}
[Metadatatype (typeof (stockautometadata)] public partial class stock {} injects metadata attributes into the partial class.
Business logic layer design:
Using WF to process the business logic layer is the focus of this series of articles. I use the warehouse receiving operation as an example.
1. Function Design for adding materials
public sealed class InsertEnterStock : CodeActivity{ public InArgument<EnterStock> Stock { get; set; } protected override void Execute(CodeActivityContext context) { InvoicingEntities entity = new InvoicingEntities(); entity.AddToEnterStock(Stock.Get(context)); entity.SaveChanges(); }}
2. Function Design for updating inventory
public sealed class UpdateStock : CodeActivity{ public InArgument<EnterStock> EnterStock { get; set; } protected override void Execute(CodeActivityContext context) { InvoicingEntities entity = new InvoicingEntities(); var res = (from r in entity.Stock.ToList() where r.MaterialID == EnterStock.Get(context).MaterialNO select r).FirstOrDefault(); res.Quantity = res.Quantity + EnterStock.Get(context).Quantity; entity.ApplyCurrentValues(res.EntityKey.EntitySetName, res); entity.SaveChanges(); }}
3. Business function design for warehouse receiving operations
Analysis: Here I divide business logic processing into two forms.
1. function: the task is single and simple, and displayed in code.
2. Business functions: a combination of complex business and functional functions, presented in a graphical manner.
With this design, my business logic processing is very clear.
Service Layer Design:In WCF, all I have to do is start these business processes.
Contract:
[ServiceContract]public interface IInvoicingService{ [OperationContract] string EnterStock(EnterStock stock); [OperationContract] IEnumerable<Stock> GetStockList(); [OperationContract] IEnumerable<EnterStock> GetEnterStockList(); }
Implementation:
public class InvoicingService : IInvoicingService{ public string EnterStock(EnterStock stock) { var p = new Dictionary<string, object> (); p.Add("argEnterStock", stock); WorkflowInvoker.Invoke(new EnterStockBusiness(), p); return "ok"; } public IEnumerable<Stock> GetStockList() { IDictionary<string, object> outArgument = WorkflowInvoker.Invoke(new GetStockList()); return outArgument["StockList"] as List<Stock>; } public IEnumerable<EnterStock> GetEnterStockList() { IDictionary<string, object> outArgument = WorkflowInvoker.Invoke(new GetEnterStockList()); return outArgument["EnterStockList"] as List<EnterStock>; }}
In the service layer, I do not have any business logic judgment and processing, and I have completely encapsulated it into the business logic layer.
Interface Layer:On the interface layer, you can use the technology you are familiar with to implement it. Here I use Asp.net MVC. I will not elaborate on the specific implementation. Let me demonstrate this simple demo.
1. Simple Homepage
2. The inventory quantity of the two materials in stock is 0.
3. Add Materials
4. The inventory quantity is updated.
5. warehouse receiving record list
Summary:This is the first article in the WCF + WF dual-sword series, which leads you to implement a simple demo. This demo is still a prototype and has many shortcomings. I hope you can give me your valuable suggestions and help me write this series of articles. The next article will talk about how the system implements the error handling mechanism.
Code: http://files.cnblogs.com/zhuqil/Invoicing.rar