I've got a system that has Java server side objects and C# client objects. The objects can be modified in both sides of the system and so have business rules attached to them. The thing is currently the business rules only live on the server side, and I really want to avoid having to contact the server every time I want to verify the objects.
So ideally I want to have shared business rules that can be used in both C# and Java, so when a change is required I only apply it in one place (and obviously deploy in 2).
Has anyone any suggestions on how I could go about this?
This may sound terrible at first, but you could in fact consider coding the business rules in javascript.
There are javascript engines available both on the java and .NET platform. That way, by hosting a (different) javascript engine both on the server (java) and client (C#), they can both execute the same javascript to enforce business rules.
Think of it as your business rules language of choice. It's not a bad choice for the task either, as it is terse, flexible and well known.
I have done something similar once, to set up flexible game rules in a java-based game. Javascript engines are surprisingly simple to set up, and nowadays they're getting pretty fast too.
Try using IKVM
Related
This is hard for me to explain and even harder for me to visualize how I'd do it, since I don't know the bounds of communication with websites with Java and C#, so if I banter a lot/make no sense in the process of describing this, I apologize.
Basically, I'm making a 'bot' for www.plug.dj. This bot is able to do things on command like kick users, ban users, send chat messages, delete chat messages, say random things, etc. As of right now, it's powered by a simple one-file JavaScript code with a ton of listeners and callbacks using the Plug.dj API to handle them. This is ALL engineered by JS -- on the back-end, I think Plug.dj is powered by Python, I could be wrong.
Anyway, what I would LIKE to do is create this bot on a language other than JS. It's really basic and not super powerful, and there are things like communicate with databases and such that I'd like to implement that aren't possible/convenient with JS. I just want to know if this is possible, and if so, where should I start looking?
I'd prefer a language like Java or C#. If there's any more info you need to know in order to answer this, let me know, please! I'd like to start working on this, I think it'd be fun to learn how to communicate with websites with Java/C#/whatever.
If the bot javascript runs on "their" server, then there is no simple solution. They are providing a mechanism for running "your" javascript on their server, but the chances are that they don't support other languages. (And the only way to find out would be to ask "them"). Assuming that the answer is "no", you would need to investigate whether you can implement your "bot"s functionality in client-side code; e.g. a custom client that you implemented from scratch in Java or C# or whatever. That's a big "if" ... because it will only work if they expose the server functionality you need in their external APIs.
OTOH, if the bot javascript runs on "your" server, then you should be able to change it to support other languages. (It wouldn't necessarily be easy though ...)
My advice would be to take a deep breath ... and stick with Javascript. We all have to use languages that we don't think are "fun".
I honestly would just leave it in javascript if it is something you need to have run in the client.
If you need to make database calls, you can introduce a web services layer in between against which you can make AJAX calls which interact with databases.
I think your perception of javascript as basic and not very powerful is not a very good one. They are very complex apps build today in just javascript and HTML5. You just might need to start looking at things like backbone.js, underscore.js, and similar libraries that can help provide more advanced code organization functionality available to you.
If however you are looking at building something that individual clients are not going to have installed in their browsers, but rather would just interact with the website in an automated admin, then certainly you can establish you own web service in whatever language you like to interact with their API's and perform admin tasks.
If they provide a JavaScript library that runs client side, it seems likely that it will be communicating with the server over HTTP. Therefore, it should be possible for you to analyse the library and the calls it makes to reverse engineer the server API (which would be the HTTP calls) and re-implement it in the language of your choice.
Looking at the code of bot.js:
https://github.com/backus/Plug.DJ-Javascript-Chatbot/blob/master/bin/bot.js
it seems everything comes down to calls against their API object, eg API.getDJs(), API.getWaitList() etc. If you can determine how this API object works, then you might be able to reverse engineer and re-implement.
The software company I work for offers data conversion as a service for new clients who have previously used other similar software. I've written a VB.NET application to automate common conversion operations, and have separate logic between known vendors. A particular file layout has become rather common, but due to the nature of how this particular competitor's application stores information, certain fields mean one thing for one client, and another for a different client.
Certain elements within this vendor's format change every time, so I've written the application to account for that. Because some data fields mean different things to different vendors, I have to change my mapping code every time. This wasn't an issue when I had one of these every six months or so, but these are becoming much more common and I would much rather find a way to further automate this process.
I was thinking of implementing a simple interpreted scripting language that would allow me to save the conversion settings for the client in a text file, so I'm not having to feed the settings into my app every time I run it. My goal is to have the time spent in implementing this pay off in faster conversions in the long run.
This is the example I had in my head:
# this is a comment
RECORD INCLUDE(source[Class] != 'I' && source[Category] != 99);
FIELDMAP
destination[name] == source[name];
desintation[address] == source[mailingaddress1];
...
END FIELDMAP
BEGIN
# logic goes here
END
I don't want to make this more complicated than it needs to be. I admit that I've never even looked into scripting and am kinda clueless as to where to start or what other considerations need to be made.
Are there already scripting libraries for .NET that will do most of what I want? Is scripting the wrong way to go about this? I really don't want to reinvent the wheel if there is a better way.
Thank you for any help. :)
IronPython (one of the the DLR languages Jake referred to) is a .NET implementation of the Python scripting language. It allows you to access .NET code (including framework classes and class you've written) and would be an excellent choice for this sort of task.
It doesn't have any real dependencies (other than the .NET Framework) and you won't need to learn much learn much Python-specific syntax in order to get the job done.
(If you decide to go with IronPython, I recommend using Notepad++ as your editor.)
Rather than write a custom language, you could embed any of the DLR languages (IronPython, IronRuby, or JScript), or you can use compiler services to compile VB.NET from your application.
Another idea ... if you only need to change the mappings between variable names, maybe you could just come up with a little XML file that would define the mappings.
I'm about to embark on a new project within which we require the ability to re-use validations based on (preferably XML) on both the client and server.
We would setup a service to provide the XML validation configuration data to the client side.
The following is not meant to be inflammatory in any way.
The Enterprise library does have support for the validation of objects to be configured in XML but java developers would not have access to a java reader version of this XML interpretation.
There is also Spring.Net validation but again I think this may be tied too much to .net. Is the Spring.Net validation suite straight ported over from the java spring framework i.e. without changes to the xml config?
Is there any other frameworks for validation which are able to be used in both .Net and Java?
The project will be fully SOA and the validation is one of the last things I have to figure out.
EDIT:
To clarify the validation needs to occur within the language that the receiving client is using, i.e. if the client to the web service is Java then the validation would be read into java and validated within java so that error conditions could be reported to the UI for the user to rectify. Equally if it was a .net client the .net client would be able to read it in and provide the same functionality.
I don't want to validate within the xml, the xml will be a set of rules, i.e. Customer.Name will be a maximum 50 chars long and must be at least 5 chars, and is a required field.
Thanks
Pete
Have a look at DROOLS.
There are .Net and Java versions of the rules engine.
Java Link and .Net Link
I've not user the libraries, so cannot comment on how "seamlessly" the one set of rules could be used in both environments.
How about trying the validation in a scripting language that can be run in both the jvm and by .net.
Scripting languages would be ideal for this kind of logic so maybe:
Ruby - http://www.ironruby.net/ and http://www.jruby.org/
or Perl.
This approach would allow use to the exact same code for validation and then call this from Java or .net.
Using jruby wouldn't be much of a performance overhead and it can integrate very closely with java. I've less experience with Ironruby but from what I've read once the code has been loaded and is running the performance is ok and it can be integrated well into the .net code - see: http://www.ironruby.net/Documentation/.NET/Hosting
Not to take away from my answer but regardless of how you do this it will involve introducing a new technology with all the associated overheads - dev environment etc. A better approach may be just to do it in .net and java seperately but maintain a very extensive test suite of examples to ensure that two validations remain in sync.
Not sure what sort of validation your are trying to accomplish. If your business objects are going to be serialized in XML form, then aside from schema validation, you can augment that with additional business rules and checks using Schematron.
Schematron is an ISO standard and provides a way of encoding business rules, restrictions and validation that is not possible in XML Schema.
The Schematron differs in basic
concept from other schema languages in
that it not based on grammars but on
finding tree patterns in the parsed
document. This approach allows many
kinds of structures to be represented
which are inconvenient and difficult
in grammar-based schema languages. If
you know XPath or the XSLT expression
language, you can start to use The
Schematron immediately.
I have a library with some objects that I use both from C# and JavaScript. There are several objects, let's call one of them Foo, with the same basic implementation in C# and JavaScript. When I want to transmit an object of this type from the server to the browser, I simply call Foo.ToJson() to serialize this object to JSON and then revive the object on the browser side with a safe eval() operation.
This is all well and good, but the library is becoming complex and the need to keep the JavaScript and C# code bases synchronized is increasingly difficult and error-prone. I'm interested in ideas for simplifying this architecture or making it easy to maintain. Thoughts?
You could look at using script#: http://projects.nikhilk.net/ScriptSharp
Since your classes are all coded in C#, you could reuse the same .cs file for both the server and client projects. that way, when you make changes, it's automatically compiled into the javascript :-)
have you considered making your "shared code" into a web service, this way any of your applications can access it, and you can make everything distributed to help with performance.
Just one solution.
http://en.wikipedia.org/wiki/Web_services
Make the parts of your app that overlap as data-driven as possible. At a certain size, it's easier to have a state-machine interpreter running on both sides that can parse the same data.
Write the code once. There are a couple C# to JavaScript compilers you can look into. Script# and jsc.
Use DTOs. Data Transfer Objects.
This way if your JScript or C# objects change, your DTOs don't necessarily have to change unless that property is immediately required at the client. And even then, you have a clear separation of what is intended to be Serialized and what is not.
Dave Ward at Encosia speaks a lot about this.
In my project I need to create a business object validation layer that will take my object and run it against a set of rules and return either pass or fail and it's list of failure reasons. I know there are quite a few options out there for accomplishing this.
From Microsoft:
Enterprise Library Validation Application Block
Windows Workflow Foundation Rules Engine
Open Source:
Drools.NET
Simple Rule Engine(SRE)
NxBRE
Has anyone had any particularly great successes or failures with any of these technologies (or any that I didn't list) or any opinions on what they feel is best suited for business rules validation.
Edit: I'm not just asking about generic validations string length < 200, zip code is 5 digits or 5+4 but assume that the rules engine would actually be leveraged.
The code-versus-rules-engine decision is a matter of trade-offs, IMHO. A few examples are:
Advantages of code
Potentially higher performance.
Uses developers' existing skills.
No need for separate tools, run-time engines, etc.
Advantages of rule engine
(Features vary across the various rule engines.)
Rule DSL that is writable (or at least readable) by business users.
Effective- and expiration-date properties that allow automatic scheduling of rules.
Flexible reporting from rule repository supports improved analysis and auditing of system behavior.
Just as data-base engines isolate data content/relationship issues from the rest of the system, rules engines isolate validation and policy from the remainder of the system.
A modified version of the CSLA framework rules.
Many of the other rules engines have the promise that goes like "The end user can modify the rules to fit their needs."
Bahh. Very few users are going to learn the complexities of the rules document format or be able to understand the complexities and ramifications of their changes.
The other promise is you can change the rules without having to change the code. I say so what? Changing a rule even as simple as "this field must not be blank" can have a very negative impact on the application. If those fields where previously allowed to be blank you now have a bunch of invalid data in the data store. Plus modern applications are either web based or distributed/updated via technologies like click=once. So you updating a couple of components is just as easy as updating a rules file.
So, because the developer is going to modify them anyway and because they are core to the Business objects operations just locate them in one place and use the power of modern languages and frameworks.
I didn't really like rule and validation blocks provided by Microsoft (too complex and inflexible) so I had to build mine, based on experience with custom business workflow engines.
After a couple of iterations the project has finally gone Open Source now (BSD license) and has proven helpful in production systems. Primary features of .NET Application Block for Validation and Business Rules:
Simple to get started with
Rules for the domain objects
Rule Reusability
Predefined Validation Rules
Behavior Extensibility
Proper object nesting
Designed for DDD and UI level validation
Multiple reporting levels
Production-proof and active development
Small codebase
Open Source
Here's how a simple binding of rules at the UI level looks like:
Note, that current implementation does not have any DSL at the moment. C# syntax is expressive enough on its own, so there has been no demand to add Boo-based DSL on top.
I have to admit, for really simple validations, I tend to write my own very small, compact rules engine, mostly because I think using someone else's implementation just isn't worth it for a small project.
I've experimented with Workflow Foundation, used EntLib, and written my own rules engine.
In small applications where I only really need to do UI-based validation to ensure invalid data doesn't sneak into the DB, I reach for the EntLib Validation Block. It's easy to use and requires only a minimal amount of code in my domain objects, plus it doesn't mess up NHibernate or anything else in my technology stack.
For complex stuff, domain-layer validation, etc., I'd easily opt to write my own rules engine again. I'd much rather write rules in code, each rule in it's own tiny class, easily testable and very simple to compose complex sets of rules with.
In the large app that I worked on where I wrote this sort of rules engine, we then used FitNesse to test our rule configurations. It was great having that kind of tool to utilize to ensure correctness. We could feed it tables and tables of data and know, with certainty, that our configured rules worked.
If you are interested in rolling your own, read JP Boodhoo's post on Rules processing. Essentially he lays out a straight forward framework for validating domain objects.
Validatiion in the Domain Layer
Validation in the Domain Layer 2
Try
http://rulesengine.codeplex.com
It's lightweight, uses fluent-interfaces to define validation logic, extensible, and Free!
You can even define rules on interfaces, implementors inherit the rules.
No more annoying attribute-style validation - what it you don't own the class you want to valida
It has a plug-in to Asp.Net MVC (server-side only).
There is also another project called Polymod.Net which uses RulesEngine to provide self-validating UI's as shown in the screen-shot!
Enterprise Library Validation Block provides a very AOP like approach and keeps things simple in both 3.1 and 4.1 from my experience.
I recommend using CSLA Framework. Not only for Validation but for other features also.