Wednesday, October 20, 2010

As much as possible out of dependence on HttpContext



In ASP.NET, the natural enemies of unit testing is HttpContext, which is the core of ASP.NET, extremely complex, but can not be Mock1 - shows that Microsoft can write such a huge ASP.NET framework really is not so easy. Now the situation has improved a lot, so you can already use System.Web.Abstractions.dll, and this assembly provides an abstraction for the HttpContext, which is HttpContextBase abstract class. Therefore, ASP.NET MVC, the various components are dependent on HttpContextBase not HttpContext. This is a good practice, since we can get rid of as much as possible of the HttpContext.
But this seems to be a paradox. While already on the HttpContext to Mock (This enhanced testability), but over-reliance on HttpContext for unit testing is also an injury. This is due to HttpContext object nature: it is too complicated. You should have noticed, this is a pet in a set of thousands of objects, from request, response, application, cache ... ... contains a Web application, almost all the information needed. If you want to test a method depends on the HttpContext, you are bound to the Mock HttpContext object for the filling all kinds of information - depending on the complexity of business may be. Moreover, Mock concern is "behavior" that is concerned about the things that used to do a "path." Then if one thing can be done using multiple paths, how will the need to prepare before the test all the paths, and verify that the code being tested, "adopted and used only one path," therefore, Stub slowly coming into line of sight. Stub concern is the "state" ... ... This is another topic, but also involves the use of Record & Replay or Arrange-Act-Assert way to unit test, put aside.

Speaking prior to unit test the view, Old Zhao has been talked about in the view should only use the data ViewData. This is not the first time talking about giving up HttpContext, and since has "abstract" is a powerful weapon, the all "disharmony" factor can be isolated. Just in the MVP mode, View and Presenter are used to interact with their abstract, all Web controls, HttpContext and other objects are gone, we have eyes only for "data" and "model." Similarly, in the ASP.NET MVC in Action approach, should not use the HttpContext, which is based on the good "can test" of the consideration. You may want to, now HttpContextBase object already Mock the ah. Yes, it certainly "can", but doing so will cause the expansion of the unit test code because the test code in a considerable part of the test data must be concerned about the preparation, instead of being tested for functionality. Action for a method, it should be of concern to the user interaction with business logic, rather than "how to HTTP requests into usable data." End of the day, or to "separation of concerns."

In the ASP.NET MVC responsible for "transforming the data" level for the Model Binder. On this point, the existing "sample" most concerned about the Form or QueryString in the data into Action parameters, but where in fact available Model Binder more. For example, in "best practice" code, the original AccountController the Delete method implementation is as follows:

public ActionResult Delete (string userName)
(
this.MiddleTier.UserManager.Delete (userName);

Uri urlReferrer = this.Request.UrlReferrer;
return this.Redirect (urlReferrer.ToString ());
)


After the removal of the specified object, the page will jump to the Url Referrer address. In the above code, this value will be obtained by visiting Request.UrlReferer to. This makes your Action methods and HttpContext produced dependency unit test code so it will need to write this:

[TestMethod]
public void DeleteTest ()
(
string userName = "jeffz";
Uri urlReferrer = new Uri ("http://www.microsoft.com");

var mockHttpContext = new Mock ();
mockHttpContext.Setup (c => c.Request.UrlReferrer). Returns (urlReferrer);

var mockController = this.GetMockController ();
mockController.Setup (c => c.MiddleTier.UserManager.Delete (userName)). Verifiable ();
mockController.Object.ControllerContext = new ControllerContext (
mockHttpContext.Object, new RouteData (), mockController.Object);

mockController.Object.Delete (userName) ...
)


In the unit test code, we had a HttpContextBase Mock object, it's Request.UrlReferrer property returns the object we are ready, and then construct a new ControllerContext and to the Controller. And if we UrlReferrer Delete method can be used as the parameter, then the unit test code is at once much simpler:

[TestMethod ()]
public void DeleteTest ()
(
string userName = "jeffz";
Uri urlReferrer = new Uri ("http://www.microsoft.com");

var mockController = this.GetMockController ();
mockController.Setup (c => c.MiddleTier.UserManager.Delete (userName)). Verifiable ();

mockController.Object.Delete (userName, urlReferrer) ...
)


Some people may ask, not that the properties from the Request for UrlReferrer value you why we construct a ControllerContext, you can not directly set the Controller object such as so much simpler:

mockController.Setup (c => c.Request.UrlReferrer). Returns (urlReferrer);

