How to use MEF to add validation aphorism apparatus to an ASP.NET MVC Web application.
The Managed Extensibility Framework (MEF) has been allotment of the .NET Framework back .NET 4.0. There are abounding tutorials beyond the Web on how to use MEF with Windows Forms, WPF and Windows Store. MEF allows an appliance to be continued through a set of apparatus that may either be alien or exported. Today I’ll appearance how to add business aphorism plug-ins to a new ASP.NET MVC application.
To get started, actualize a new ASP.NET MVC 4 Internet Appliance in Visual Studio 2012 or 2013. Again install the MEF.MVC4 NuGet package, as apparent in Figure 1.
Next, add a new Chic Library called BusinessRules to the Visual Studio solution. The BusinessRules activity will accommodate the IValidate interface and some abject business aphorism apparatus that will alien in the MVC project. Again add a new chic called ValidationResult that has an IsValid boolean acreage and an ErrorMessage cord property. Your ValidationResult chic should attending like this:
Next, add the IValidate all-encompassing interface. This interface defines a Validate adjustment that accepts a all-encompassing ascribe blazon and allotment a ValidationResult:
Now add the IValidateMetaData class, which contains a Name cord property:
Now it’s time to add the aboriginal validation component, which validates a user’s email address. The ValidateEmail chic uses a approved announcement to validate the email, and allotment a ValidationResult article with IsValid set to apocryphal and an absurdity bulletin set if the email is not valid. The ValidateEmail chic exports the IValidate<string> interface and exports a metadata name of “Email”:
It’s now time to add the ValidateUsPhone class. It’s actual agnate to the ValidateEmail class, except is uses a U.S. buzz cardinal approved expression. I set the consign metadata name to “U.S. Phone” for the class:
Now that the BusinessRules chic is completed, it’s time to wire up MEF in the MVC activity to amount the validation rules. Accessible up the MefConfig chic file, amid at App_State/MefConfig.cs, and adapt the ConfigureContainer method:
The business aphorism apparatus are loaded into MEF through the businessRulesCatalog. In addition, I amount in any MEF apparatus independent aural the MVC activity accumulation through the assemblyCatalog object. The catalogs article is an AggregateCatalog accumulating that contains both the active accumulation and the business rules assembly. Lastly, a new CompositionContainer is created from the AggregateCatalog and returned.
The completed MefConfig chic book should now attending like this:
Next, accessible up the Global.asax.cs book and alarm MefConfig.RegisterMef() from aural the Application_Start method:
The aftermost footfall is to amount all the validation rules into an MVC form. Actualize a ViewModels agenda aural the MVC project, again add a new chic called ValidationFormModel to the ViewModels folder. The ValidationFormModel chic contains backdrop for anniversary of the ascribe fields and labels for the validation analysis form.
There’s an Ascribe acreage that will abundance the user’s amount to validate. The Rules acreage will accommodate all the accurate validation rules alien through MEF. The Aphorism acreage will be loaded with the user’s called rule, and the StatusLabel acreage will acquaint the user if their submitted ascribe is valid:
Now that the appearance archetypal chic is completed, let’s set up the Razor appearance for the analysis page. Accessible up the Home controller’s Index activity appearance at Views/Home/Index.cshtml, and archetype in the afterward markup:
The final footfall is to apparatus the Index activity on the HomeController class. Aboriginal I add application statements for MEF and the ViewModels namepsaces:
Next I add Validators acreage that uses the MEF ImportMany aspect to acceptation all validators that apparatus IValidate<string>:
Again I apparatus the Index HttpGet action, which creates a new ValidationFormModel with a loaded Rules drop-down account and endless it into the Index view:
Finally, I apparatus the HttpPost Index ambassador activity that accepts a ValidationFormModel. Aboriginal I get the called aphorism from the Validators accumulating by name:
How Asp Net Web Forms Tutorial Is Going To Change Your Business Strategies | Asp Net Web Forms Tutorial – asp net web forms tutorial
| Encouraged in order to my personal website, in this period I’ll teach you regarding asp net web forms tutorial