Recently, due to busy work, blog updates are slow and I am sorry to my friends in the garden. So I wrote a comprehensive application of the Spring. NET Architecture over the weekend. I hope this practical architecture will be welcomed by everyone.
I. Summary
The development tool used in this Demo is VS2010, the database is arbitrary, and the Silvelright version is 4.0.
Demo is divided into three layers (Tier), database, server, and client. The server is divided into three layers: persistent Layer, service Layer, and facade Layer.
WCF uses Windows Services as the host, and clients use Silverlight to run browsers.
Figure 1
Figure 1 shows the items included in the solution. The Server folder contains the Server-side code, the Host folder contains the Server-side Windows service Host, and the Client folder contains the Silverlight application.
Ii. Technical Points
The Binding configuration of WCF uses NetTcpBinding to implement "Callback ".
On the server side, add, modify, and call the client callback When deleting data:
Callback
Private void ReceiveProduct (Product entity)
{
Try
{
Parallel. ForEach (callBackList, (item) =>
{
Try
{
Item. ReceiveProduct (entity );
}
Catch (Exception ex)
{
System. Console. WriteLine (ex. ToString ());
}
});
}
Catch (Exception ex)
{
System. Console. WriteLine (ex. ToString ());
}
}
Public void DeleteProduct (Guid id)
{
Var entity = this. ProductManager. Get (id );
If (entity = null)
{
Return;
}
This. ProductManager. Delete (entity );
Try
{
Parallel. ForEach (callBackList, (item) =>
{
Try
{
Item. ClearProduct (id );
}
Catch (Exception ex)
{
System. Console. WriteLine (ex. ToString ());
}
});
}
Catch (Exception ex)
{
System. Console. WriteLine (ex. ToString ());
}
}
The client processes the request when receiving the callback:
Client
Public void proxy_receiveproductinclued (object sender, ReceiveProductReceivedEventArgs e)
{
Var list = this. grid. ItemsSource as ObservableCollection <Product>;
If (list = null)
{
Return;
}
Var entity = list. FirstOrDefault (f => f. ID = e. entity. ID );
If (entity = null)
{
List. Add (e. entity );
}
Else
{
Entity. Code = e. entity. Code;
Entity. Name = e. entity. Name;
Entity. BuyPrice = e. entity. BuyPrice;
Entity. SellPrice = e. entity. SellPrice;
Entity. QuantityPerUnit = e. entity. QuantityPerUnit;
Entity. Remark = e. entity. Remark;
Entity. Unit = e. entity. Unit;
}
}
Public void proxy_clearproductinclued (object sender, ClearProductReceivedEventArgs e)
{
Var list = this. grid. ItemsSource as ObservableCollection <Product>;
If (list = null)
{
Return;
}
Var entity = list. FirstOrDefault (f => f. ID = e. id );
If (entity = null)
{
Return;
}
List. Remove (entity );
}
In this way, the data of multiple clients is consistent.
Currently, Spring. NET's OpenSessionInView is only applicable to the Web. Therefore, I use AOP to intercept the Contract interface implementation class of WCF. Enable SessionScope before calling and disable SessionScope after calling. In this way, the Session synchronization of NHibernate is realized in the same request.
OpenSessionInViewModule
Public class OpenSessionInViewModule: IMethodInterceptor
{
Private static log4net. ILog logger = log4net. LogManager. GetLogger (typeof (OpenSessionInViewModule ));
Public object Invoke (IMethodInvocation invocation)
{
SessionScope sessionScope = new SessionScope ("SessionScope", typeof (SessionScope), false );
Try
{
SessionScope. Open ();
Object obj = invocation. Proceed ();
Return obj;
}
Catch (Exception ex)
{
System. Console. WriteLine (ex. ToString ());
Logger. Error (ex );
Return null;
}
Finally
{
SessionScope. Close ();
}
}
}
Deploy Windows Services.
The Host folder contains two batch processing (. bat) files: Install. bat and UnInstall. bat. These are the commands for installing and uninstalling Windows Services respectively. 2.1.
Fig 2.1
Cross-Domain file clientaccesspolicy. xml must be placed on port 80 of the http server for cross-region Silverlight 4, as shown in figure 2.2.
Fig 2.2
The http server can be not IIS, but the clientaccesspolicy. xml page must be accessible when you enter the domain name + clientaccesspolicy. xml.
Clientaccesspolicy. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Access-policy>
<Cross-domain-access>
<Policy>
<Allow-from http-request-headers = "*">
<Domain uri = "*"/>
</Allow-from>
<Grant-to>
<Socket-resource port = "4502-4534" protocol = "tcp"/>
<Resource path = "/" include-subpaths = "true"/>
</Grant-to>
</Policy>
</Cross-domain-access>
</Access-policy>
Iii. Running Effect
Fig 3.1
Figure 3.1 shows the database after the automatic nhib.pdf table.
Fig 3.2
Figure 3.2 shows the running effect of the Silverlight application.
The effect of Running multiple clients is shown in Figure 3.3.
Fig 3.3
Click Add or modify to bring up the modify page (3.4 ). Click "OK" to maintain the data. Because the traffic is bidirectional, the data of the two clients is consistent (3.5 ).
Fig 3.4
Fig 3.5
Iv. Summary
This Demo is a comprehensive application of Spring. NET and NHibernate frameworks and is applicable to enterprise applications in small projects. This architecture is highly practical and achieves "Quick Development" in project development. I hope that my friends who are interested in research can download my code and discuss it with me.
Code download
Source: http://www.cnblogs.com/GoodHelper/archive/2010/10/16/SpringNetFramework_Step2.html
You are welcome to reprint it, but you must retain the copyright.