Seems feasible, but you will find the time to run, the framework will throw an exception, that only members of the interface, or you can override the members can be Mock. Yes, Controller's Request property is not virtual and can not override. Controller class so the design is intentional, the purpose is to limit the available paths. Imagine, if you Mock the Controller.Request property, but by Controller.HttpContext.Request visit code, how to do it well on the way similar to the practice of overloading design.



In general, all of which are several ways to delegate to them the only way, and only that method can be override the. In preparing this test, we have only identified Mock entrance to avoid the test code to understand methods to achieve over the issue.

Back to the topic. If you want the Delete method of access urlReferrer by the parameters, then we have to prepare Model Binder-related components:

public class UrlReferrerModelBinder: IModelBinder
(
public object BindModel (
ControllerContext controllerContext,
ModelBindingContext bindingContext)
(
return controllerContext.HttpContext.Request.UrlReferrer;
))


And it can be directly applied to the parameters of the Action:

public class UrlReferrerAttribute: CustomModelBinderAttribute
(
private static UrlReferrerModelBinder s_modelBinder =
new UrlReferrerModelBinder ();

public override IModelBinder GetBinder ()
(
return s_modelBinder;
)
)


Consequently, we have the Delete method can be written as:

public ActionResult Delete (string userName, Uri urlReferrer)
(
this.MiddleTier.UserManager.Delete (userName);
return this.Redirect (urlReferrer.ToString ());
)


Today's code, whether the application or the framework of the library, must consider "testability"







Recommended links:



Premier Trace And Ping TOOLS



Business STORAGE



OGM converter



2009IT affect China - moved to action



MP4 To Flash



Evaluate Religion



DLL calling convention and name of the MODIFICATION in



Qihoo give the flag To win users for free



How to control DATAWINDOW in PB modify column properties



Webmaster Tips Optimize Network Performance To



Dell is holding a 10 billion U.S. dollars In cash ready to deal



3GP to FLV



Proper way to make money: to be a know how to "lose money," the dealer



Yuan Meng: Ubuntu 9.10 new Version of the debut



Life Difficult For PC Industry In 2009, Will Welcome The First Decline Since 2001



With CloneCD CD-RW disc to save the damaged



Tuesday, October 5, 2010

Alliance with the merger as


Two years ago, is busy will Renke Na into the capsule in the Oracle broke the news going on between Microsoft and SAP merger talks, which succeeded the media focus shifted to the competitors from their own office. And is an anti-Microsoft merger rumors in the past, "no comment" attitude, formal recognition of the SAP had indeed made in the late 2003 merger intentions, and in subsequent months, the two sides explored the possibility of merger. However, when they found that the merger is too complicated, they abandoned the merger.

Microsoft said the two sides do not intend to re-start of the merger, but the contact was to deepen the mutual understanding between Microsoft and SAP. In fact, the two companies found in the communication process, with the merger, as an alliance, it can also achieve the desired results. Therefore, further deepening cooperation between the two sides.

Duet born

SAP with Microsoft intent beyond the industry expected. Once the merger of two companies, the world software industry structure will be radically changed. But later, a time when Oracle acquired PeopleSoft antitrust trial, in a voice of anti-monopoly, the two companies had no further negotiations on the merger. But no thought of Microsoft Chairman Bill? Gates and SAP CEO Henning Kagermann or come together.

Then in May 2004 of the SAP annual "Sapphire User Conference," Kagermann announced that intention only, but also unexpectedly in his speech during the broadcast a video - which, Bill? Gates describes in detail Microsoft and SAP deep in the field of integrated product line. Just a year later, in Copenhagen 2005 user conference, SAP and Microsoft also announced that it will set the two sides jointly develop and market two new software functions into one product. This is considered the two companies to the latest major achievement, competition, aimed against the Oracle, of course, IBM.

May 11, 2006, SAP and Microsoft first announced in June the two sides will begin a two-year time sale conceived crystal - Duet (Chinese paraphrase: duet), one for Microsoft Office and SAP ERP software. According to a press release jointly issued by both sides as described, Duet allows information workers to use their familiar Microsoft Office environment to access selected SAP business processes and data. Users can use this method more flexible access to SAP applications to help companies use Duet save time and money, improve on the process of compliance, assist in decision-making.

