How to centralize validation rules in Asp.Net MVC 4? - validation

Below is a sample scenario:
User can create and/or update a policy online. While updating, user may change the start date of the policy. Condition is, if policy start date has passed (means smaller than today) then, start date can't be changed, else it can be changed.
My problem is:
There can be 5 types of policies and each has some common properties as well as some uncommon properties. Now I want to validate those properties against my business rules (like the sample) from a centralized place (may be class or whatever). But I am stuck on how/where to start.
Currently trying:
Currently I am validating those properties inside Controllers and sometimes inside Views but that is requiring lots of manual work and code maintenance will be troublesome in future. Can someone please give some idea.
Environment:
Asp.Net MVC4, Razor, VS2013

Related

Business Rule not working

I have made Business Rule for CRM and activated it, but it doesn't perform any action it should. It is activated, by the way.
I have checked the conditions multiple times and the look fine. I have also made JS Web Resource with the same conditions and it works.
The entity I am working with is custom entity(not activity). Is there anything I should enable on my entity to enable business rule, something like: Business Process Flows, Activities, Connections etc. or should I look for problem solver somewhere else?
When Business Rules have issues, they simply don't run (no errors, no nothing).
Make sure the scope of the Business Rule is correct (if you're not sure, pick the all forms option and safely forget about it).
Also, all the fields appearing in the business rule must be placed in the form body, if even only one is missing then the rule will not run. They can be locked and/or invisible if that's an issue.
Business rules that will be applied to Main and Quick Create forms, and to an entity, in the Web application and Microsoft Dynamics CRM for tablets. It is applied to the Main and Quick forms in the Outlook client in online and offline modes. (https://technet.microsoft.com/en-us/library/dn531086.aspx)
If all is good and the rule still doesn't run, clear it out then test it after each single change you make.

How to lock a field based on user role?

The old way I've always done it was to trigger a script on load and set the fields to this or that (hiding, locking etc.). But since we got those new, cool, shiny business rules now, I thought I could give them a whack.
The problem is that I can't figure out how to specify a condition based on the current user's role... Where is it hiding?!
They are not hiding because they don't exist. Business rules have a limited set of capabilities and they can't check the role of the users.
If you just need to lock the field Field Level Security can be an alternative, but for CRM 2013 you can only use on custom fields.
However good old JS + Plugin enforcement is (in my opinion) the preferred way.

what is the correct approach in order to host / integrate / show my existing MVC3 project inside orchard?

I've an existing MVC3 project that implements a certain functionality, this project has it's own views, and a separate Database.
now I'm required to use the same functionality inside one of my orchard project,so I thought that I can host this solution in somewhere and view it inside an iframe or something.
Am I thinking right?,
is this the correct step to take in order to achieve this requirement inside Orchard?
to make it more clear, all I need to do is to view this solution and interact with it's controls and views from a hosting page inside orchard, and the subsequent requests should be handled by my solution in order to hit it's own data store and get back with the requested data in order to be displayed to the user.
any help would be appreciated.
Update:
thanks for Bertrand Le Roy for his answer, I can now view my solution inside my
orchard website.
I came in to one more HUGE problem, which is that my application can no longer connect to my external database.
I've a DB that is hosted in some where else, and I'm using EntityFramework to deal with it.
the problem is that if I put the connection string inside my module web.config, or main orchard web.config, I run into several types of errors like:
"System.Reflection.TargetException: Object does not match target type."
or
"System.Data.MetadataException: Unable to load the specified metadata resource."
My question is: How could I pass my connectionstring correctly to my solution, assuming that I'm using Entity framework as my ORM.
Many thanks.
You will need to put it into a module.
You will have to move route definitions to a Routes.cs file (look at any existing such file for examples).
You will also need, in order to access your data store, to opt out of the ambient Orchard transaction around the data access code (using (var scope = new TransactionScope(TransactionScopeOption.Suppress))).
If you are using dependency injection, you may have some work to move that to the Autofac-based way of doing things in Orchard.
If you want your work to appear seamlessly in the Orchard admin, you may want to decorate your admin controllers with the Admin attribute. If you want your front-end to use the current theme, you'll have to add Themed attributes and maybe refactor your views so that they only emit HTML for the content zone instead of for the whole page.
Add a manifest (module.txt) to your module folder and you should be good to go.

Getting own EF 4.1 Code First classes to work with .NET Membership

I am working on a simple web application that is built with EF 4.1 Code First and MVC3. My Domain Model sits in a project outside the MVC3 project.
I have gotten to the stage where my web application is more or less complete. However right now, my application doesn't have any form of authorization or authentication. So a regular user can get to Actions and Controllers that only an Admin is suppose to have access to.
My question is, how do I get my web application to work with the .NET Membership ? Because I am aware that the .NET Membership has many database tables of its own !
My knowledge is fairly limited, thus any clear, easy to follow responses would be greatly appreciated.
After the Membership is set up, I know my way around applying Authentication and Authorization to Actions and Controllers.
Thank You.
Go into your C:\Windows\Microsoft.NET\Framework\v2.xx or v4.xx\ and execute aspnet_regsql, this will launch a wizard that allows you to choose your database and it will add the stored procs and tables to allow membership to function.
Then you run the ASP.NET configuration wizard (the little toolbox icon at the top of your solution explorer) and then configure your roles and accounts. See this page and skip to the Configuring the website for membership and roles section.
ASP.Net membership is actually kinda interesting and frustrating to an extent. To you run a tool called aspnet_regsql.exe found in your Windows\Microsoft.net\Framework folder which generates all of the necessary tables to get it running. Next move onto your Web.config file. If you started with your basic ASP.NET MVC 3 template then you should have a <connectionStrings> entry under the name "ApplicationServices". Change that to your database connection string. This is what the membership provider uses to hook everything up. If you look through the rest of the config file there should be sections for <authentication>, <membership>, <profile>, and <roleManager>. These deal with the various sections and settings that the ASP.Net user management is broken into.
At this point everything should be hooked up. If you select your project in the solution explorer, a little red hammer icon should appear. This is the ASP.NET configuration manager. You can use this to set up different profiles and manage different users.
Now this stuff won't be connected to your EF4.1 code first stuff at all. Instead, you can interact with your membership provider through static classes Membership and Roles. If you take a look at the AccountController on a default ASP.Net MVC 3 Internet application template it will give you a good idea how it works.
After working with this stuff for several months, I found it much easier to basically write your own. Tying things together eventually becomes a huge pain and there is a whole lot of extras that the membership provider gives you that is unnecessary for small applications.

Workflow engines that integrate easily/work well with Spring Web?

I have a Spring MVC web application that I need to introduce workflow into and was wondering if anyone had recommendations on existing products that would integrate well/easily with Spring.
The steps (if that's the term) in any given workflow may or may not involve user interaction - so you could imagine kicking off something like a sign-in workflow that involves the following steps:
- user choosing a login name and password and submitting
- once submitted an email is generated with a confirmation URL
- on click of the confirmation URL the user is asked a few more questions and then the account is enabled
- if URL is not click on in three days a reminder is sent
- if URL is not clicked on after reminder is sent, account is deleted
Admittedly this is a workflow that could be solved without a workflow engine and I'm just using the example to illustrate that the steps involved in the workflows may or may not involve user interaction, have a time element and a concept of branching on certain event.
I had a look at Spring WebFlow and this seems more geared toward putting webpages together in a certain order to achieve a particular flow through the UI as opposed to specifying a persistent workflow.
I had a look at Spring Workflow Extensions but (a)the documentation seemed a bit thin on the ground and (b) its still in incubation state, both of which make me a bit nervous.
Any suggestions?
I've worked with and evaluated a few workflow engine solutions. In general, they end up being so complicated to setup, it ends up being easier to code your workflow by hand. There is one open source project called OS Workflow that looks like it takes this complexity into account, but I haven't had an opportunity to give it a closer look yet. Also it doesn't look like the product is under active development currently.
I've used jBPM on a project, but I found the product to be confusing and not well documented. We ended up only using a very small set of the functionality and we plan on eventually replacing the functionality we're using with something homegrown.
Both frameworks also have Spring support via the Spring Modules project (which also looks to no longer have any development activity).
A quick google search found me this article on popular Java workflow solutions, but I'm not familiar with most of these products.

Resources