Friday, September 9, 2011

Create Parallel Task in State Machine Workflow in SharePoint2010

I faced a problem while creating parallel tasks in state machine workflow. My requirement is same as OUT of Box SharePoint approval workflow which allows multiple users to approve the document and when everyone approves the document, document moves to next state which is approved.

All the articles pointed me that this is possible in sequential workflow.

Here is the thread which explains the problem, I would no be investing much time to explain the problem

I downloaded the code sample available at Microsoft site which is basic state machine workflow. Following is the link.

Modifications to the code are as follows:
  • Add a new state at level 2.
  • In the state initialization, create two tasks with different Correlation token, task ids and task properties.
  • Now add two event driven activity.
  • Keep flags for each parallel task
  • Added OnTaskChanged event driven activity, On Invoked event updated the associated flag to true.
  • Put an IF block and check if all the flags are true, then set state workflow to next state.

I found this easy to implement, though manual code is involved which should be done via a parallel activity.

Code can be downloaded from here.

Monday, June 29, 2009

N Tier Architecture

Some Myths

People normally think if they have created UI (ASP.Net application project), BL (.Net Class Library Project), & DAL data access layer (.Net Class Library Project) and referencing these projects in their web project, they are following 3 tier architecture. I think creating a n tier application is more than this.

What is tier?

According to me tier means a stand-alone process which has some code to process. In the above scenario where ASP.Net Web Application is referencing BL and DAL, all the code is running in w3wp (server OS)/ aspnet_wp process which is known as worker process. So in real terms all this is single tier.

Then question arises that, is it 1 tier application? If you are not dealing with databases or any other thing which has its own process then yes it is 1-tier architecture. Database makes it 2-tier architecture. As all the databases are run under some process. sqlservr is the process for SQL databases.

Some people also say that a web application will always run under a browser and why don't treat that as a tier and say this as 3 tier architecture?

I would say a tier should contain some meat in it. Meat means, some code which processes some part of the request handling, it should minimize the effort of request processing. Now what all can be processed at browser? To process anything on browser one has to write java script or any other client script.

Browser can do validation, however every one should be agree that all the validation which we do on client should be done on server also. So browser do not minimize any processing here.

Browser can also enhance the UI which is instant action. That's a good help in enhancing the UI, now logically it is a part of UI layer which is been run on browser instead of server just to improve the user experience it is not helping in request processing.

What makes 3 tier architecture

One has to introduce one more process somewhere. We can host the Business Logic as a WCF service / Remoting. WCF service should be hosted in an application pool other than the web application pool. This is just to make sure that both the application will be run in different worker process.

In this way we would have 3 different processes 1. Web Application Process, 2. BL WCF process, and 3 would be the database process.

Why to make 3-tier

Going back to the starting why did you want your application to be 3-tier. Answer to this question contains many big words like Performance Optimization, Scalability, Robustness, Code Re-Usability, and many more.

Compare this with following question:

2 person to do your job vs 3 person to do your job, you will get all your answers. 3 men can do the task more rapidly, correct? Yes if task can be broken into sub tasks. Same applies here if you can divide your task in sub tasks then 3 tier architecture will be beneficial.

Some people take the following approach in writing the code:

Lets take an example when one is creating a inventory application, now they write a method in DAL PlaceOrder (which takes many arguments like Customer, collection of products, PersonBillingPlan etc), this method will call a SP sp_PlaceOrder and all the logic will be written in that SP. This DAL method will simply be called in BL and will return success / failure. Now even if we create WCF service and force it to run in different process, we won't achieve what we intended to while following 3-tier architecture. This is because we do not have any meat (code to execute in BL), hence all the load is on the database. Database has to perform all the operation of validating data and applying business rules. Now in a team of 2 people if one is over burdened then will my team give the right productivity as expected? I guess no... what do you say please let me know in the comments section.

One more interesting question arises here while following true 3-tier architecture

People say, if we have to write business logic in .Net code then we have to fetch some extra data from Sql to .Net and write code on them, where in SP approach everything was coded in SQL and no data was travelling from SQL to .Net and finally a flag was sent from the DB which signifies success or failure. Now to counter attack this question I would say following things:

1. Data is anyway fetched from database, select query will be hit in both the cases only thing is data will be travelled from sql to .Net. I guess this would not harm that much but depends.

2. Data caching: data caching is way to minimize the above damage. One can easily write code to use data caching at DAL and save the effort of fetching common/static data. If data will be cached then it would really enhance the performance of the application by leaps and bounds.

I hope this would help you writing 3-tier application.

Tuesday, March 24, 2009

Client Storage in SilverLight

Client Storage for Silverlight
Silverlight provides two classes, one is IsolatedStorage and another is DataContractSerializer.

Silverlight provides IsolatedStorage class to store data at client machine. With isolated storage, data is always isolated by user and by assembly. Credentials such as the origin or the strong name of the assembly determine assembly identity. Data can also be isolated by application domain, using similar credentials.

DataContractSerializer class, which is used to serialize and deserialize instances of a type into an XML stream or document. For example, you can create a type named Person with properties that contain essential data, such as a name and address. You can then create and manipulate an instance of the Person class and write all of its property values in an XML document for later retrieval, or in an XML stream for immediate transport. Most important, the DataContractSerializer is used to serialize and deserialize data sent in Silverlight version 2 messages. Apply the DataContractAttribute attribute to classes, and the DataMemberAttribute attribute to class members to specify properties and fields that are serialized.

Problem Statement

To make use of IsolatedStorage developer has to create file stream and save the data. Ideally in real world all data will be kept in memory as an object of some type. Now to store this object in IsolatedStorage one has to first deserialize it and store it in files and define file names for them. Later this data will be read from the files and will be kept in memory again in the form of an object.

To simplify the above task I have combined the funcationality IsolatedStorage and DataContractSerializer and created ClientStorage class which helps user to store any object directly in the IsolatedStorage i.e. making object persistent an interesting feature of OOPs. Persistence is a feature of OOPs then to achieve this one must follow the oops approach rather than writing same code again and again for different objects this piece of code can be clubbed together and I have named it as ClientStorage.

What ClientStorage does:
The approach is simple and following the habit of using Session and ViewState. When we use Session to store any object in, developer does not bother where this information will be kept. She keep her objects in session when she wanted them to be available through out the life time of user's session. Same applies to IsolatedStorage, when developer wants to store something per user/application wide data on client machine that would be available cross browser to the application independent of session like persistent for always until user deletes them manually. Then why to complex things, why not something like ClientStorage[keyname] = object; and its all done.

ClientStorage is a singleton class that is basically a indexed collection which helps in fetching the objects on the basis of key. It automatically serializes the object and also stores the type of the object, so that it can be deserialize later. Gives Index based object fetching mechanism which makes life easier.

Next, it can be improved/enhanced further to provide versioning support for the objects stored in ClientStorage so that it helps in identifying the stale objects in the ClientStorage and allows to re-fetch/re-creation of that object.

I know this can be improved further, any suggestion or feedback is highly appreciable.
Source Code of Client Storage can be found here