Validation framework in C#? - c#

In the java world there is the bean validation framework JSR-303 which is a nicely well thought out strategy for performing data validation in both the presentation and persistence layers of an application. It covers a lot of things, including validation of whole graph models, validation domain grouping, i18n, etc.
I have failed to find any data model validation frameworks in C#. Are there anything similar to JSR-303 in C#?

You can try fluent validation:
http://fluentvalidation.codeplex.com/

There is the Enterprise Library Validation block.
http://msdn.microsoft.com/en-us/library/ff648831.aspx
It may not do some of the specific items you are asking about (i18n) out of the box, but it fits the bill for many other use cases.
Also, is free to use and has source code available.

On the front end:
The web side (asp.net) has Validation controls for web forms, and Validation Helpers for MVC. Both of these are smart enough to know how to render validation logic to the client page (for faster failure responses and reduced server load) and duplicate the necessary logic server-side (since you can't trust the client).
The Windows side has Error Providers for winforms. I'm not sure about WPF/Silverlight, or if there's anything for Console apps.
The result is that things are a little fragmented, but not so bad that you can't quickly find what you need. The idea is find the approach that's best for each platform, rather than shoehorning different platforms to the same set of principles.
On the back end, you can use the Enterprise Validation Application Block.

There is also the data annotations that can help with client and server side validation: http://msdn.microsoft.com/en-us/library/ee256141.aspx

Related

Client side validation with Jquery - MVC C# app

I am working on a very LEGACY ASP.NET MVC app using C# which is built from 2009. They don't have any client side validation at all. In other words, they don't use validation mechanism provided by the ASP.NET MVC framework as the following link
https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-5.0#client-side-validation
They use service, interface etc... to do validation. Not using data annotations, either. As a result, they don't have unit tests for model validator. All validation rules will be tested manually through server side only or the unit tests have strong dependency to Resource.resx or other services which I don't think they are unit tests.
Therefore, I have added a client side validation and server-side validation by using data anotations.
Unfortunately, my PR is declined because they said that they don't want to use or add jQuery to the current application.
The reason is that: "they want to build portable micro-frontends pointing at APIs rather than all encompassing large applications the current app. because of that jQuery Validate is at the complete opposite end of the scale from zero compromise. It can be done by HTML5 and plenty of other client side only JS libraries there are plenty of better compromises. The other end of the scale (our ideal) is the validation happens in an encapsulated web component that uses Angular Forms." (An answer from a Front end developer)
The problem is that we have not the plan to replace that legacy app yet and I don't see the reason why I cannot use Validation mechanism from Microsoft to do both client side and server side validation. My thought is that it is better to do validation in correct way, rather than let it be bad and will be replaced by web-component soon.
Based on the source code, I am not sure they are able to apply proper validation way in new technology because with the ASP.NET MVC app, I think they did not apply correctly or I don't think they understand the framework.
Any ideas why I should not use jQuery for MS's validation on a legacy app?
Thanks

Razor framework - backend or frontend?

What kind of framework is Razor? Is it backend or frontend?
What is the difference between the two types of frameworks?
I'm trying to learn a little bit more about backend and frontend frameworks and since I usually work with Visual Studio Asp.net MVC was wondering about it.
It is not a framework . I think you're misinterpreting certain concepts. Razor is a server side view engine, and it uses C # or VB.NET to generate dynamic content.
Razor Syntax Quick Reference
This question is a couple of years old, but I'm going to add my two cents.
People struggle to give an answer to this question because the terms 'front-end' and 'back-end' aren't formally defined anywhere. Because of that, any answer is purely subjective.
That being said, it is my opinion that the relationship between front-end/back-end and client-side/server-side isn't necessarily one-to-one
I think it helps to think of it like this: client-side and server-side are run-times, while front-end and back-end are a separation of concerns.
'Client-side' always refers to code executing on the client's machine and 'server-side' always refers to code executing on the server. A 'front-end' developer deals with displaying data to the user and getting data from the user, while a back-end developer deals with storing, manipulating, and retrieving that data.
Consider a front-end developer who is tasked with building a UI. Much of the code they write will be the typical HTML / CSS / JS. However, they will also have to deal with the data that is passed to the front-end from the backend. This is where Razor comes into play. The front-end developer will write the Razor code (which executes on the server-side) to display the data.
That is, the front-end developer will write server-side code to help generate the UI, in addition to writing the client-side code that really defines the UI.
Now, I can't imagine a scenario where a back-end developer will write client-side code.
So, to answer your question, Razor is a front-end technology that executes on the server-side runtime. It's only purpose is to generate the UI, which is the concern of the front-end.
Razor is for writing dynamic html page which is front end and c# is for writing backend logic. Although you could move all the backend logic inside razor but its highly not recommended.
Razor allows you on the back-end more easily create views (.cshtml in C#).
It is more like templating system...
http://www.asp.net/web-pages/overview/getting-started/introducing-razor-syntax-c

Keeping the same logic implemented in C# and JS in sync

There is a need to have a calculator for our invoices which will be used both in the web UI and in the back-end. This requires two implementations of the same logic in Javascript and C# and the real problem is to keep these two logics in sync when it comes to changing the rules.
At the moment, what we do in the JS side is that we send all the requests for recalculations to the backend through ajax calls in order to use the calculator implemented in C#. This keeps our servers busy and I don't like the idea. I think that there should be a rule engine or something somewhere that we could utilise to keep the calculation logic in a language other than C# or JS and use/interpret it in both the front-end and the back-end.
Has anyone else faced the same problem? Any idea would be appreciated.
Faced the same problem and solved it by implementing the logic in javascript and then run it with https://jurassic.codeplex.com/ on the server. Works nicely.
You can try using Haxe to implement your rule engine and then cross-compile it to JavaScript and C#. This would give you a common implementation base for both and then - during compilation - your two different language outputs would be formed.
Disclaimer: I haven't used Haxe yet so I can't testify if it'd work for your situation but it supports both languages as targets.
You can use any decision-as-a-service platform. They usually expose decisions(bunch of connected business rules with relations and dependencies) as a REST API service. So you can simply interact with the service for execution and management of rules.
For example here is a sample how you can communicate with decision service for execution by passing input parameter values in JavaScript.

Easily branded site architecture

I recently attended a demo of a large-scale enterprise system, whose web pages may be customised to the point of including fields added by the client. The way I understand it, their architecture is made up of the following layers:
Database
Web service API
XML files that dictate layout
The web pages that are generated from the XML files.
When I was asked to investigate building a web portal which could be easily branded, that struck me as a good way of going about it. The question now, is how one would design it.
I understand the database and web service layers, but I am a little confused by the various possibilities for building web sites in .NET.
Considering the requirement for customisability and the architecture from above, here is how I understand the options:
Webforms - the option I am most familiar with, but it is essentially enriched HTML with code-behind. I think there will probably be a lot of work to make it work with the idea of an XML layout.
WPF - the XAML middle layer is built-in, but as I understand it, WPF can only really be used in browser applications and not websites.
Silverlight - more for building applets than websites, right?
MVC - This looks interesting, but all the demos I have seen use Entity Framework as well. It seems to me like Entity Framework with all its automatic code generation is much more suitable to applications that are all new. In my case, I have a very large database that already exists.
If none of the above are suitable, I thought of an alternative. One could do a stock standard Webforms site with a web service that returns the branding elements. That isn't quite the same as what I described at the top, but is sufficient for my needs.
Or am I barking up the wrong tree?
I think you're off on your criticism of MVC. First, you don't need to use Entity Framework, and secondly even if you did, you can do it database first to generate your entities.
Your assessments of WPF and Silverlight is pretty spot on imo.
You could do this with webforms, but I think you'll probably find doing it with MVC architecture will be cleaner. Very simply, if you use clean HTML and put all branding elements into an external CSS file (logos, colors etc), then you are half way there to a custom brand. Even a different layout could potentially be defined by the CSS file (although it might be harder for your end users to customize that look since they would need to know css pretty well)
Building additional fields is potentially more difficult:
Off the cuff, the way I'd be looking at implementing this would be a combination of my predefined fields in a standard database layout (users table with username, password, first name, etc etc) and additional support for the "customizable fields" using the Entity-attribute-value pattern
From there you will need to develop an extensible system to 1. generate a page from xml with the appropriate form elements (select, text input, textarea, etc). 2. Generate a generic model that will read the same XML file and be able to receive data from a posted form and know how to save that to the database (note in this case if it was ALL entity-attribute-value that would probably be easier to manage than a combination of standard relational and EAV).
You'll probably want to look at .NET Data Contracts as serializable entities to get an understanding of how you might design your XML files to be extensible to allow for things like "select menu has the following 3 options" or text input must match this regex.
Really keep an eye towards extensibility, because you can't build it all at once.

How can I share code between C# and Flex?

I am developing a Flex / Flash application which talks to an ASP.Net / C# backend. Is there any way I can share code between the two?
The server provides a reasonably interesting domain model which the client is designed to maniuplate. Ideally I would like to be able to define this domain model once and have both sides use it for consistency. I am after all the benefits that come with being DRY.
I'm new to Flex but the sort of thing I had in mind was some intermediate language that compile to both C# and ActionScript.
Update
I currently have a basic REST style web service which sends XML serialized versions of the objects down the wire to Flex. This works fine but what I am really interested in is being able to share simple business logic that goes along with these objects. There are certain business rules that need to be processed on both the server and the client and is possible I would prefer not to have to call back to the server for performance reasons.
I faced this problem as well, so I wrote a C# to ActionScript converter.
http://cs2as.codeplex.com/
Write your logic in C#, and add this utility as a post-build step. Then your business logic will be available in both environments. It works very well for me - I'm using it to share over 30,000 lines of code.
I'd hack together a domain model specification and have it generate models in both languages. But that's probably not the most time-effective thing to do.
Check out http://www.fluorinefx.com/ (It is open source flash remoting). I have used it extensively to call web services written in C# from ActionScript, and it works great. Once nice thing is that your c# can return a DataSet (or something similar) and the Flourine framework will convert that into an object ActionScript understands.
Not that I'm aware of, C# is essentially a strongly-typed, compiled language and ActionScript is a loosely-typed, interpreted language. Chalk and cheese I'm afraid.
What if you serialize the objects to XML and send them to Flex....that would at least let you share the data
Update
You can pass objects through the use of Lists, but what you're really passing through is just the data.
You can't really pass a 'method' to a client.
They don't necessarily have to honor it. Once they're in possession of the data, you can't control whether they honor the methods you passed or not.
You wouldn't want to trust anything they send back to you subsequently.
I think the issue is with the set-up.
You can process anything you want on the client side using ActionScript, and you'll just have to put the business logic you want to manipulate in the ActionScript side of things if you want to do it on the client-side.
Olde Answer
I use Flex and C# together through the use of a web service layer.
You may want to look at creating web services to have flex talk to your C# code.
Web Services with ASP.NET
CodePlex ASP.NET Tutorial (web services)
try weborb # http://www.themidnightcoders.com/products/weborb-for-net/overview.html

Categories

Resources