Inside a BizTalk Expression shape, I'm presented a blank text editor with some crude "examples" above, mocking me. "It's so easy" they chant. Try as I might, I'm just not making the connection. Maybe I'm over thinking it. I'm a BizTalk newbie. Coming from many years of heavy .NET and software engineering experience, my think doesn't seem to brain...
Would someone with great BizTalk experience enlighten me on this question: What is in scope and available for use inside the Expression shape? And subsequently, the MessageAssignment shape as well?
By scope, I mean like in real programming: Variable names, namespaces, etc.
Every example I see on BizTalk assumes you understand where these things come from. For instance, see this MSDN page: Using Distinguished Fields and Property Fields
It assumes I know where "MyMessage" is created, instanced, and accessible. I have no clue where to initialize it, what shape identifier to give a name, etc.
My design seems simple: When an error occurs, catch it, create an ErrorMessage out of thin air, assign string value to a distinguished field "Reason", and send to a send port. I can get everything but the expression right.
Any expert insight is greatly appreciated.
You can only create a message inside a Construct Message shape and then either
Map or an Assignment shape in side that. You can NOT construct a message in an expression shape.
Map option
For a map in an Exception block, the only messages you can map from are those that were created before the scope that the Exception block is in. So for an exception block for the whole Orchestration I map from the message received in the initial activating receive (which should be before the scope) and too the error message schema. Then you can have a message assignment in the Construct shape after the map.
Note: This initial received message is the only one you will be certain of having in an Orchestration if the scope covers all but the Initial Receive. I have only had it once in an edge case involving a badly formatted MIME message that caused the Orchestration to start up but not have this initial message.
Non map options
Creating it without a map using only a Assignment shape you either
need to call an external class e.g. The ESB Fault handling eSBFault = Microsoft.Practices.ESB.ExceptionHandling.ExceptionMgmt.CreateFaultMessage(); where eSBFault is defined as Message Type Microsoft.Practices.ESB.ExceptionHandling.Schemas.Faults.FaultMessage
Assign it from another existing message e.g. xTempDoc= wcfFault.fault; where in this example wcFault is the Exception Object Name set in the Catch Exception and xTempDoc is a Variable of type of System.Xml.XmlDocument that is then assigned to a message variable.
Manually create the message e.g below from creating a new message in the Message Assign shape
Example
xmlDocMessage = new System.Xml.XmlDocument();
xmlDocMessage.LoadXml("<Out><ErrorCode>4711</ErrorCode></Out>");
Creating a Message variable
For all of the above you need to go into the Orchestration view and create a Message variable of that name of the message and set it to the Message Type of the message you want to construct. It needs to be in the scope where you are constructing it or a an enclosing scope. Note: It will only be available in the scope where you define it or an child scope of the scope where it is defined.
Other limitations of the Expression Shape
For other limitations of the Expression shape see Requirements and Limitations for Expressions
#Dijkgraaf's answer is excellent, but technically speaking the expression editor is giving you access to the XLANG/s language. AFAIK you still have to declare variables as he illustrated (using the orchestration toolbox window). However, the language itself is somewhat similar to C# and .NET, and you can technically do just about anything in an expression shape that you could otherwise do in other shapes, using the proper keywords.
For example, it's possible to construct a message in an expression shape like so (taken from this blog):
construct OutboundMessage {
XmlDocument.LoadXml(
#"<?xml version='1.0' standalone='yes' ?>
<Root id='012345' xmlns='http://schemas.sample.org/BizTalk/2010/input' />
");
OutboundMessage = XmlDocument;
}
Typically, this is a bad idea. You should ordinarily use the regular construct message shapes - why? Because when you go to look at your orchestration a year later (or someone else does), it will be immediately obvious where you're constructing messages. Same can go for decision shapes - a decision shape might be overkill in some cases, but it will be immediately obvious to a future developer that there's decision logic (instead of looking for an if statement in an expression). Expression shapes should:
Always be properly named/labeled. Don't leave yourself trying to figure out what Expression_2 is actually doing, name it something useful like "Increment Loop Counter"
Avoid lengthy expressions. The editor is not very friendly (very limited intellisense, no highlighting, no automatic formatting). More than once I've lost code in an expression shape by hitting Esc instead of clicking OK and not realizing what I had done immediately. It's very good for XLANG/s specific things (like using the xpath function, or accessing distinguished fields, or doing simple things). If you're getting into more complicated logic, have your code call out to a C# helper library that the orchestration project references.
Related
I have a child class which wants to add more functionality to a base class function, how can I represent that it also does the base class function not just the newly added functionality?
Interesting question. I tried that with Enterprise Architect. It did let me select the parent's operation but the display in the diagram did not change. It seems like you need to use notes for that:
As you can see Class2 inherits from Class1. The SD shows a call to Class2's operation a(). The call to the super-class's Class1.a() as internal call shows the same signature. A note clarifies the situation.
Maybe there's something else possible with this. But that's what I came up with immediately.
P.S. I've looked up the specs. P. 575 of UML 2.5 says
The message-name appearing in a request-message-label is the name property of the Message. If the Message has a signature, this will be the name of the Operation or Signal referenced by the signature. Otherwise the name is unconstrained.
That would put in the option to specify the operation in question as Class1:a() or the like. Actually Enterprise Architect shows it that way in the properties of the message but shortens it to just the basic name. Just a border case, I'd guess.
I have a child class which wants to add more functionality to a base class function, how can I represent that it also does the base class function not just the newly added functionality?
Interesting question. I tried that with Enterprise Architect. It did let me select the parent's operation but the display in the diagram did not change. It seems like you need to use notes for that:
As you can see Class2 inherits from Class1. The SD shows a call to Class2's operation a(). The call to the super-class's Class1.a() as internal call shows the same signature. A note clarifies the situation.
Maybe there's something else possible with this. But that's what I came up with immediately.
P.S. I've looked up the specs. P. 575 of UML 2.5 says
The message-name appearing in a request-message-label is the name property of the Message. If the Message has a signature, this will be the name of the Operation or Signal referenced by the signature. Otherwise the name is unconstrained.
That would put in the option to specify the operation in question as Class1:a() or the like. Actually Enterprise Architect shows it that way in the properties of the message but shortens it to just the basic name. Just a border case, I'd guess.
I've found filter_var to be extremely useful in validating and sanitizing user input with PHP, but I've yet to find anything even remotely as convenient in ColdFusion (more specifically, CF8).
Obviously I can hack together something using REReplace, but that would take significantly more time to code up and would be much uglier than using the pre-defined filters available in PHP. Is there a more efficient way or do I just need to bite the bullet?
There are three different options available to you. Since you're attempting to manage user input, I assume you're using forms. isValid most closely mimics your functionality, allowing you to check if a value specified matches either a data type or a regular expression and returns true or false, and includes attributes by default to define a range. It does not support the ability to create a custom 'filter' beyond defining a regular expression however.
The second option would be using cfparam tags on your POST processing page, which allows you to specify the existance of a variable, test against a data type or define a regular expression, and optionally assign a default value if the variable doesn't exist. If you attempt to process a page where the field is not defined and no default value is assigned however, ColdFusion throws an error.
Finally, you can do validation by using cfform and cfinput fields on your form itself; which allows for client-side data validation for existence and types (it also supports server-side validation but it's implementation is sloppy), regular expressions, and input masking: taking user-inputted data and conforming it to a specific format (like adding dashes to phone numbers and zip codes).
I want to serialize an Entity Framework Self-Tracking Entities full object graph (parent + children in one to many relationships) into Json.
For serializing I use ServiceStack.JsonSerializer.
This is how my database looks like (for simplicity, I dropped all irrelevant fields):
I fetch a full profile graph in this way:
public Profile GetUserProfile(Guid userID)
{
using (var db = new AcmeEntities())
{
return db.Profiles.Include("ProfileImages").Single(p => p.UserId == userId);
}
}
The problem is that attempting to serialize it:
Profile profile = GetUserProfile(userId);
ServiceStack.JsonSerializer.SerializeToString(profile);
produces a StackOverflowException.
I believe that this is because EF provides an infinite model that screws the serializer up. That is, I can techincally call: profile.ProfileImages[0].Profile.ProfileImages[0].Profile ... and so on.
How can I "flatten" my EF object graph or otherwise prevent ServiceStack.JsonSerializer from running into stack overflow situation?
Note: I don't want to project my object into an anonymous type (like these suggestions) because that would introduce a very long and hard-to-maintain fragment of code).
You have conflicting concerns, the EF model is optimized for storing your data model in an RDBMS, and not for serialization - which is what role having separate DTOs would play. Otherwise your clients will be binded to your Database where every change on your data model has the potential to break your existing service clients.
With that said, the right thing to do would be to maintain separate DTOs that you map to which defines the desired shape (aka wireformat) that you want the models to look like from the outside world.
ServiceStack.Common includes built-in mapping functions (i.e. TranslateTo/PopulateFrom) that simplifies mapping entities to DTOs and vice-versa. Here's an example showing this:
https://groups.google.com/d/msg/servicestack/BF-egdVm3M8/0DXLIeDoVJEJ
The alternative is to decorate the fields you want to serialize on your Data Model with [DataContract] / [DataMember] fields. Any properties not attributed with [DataMember] wont be serialized - so you would use this to hide the cyclical references which are causing the StackOverflowException.
For the sake of my fellow StackOverflowers that get into this question, I'll explain what I eventually did:
In the case I described, you have to use the standard .NET serializer (rather than ServiceStack's): System.Web.Script.Serialization.JavaScriptSerializer. The reason is that you can decorate navigation properties you don't want the serializer to handle in a [ScriptIgnore] attribute.
By the way, you can still use ServiceStack.JsonSerializer for deserializing - it's faster than .NET's and you don't have the StackOverflowException issues I asked this question about.
The other problem is how to get the Self-Tracking Entities to decorate relevant navigation properties with [ScriptIgnore].
Explanation: Without [ScriptIgnore], serializing (using .NET Javascript serializer) will also raise an exception, about circular
references (similar to the issue that raises StackOverflowException in
ServiceStack). We need to eliminate the circularity, and this is done
using [ScriptIgnore].
So I edited the .TT file that came with ADO.NET Self-Tracking Entity Generator Template and set it to contain [ScriptIgnore] in relevant places (if someone will want the code diff, write me a comment). Some say that it's a bad practice to edit these "external", not-meant-to-be-edited files, but heck - it solves the problem, and it's the only way that doesn't force me to re-architect my whole application (use POCOs instead of STEs, use DTOs for everything etc.)
#mythz: I don't absolutely agree with your argue about using DTOs - see me comments to your answer. I really appreciate your enormous efforts building ServiceStack (all of the modules!) and making it free to use and open-source. I just encourage you to either respect [ScriptIgnore] attribute in your text serializers or come up with an attribute of yours. Else, even if one actually can use DTOs, they can't add navigation properties from a child object back to a parent one because they'll get a StackOverflowException.
I do mark your answer as "accepted" because after all, it helped me finding my way in this issue.
Be sure to Detach entity from ObjectContext before Serializing it.
I also used Newton JsonSerializer.
JsonConvert.SerializeObject(EntityObject, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
I have set of custom attributes, my logic reflects over applied attributes on specific properties. Part of the logic was trying to rely on the sequence in which the attributes are applied in he source code, however what I found after decompiling few timers that the C# compiler doesn’t put the attributes in IL in the same sequence as it appears in the source code. Any insight on this? I am not able to find any information about this in MSDN.