Now, every day millions of information workers use Microsoft Office system work and collaboration. However, most of them also have the use of business processes and information in order to work smoothly. As established in the enterprise business processes and accurate information to the communication link between the lack of a unified, easy to produce irrational decision-making, a breach of company policies. Help in the Duet, companies can provide users with a familiar environment - this environment, rapid deployment, no special training - thus promoting enterprise systems and business processes in the 鏅強 users.

Who is who account for less

For this cooperation, Bertelsmann (China) Jiang Wei Direct Group IT director, said the cooperation between the two terms is undoubtedly beneficial for the users, and for co-operation would be a win-win situation in terms of both sides. He affirmed that the pre-SAP is beneficial to some, after all, Office of the user base is immeasurable, but later on the impact of Microsoft's MBS may be greater. Jiang Wei's view with the SAP product and technology group president, executive director Shai (Shai Agassi) coincidentally, Shai is also being honest with SAP, the potential financial help much. "SAP customers in almost all of the majority of Office, but directly with the SAP application users less."

Farmer Spring Co., Ltd. Hu Jian, director of information products is also considered the introduction of more help Duet SAP applications through Microsoft to seize the small market share. He said that SAP may be aimed at existing customers selling Duet, but with the Office and SAP software to enhance integration, may allow SAP and Microsoft Dynamics ERP sector intensified competition for customers. "This is not necessarily a good thing for Microsoft Dynamics department. Duet Dynamics will reduce the amount of the SAP user base."

In response, Microsoft Business Division President Jeff Raikes also behave more "generous", Raikes acknowledged Microsoft and SAP compete in certain areas do, but Microsoft will firmly support the Duet plan. "We are different companies, competition in several areas, but both sides can do is to build effective partnerships. This is an ambitious plan."

SAP CEO Henning Kagermann has also repeatedly stressed the external: "Our strategy is to open software, to attract small business development in the SAP platform." Today, SAP is actively absorbed third-party developers to write add-ons for the SAP software, 150 wife and children business success, while 350 are certified.

Since late 2005, SAP and Microsoft's nearly 100 joint customers and partners (including Atmel and Infosys) has been the experience of the advantages of earlier versions of the software. In addition, SAP and Microsoft also announced a number of projects, and hopes that these plans this year to improve the performance of Duet, and the second half of 2006 other related business program.

Common opponents

Ever since Microsoft officially involved in the rule of SAP enterprise applications software market once, SAP will be many occasions considered the future of Microsoft's biggest competitor, but the global enterprise as the first and third largest independent software supplier jointly between , is simply directed at their common rival - the world's second largest independent software company Oracle provider.

Oracle is among opponents by a combined vertical and horizontal isolation.

In addition, SAP has also strengthened cooperation with IBM to strengthen its business management software, configuration, maintenance and availability, improve software performance. In turn, users can also provide the platform for SAP on IBM's database under the automatic configuration. This is the first IBM software vendors for specific applications simplify the database. It is believed that the users in the SAP, 60% (about 44,000 firms) using Oracle database, and co-operation with SAP will help IBM to vie with Oracle customers. Coincidentally, this cooperation also took place in a time when Oracle acquired PeopleSoft.

Oracle, SAP can be described as heavyweight rivals. According to "Business Week" disclosure, SAP has 26,000 global enterprise customers with a complete product line, SAP in enterprise applications software market is almost equal to the money market "gold standard." Goldman Sachs predicts that four key enterprise application software provider of software license revenues, SAP's share has grown to 70% last year. Oracle (including PeopleSoft) share dropped from 30% to 20%. The SAP's major league, so that business applications software industry, technical direction backward side of unfavorable Oracle. Select Microsoft Office, undoubtedly, has spared no effort to promote Java, Oracle pressure; choose IBM, is also proud to make Oracle's database business challenge. SAP and Microsoft's co-operation far beyond the total amount of Microsoft Business Solutions total revenue, with IBM's cooperative business volume has exceeded the value of IBM middleware.

Today, SAP is in a honeymoon period and Microsoft has made preparation to introduce customers to their newborn Duet, but the future is unpredictable variables, especially for large enterprise applications UNIX users have yet to be tested. Shai naturally saw this, he said Duet would support the 2007 release of Microsoft Office, and the next version of MySAP ERP. The application process will be through a XML user interface language (called "XAML") to integrate.

Even so, the honeymoon period of the two sides can come another period of warm love to jointly promote the Duet, need time to test.






相关链接:



ie innerhtml Unknown runtime error eliminate



VOB To WMV



MKV TO MOV



YouTube to FLV