Topic content of form development
1. Form attribute settings
2. Crud andCodeGenerate
3. Advanced options
4. Design Specifications
Use the EPN framework to create the employee main file form, as shown below:
Attribute settings
Add a form, employeemaster
Set the inherited class to entryform.
[Functioncode ("saisem")]
Public partial class employeemaster: entryform
{
Public employeemaster ()
{
Initializecomponent ();
}
}
And added the function code saisem.
In the EPN category setting option, set the EPN attribute of the form as follows:
This indicates that the form supports adding, modifying, deleting, copying, importing/exporting, posting, adding attachments, and does not support approval (workflow)
Drag an entitycollection to the form and set its entityfactorytouse object
Drag several bindingsources to the form according to the data hierarchy to be displayed.
For example, to display the purchase order header and purchase details, you need to drag two bindingsources to display the purchase order header and procurement details respectively.
In this example, to display the header information and document information of the employee table, you need to drag two bindingsources
The component window looks like this.
Then, set the bindingsource data source in sequence.
Set the data source of employeebindingsource
Set the data source of employeedocumentbindingsource
Drag and Drop texteditor, numberieditor, grid, and checkbox in the form based on the fields to be displayed.
In Visual Studio 2010, it looks like this
Set the data source attributes of the form, mainbindingsource and navigatebindingsource.
If the current form needs to read all entity into the form's gridview at a time, set mainbindingsource and navigatorbindingsource of form to employeebindingsource.
If only one entity is displayed when the form is opened, set mainbindingsource of the form to employeebindingsource and add a new bindingsource component. Do not select the data source, leave it blank. Set it to navigatorbindingsource of the form.
Set form primary key attributes
The setting value is as follows:
At this point, F5, the form can be run. Enter saisem in the Quick Start column to start the form, as shown in the following figure.
Code Generation
So far, the form can be run but cannot be read/written. We also need to add new data read/write code to it.
Reference a diagram of inventory items in the development framework.
Businesslogic business logic, business entity, which is automatically generated by the Code Generator
Interface interface for reading and writing data and business logic
Manager interface implementation Interface
System Administration/inventory, interface, put in the employeemaster form
To this end, we offered codesmith, a code generation tool, to help us complete the code of the boring and error-prone application framework.
Interface file iemployeemanager, copy the generated file to the interface project
Implement the employeemanager file to copy the generated file to the Manager project.
Form file generation, copy the generated file content to the employeemaster form File
The Code is as follows:
Private iemployeemanager _ employeemanager = NULL;
Private employeeentity _ Employee = NULL;
Protected override void onshown (eventargs E)
{
Base. onshown (E );
If (! DesignMode)
{
}
}
Protected override void onload (eventargs E)
{
If (! DesignMode)
This. _ employeemanager = clientproxyfactory. createproxyinstance <iemployeemanager> ();
Base. onload (E );
}
Protected override void initnavigator (initnavigatorargs ARGs)
{
Base. initnavigator (ARGs );
Args. sortexpression. Add (employeefields. employeeno | sortoperator. ascending );
}
Protected override entitybase2 loaddata (Dictionary <string, string> refno)
{
Base. loaddata (refno );
String employeeno = string. empty;
If (refno. trygetvalue ("employeeno", out employeeno ))
{
Iprefetchpath2 prefetchpath = new prefetchpath2 (INT) entitytype. employeeentity );
Prefetchpath. Add (employeeentity. prefetchpathemployeedocument );
_ Employee = _ employeemanager. getemployee (employeeno, prefetchpath );
}
Else
{
_ Employee = new employeeentity ();
}
Return _ employee;
}
Protected override void bindcontrols (entitybase2 entity)
{
Base. bindcontrols (entity );
This. employeebindingsource. datasource = entity;
}
Protected override entitybase2 add ()
{
Base. Add ();
This. _ Employee = new employeeentity ();
Return _ employee;
}
Protected override entitybase2 save (entitybase2 entitytosave)
{
Base. Save (entitytosave );
Employeeentity _ Employee = (employeeentity) entitytosave;
This. _ Employee = This. _ employeemanager. saveemployee (_ employee );
Return _ employee;
}
Protected override void Delete (entitybase2 entitytodelete)
{
Base. Delete (entitytodelete );
Employee ID = (employee ID) entitytodelete;
This. _ employeemanager. deleteemployee (employee );
}
Protected override object clone (Dictionary <string, string> refno)
{
Base. Clone (refno );
String employeeno = string. empty;
Refno. trygetvalue ("employeeno", out employeeno );
If (string. isnullorempty (employeeno ))
{
Using (ilookupform lookup = EPN. Common. getlookupform ("employeelookup "))
{
Lookup. setcurrentvalue (currentrefno );
If (lookup. showdialog ()! = Dialogresult. OK)
Return NULL;
Employeeno = lookup. getfirstselectionvalue ();
}
}
If (! String. isnullorempty (employeeno ))
{
This. _ Employee = This. _ employeemanager. cloneemployee (employeeno );
Return this. _ employee;
}
Return NULL;
}
Protected override void releaseresources ()
{
Base. releaseresources ();
Try
{
_ Employee = NULL;
_ Employeemanager = NULL;
}
Catch
{
}
}
At this point, all content of form development has been completed.
To add more verification logic, generate some verification and put it in the verification project
For example, to verify that the ID card number must be entered, the phone number must comply with the local numbering rules, and so on, these logic verification can be written here
Advanced options
So far, almost no code has been typed, And we have completed the development of the staff main file. This speed will certainly surprise you and start to wonder:
1. Where is the business logic written?
For example, when the input employee's date of birth is 1983,ProgramThe employee's age should be calculated automatically as datetime. now-datetime (1983,10, 20), when an employee's degree is entered as the subject, it automatically corresponds to 22B in the company's organizational structure level, rather than 21B in college graduation.
As shown in the diagram from the beginning, the program is separated from the interface and logic, and the logic is directly written to the businesslogical project, that is, the employeeentity file automatically generated by the Code.
Here is a tip: the code generator will generate two employeeentity files for us, one of which is employeeentity. CS, and the other is employeeentity. logical. CS files. The previous file is an automatically generated file based on the fields in the data table. The last file is the place where the custom logic is stored, that is, the code file that needs to be put into the logic.
2. For special requirements on the program interface, the code generated above is certainly not implemented.
For example, you need to add an image display to the employee's primary file to show the picture of James Bond. This function needs to be manually written.
Secondly, we need to add bar code support to the material main file, and use the generated bar code as the basis for material encoding. This function also requires handwriting.
Another example is the figure in the opening section. The button next to employee No. is used to intelligently encode employees. Generally, the employee code is generated based on the employee's educational background, work experience, and major.
3. Problems arising from strict separation of logic and interface.
If you want to give the user a prompt based on different conditions in the logic, this will be quite troublesome. Because the logic is strictly separated from the interface, MessageBox cannot be called in the logic. show, in order to prevent this behavior from occurring, the system is directly deleted in the logical references. windows. references of forms.
Similarly, some of the code snippets in the access logic on the interface are quite troublesome. Sometimes you also need to directly access the database on the interface. For example, when performing sales and shipping, the current user enters the sales order number, determine whether the so has been delivered. If the delivery has been completed, the delivery order is not allowed. This logic is only implemented on the interface, because the shipment object has not yet been generated in businesslogical, no judgment can be made.
4. Performance
When reading data from the database to the interface, Orm and ADO. NET performance is not much different. However, when saving and updating, the orm will judge the data that has been modified and only generate the update statement of the modified data, such performance improvement is dominant.
In ADO. net, the statement for updating employees is generally
Update name = 'James bond', Country = 'England 'Where employeeno = '007'
It is rarely written like this. To update employee names, write an SQL statement
Update name = 'James bond' where employeeno = '007'
The employee's nationality is updated, so another SQL statement is written.
Update Country = 'England 'Where employeeno = '007'
The ORM framework will be happy to help us determine which data has been modified (dirty) and only generate the update of the data.
Form Specification
Codesmith has helped us to do a lot of repetitive code, which is also quite standard.
If you still need to write the code by hand and follow the common norms, the code will look more unified and standardized
Control naming rules
control type |
prefix |
example |
label |
LBL |
lblmessage |
button |
BTN |
btnsave |
groupbox |
gbx |
gbxmain |
gridview |
GRD |
grdsalesorderdetails |
Only this specification is not enough. After all, it is still a little difficult to comply with it. The EPN framework also uses word segmentation technology to automatically rename the controls in the form.
The control code in our form looks like this
Private EPN. winui. texteditor txtdeptname;
Private EPN. winui. Label lbldeptname;
Private EPN. winui. picturebox picimage;
Private EPN. winui. gridview grdsalesorder;
When an attribute is bound to a control, the control is automatically renamed. For example, when I bind textbox1 to the data source employeeno, the control is renamed to txtemployeno. by looking at the control name, you can know the data source and the control type to achieve a high degree of standardization.
Form Design Standards
1 dialogbox, dialog box, usually need to be able to respond to the ESC and enter keys, there are also OK and cancel2 buttons
2. Is the tab order of the control in the form reasonable?
3. Short cut key and quick key access. All forms must be consistent and cannot conflict with each other.
4 there are two ideas when the amount of data to be stored in the form is large.
Group by groupbox for easy viewing
When the amount of data increases, group by Tab
5. For tasks with long form execution time, make backgroundworker to ensure that the form can accept the response. After the task is executed, or use MessageBox. show Displays successfully done, or displays the execution result in the status bar like vs2010. for example, after the check in code is successfully checked in vs2010, the status bar displays the successful check in change set 54498.
At the same time, cursor cannot be used, such as writing:
This. cursor = cursors. waitcursor;
... Long operation
This. cursor = cursors. default;
6. The form is organized to avoid MDI forms. We recommend the tab-based MDI.
If necessary, place the form in the panel and set the form toplevel to false.
7. The listview control must support copy and multiple lines of copy.
8. Both enter and Tab key are used to control the next tab order.
9 winforms program. It is best to accept the startup method of command line, which can be started with the bat command. You can also add startup parameters.
Vs2005 and can both be started from command line. Do you still remember the commonly used Deven/resetpackage?
10 try to stop a computer and run two instances of the program at the same time in singletion mode.
11 target should be any CPU instead of X86. Because the Microsoft Jet Access database does not have a 64-bit version, we would rather remove the dependency and use of jet access than change target to x86. 64-bit OS has become quite popular since Windows 7, to stop working without opening your program, compile it into anycpu as much as possible.
12 RichTextBox. if the content is added by the program, auto scroll is required to the row at the row. Observe the output window during vs2010 Compilation
13 For data that cannot be modified by the user, we should not only set textbox to readonly, but also make the background color gray to clear at a glance.
Controls that must enter data must have a unified prompt, such
When the control has a value, the red image on the right is hidden. If there is no value, the image is displayed. This is the same principle as watermark in Ajax.