Wednesday, February 18, 2009

LINQ to SQL Data context class features and properties

LINQ-to-SQL is a new programming model for data access layer in managed .net environment. you may also consider it as another ORM tool if you want to.LINQ-to-SQL is a LINQ-based framework that allows users to query database using object model build using designer tools available in visual studio 2008 or using command line tool called Sqlmetal.exe.

Generated database driven object model resides in LINQ-to-SQL class file ending with a .dbml extension.All data types that implement IQueryable or its generic version IQueryable can be employed as data sources in a LINQ query.

DataContext:

Another important thing is to know in LINQ-to-SQL world is Datacontext object. Simply put, DataContext object manages the database connection and holds connection strings. It is closely associated with underlying LINQ-to-SQL domain model and provides single point entry to LINQ-to-SQL data model.

You need an instance of datacontext everytime you need to query or update database. DataContext object is light-weight and extensible in nature. Usually, what I know is to create DataContext object for every unit of work. Of course, we can also maintain a global context class for all database calls. DataContext class is also extensible using partial classes for various scenarios like returning multiple result sets.

By now, we might understand the importance of DataContext class for database operations. So, Let's peek into few properties which're worth looking into

ObjectTrackingEnabled:

As already told, DataContext is self-contained box which maintains identity and tracking information for all the entities it holds. But question is do we need the tracking or identity information all the times? Nopes.. If you're dealing with read-only data or just retrieving data for display purposes then we don't need burden of tracking information which's quite useful for editable data. So, just switchoff tracking in DataContext object. How do we do that?
Just set ObjectTrackingEnabled property of DataContext object to false. This will avoid unnecessary identity management of entities and also makes Data Context object light weight.

using (NorthwindDataContext context = new NorthwindDataContext())
{
context.ObjectTrackingEnabled = false;
}

Optimistic Concurrency Control:

LINQ to SQL comes with out of the box Optimistic concurrency support mapping a binary type for all SQL Time stamp columns in database. In some cases like low volume environments where we don't care much about concurrency control or we may employ our own custom concurrency control independent of LINQ-to-SQL optimistic concurrency control techniques, then it would be a gain if you turn off Optimistic Concurrency property in mapping file (.dbml file) which does the invalidation of insert/update data.

Set UpdateCheck property of DataContext object to Enumeration value of Never to turn optimistic concurrency off in LINQ to SQL.

Here is an example of turning optimistic concurrency off implemented as attribute level
mapping:

[Column(Storage=“_city”, DbType=“string”,
UpdateCheck=UpdateCheck.Never)]
public string city
{
get
{ return this._city; }
}

Persistance Ignorance:

LINQ-to-SQL(LTS) allows to generate POCO (Plain old CLR objects) with an acceptable level of persistence ignorance by setting the DataContext class’s Access property value to Internal.

Generated SQL queries:

Wanna see SQL statements generated by LTS? There is a possibility that you may not be aware of additional columns or extra data that is retrieved behind the scenes. Use Data Context’s Log property to be able to see what SQL statements are being run by the Data Context. Quite useful to analyze SQL statements generated by LINQ-to-SQL.

using (NorthwindDataContext context = new NorthwindDataContext())
{
context.Log = Console.Out;
}

Similarly, to avoid logging of SQL statements in production environments, set DataContext.Log to null.

DataContext.CommandTimeOut:

The default value of the DataContext class's CommandTimeout is set to 30 seconds. Any database queries taking a longer time to complete than 30 seconds will throw a Timeout Exception.

One solution to this problem is to set the value of the CommandTimeout before each time a LINQ to SQL DataContext object is created and such a query is invoked. But the problem with this approach is, it will introduce code duplication. To do it efficiently, Fortunately enough, the Visual Studio 2008 auto generated DataContext subclasses provide an easy way to achieve this target using extensibility through partial methods.

Data filtering:

When we retrieve data with Load or LoadWith methods, we assume that we want to retrieve all the associated data those are bound with the primary key. But in most cases we likely need additional filtering to this. Here is where DataLoadOptions.AssociateWith generic method comes very handy. This method takes the criteria to load the data as a parameter and applies it to the query – so you get only the data that you need.

using (NorthwindDataContext context = new NorthwindDataContext())
{
DataLoadOptions options = new DataLoadOptions();
options.AssociateWith(x=> x.streets.Where(y => y.michiganave)); context.LoadOptions = options;
}

DataContext.Refresh:

To reload entity object, Data Context object provides Refresh method to invalidate the entity if it's changed.










Saturday, February 14, 2009

UI Design Patterns: Model View Presenter(MVP) pattern and Model View Controller(MVC) pattern

Introduction to UI Design Patterns:

UI design patterns isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other.

There are a number of frameworks in use today that are based on these UI patterns including: JAVA Struts, ROR, Microsoft Smart Client Software Factory (CAB), Microsoft Web Client Software Factory, and the recently announced ASP.Net MVC framework.

Features of UI Design Patterns:

Loose coupling The presenter/controller are an intermediary channel between the UI code and the model. This allows the view and the model to evolve independently of each other.

Clear separation of concerns/responsibility

UI (Form or Page) – Responsible for rending UI elements

Presenter/controller – Responsible for reacting to UI events and interacts with the model

Model – Responsible for business behaviors and state management

Test Driven – By isolating each major component (UI, Presenter/controller, and model) it is easier to write unit tests. This is especially true when using the MVP pattern which only interacts with the view using an interface.

Code Reuse – By using a separation of concerns/responsible design approach you will see increase code reuse. This is especially true when using a full blown domain model and keeping all the business/state management logic where it belongs.

Hide Data Access – Using these patterns forces you to put the data access code where it belongs in a data access layer.

Flexibility/Adaptable – By isolating most of your code into the presenter/controller and model components your code base is more adaptable to change. A properly designed solution using MVC or MVP can support multi UI and data access technologies at the same time.

Let's see each of these design patterns in more details:

Model-View-Controller Design Pattern (MVC):

The MVC pattern is a popular UI architectural pattern used in application programming. This pattern separates responsibilities across three components

1) View is responsible for rending UI elements,
2) Controller is responsible for responding to UI actions,
3) Model is responsible for business behaviors and state management


The Model-View-Controller, on the other hand, provides limited capabilities for communication between the Controller and the View. MVC requires passing all data to the view in a single call

In most implementations, all these three components can directly interact with each other and in some implementations the controller is responsible for determining which view to display (Front Controller Pattern),

Model-View-Presenter Design Pattern (MVP):

The MVP pattern is another popular UI presentation pattern based on the concepts of the MVC pattern. The pattern also separates responsibilities but across four components as opposed to three in MVC world.

1) View is responsible for rending UI elements,
2) View interface is used to loosely couple the presenter from its view,
3) Presenter is responsible for interacting between the view/model through View Interface and optional service Interface
4) Model is responsible for business behaviors and state management.

Types of MVP's:

The MVP pattern is even broken down into two other patterns based on complexity into

  • Passive view
  • Supervising controller

to even more decide on how much authority the view has.

Passive MVP:

MVP is basically the MVC pattern, but it gives less control to the view and puts most of the responsibility in the presenter. It is an attempt to make all logic part of the presenter, thus making the view “passive”.

In passive mode, there won't be any event driven code to support view event handling. Instead, view would make a direct call to presenter. Due to the fact that presenter is indirectly manipulating the view elements and that the view is "dumb", there is no need for using any DTO objects because there is no need for presenter to pass any kind of data back to the view, because view doesn't do anything.

Benefits:

Simpler view-presenter wire up code where there is no need for event publishing in view or for event subscribing code in presenter.

Testability is increased because it is now much simpler to just call public presenter method to trigger that behavior instead of triggering it indirectly through simulation of the event occurrence

Supervising Controller MVP:

We tend to lean more towards the passive view, but understand it is very restricting. If I have to do a lot of data binding, I see that the supervising controller can be a more flexible pattern, but you lose a little bit of testability that you would get with the passive view.

ASP.NET MVC Framework:

It is important to understand that ASP.Net MVC is not a replacement for WebForms, it is just an alternative.

Advantages:

1) Built in MVC Pattern
2) Front Controller pattern (implemented with Routing). Note: Routing is not an ASP.Net MVC pattern. It is a new functionality, of .Net, that the ASP.Net MVC framework takes advantage of.
3) Mock testing - using a Mock framework like RhinoMock, the ASP.Net MVC allows us to mock up website objects such as server, context, etc… This is a great advantage because we don’t have to have a website actually running to do our testing.
4) No post backs or view state. This really makes .Net development more web-centric and really cleans up the html page that is ultimately created (because the view state is not in the webpage anymore)
5) Great for Agile development. It really forces you to create end to end segments, one at a time.
Disadvantages:
1) Controls support - Controls that take advantage of post backs or view state will not work. These include the data bound controls like gridview control and the AJAX controls.

2) Not great for design driven or waterfall development of large, business applications. Using something like an MVP pattern in a Web form will allow you to create interfaces and business layers completely before building any UI or View code. With ASP.Net MVC, you should create you views and test them from the beginning.

Important differences between MVC and MVP patterns:

MVP Pattern:

1) View is more loosely coupled to the model. The presenter is responsible for binding the model to the view.

2) Easier to unit test because interaction with the view is through an interface

3) Usually view to presenter map one to one. Complex views may have multi presenters.

4) Best testability coverage of views.

5) Decouples UI completely from Data Sources

6) MVP summarized: Navigation is handled on a per page basis using page controller pattern approach.

MVC Pattern:

1) The Model-View-Controller pattern, on the contrary, assumes the View to receive user gestures and then to delegate processing to the Controller. Such a scheme comfortably fits most ASP.NET UI libraries.(Conforms to Modern UI programming)

2) Controller has a constant link to the associated View object. This allows a Controller to access its View at any moment, either by getting or setting some View properties.

3) Direct requests from the controller to the view become possible. Thus the controller itself may trigger the view updates instead of performing a round trip through the presentation model.

4) Controller are based on behaviors and can be shared across views and are based on Front Controller pattern (responsible for determining which view to display)

MVC summarized: The navigation is handled in one centralized configurable place for all the pages based on front controller pattern.

MVC pattern bypasses the standard ASP.NET request processing scheme, because the incoming gestures in MVC are received by the Controller.

Form of user requests in the ASP.NET MVC framework (which is based on a URI pattern) makes it difficult to adapt this framework to use with the conventional ASP.NET server-side event model.(doesn’t confirm to Modern UI programming)

The MVC pattern, quite the contrary, is good mainly for web applications and hardly fits other presentations. In MVC, view is aware of Model too!!

The difference between the two is just how much work the view is allowed to do. The MVP pattern attempts to take work away from the view.

Thus, if an application needs extensive communication between Controller and View objects, MVP is more preferable than MVC.

Therefore, if an application needs or later will need support for multiple GUI platforms, MVP would be the right solution.


Wednesday, February 11, 2009

WCF Throttling Behaviors and concurrency modes

I am working in a team which is responsible for creating & maintaining enterprise WCF services which are consumed by different clients in an intranet environment. We built well architected WCF services and tested them using our own web client. Everything looked pretty for a while. We threw open the services for enterprise wide intranet clients to consume hoping they'll have the same good time as we had.

But, issues started to see light as different clients began to hit our services. Major complaint is timeout exception. After digging deep, we found is WCF service stops responding after a certain set of client messages. Initially, WCF responded well. All of a sudden, it stops responding. What's happening? Anyways, we found the issue. It's all about sessions which are created for every request. After 10th request, WCF runtime reaches threshold value for maximum number of sessions which results in Timeout exception being thrown out. Then, how can we overcome this issue? There comes to our rescue the WCF runtime throttling properties. Let's see what they're and how they're used.

In a real world, WCF services can experience many issues like Denial of Service attacks, performance problems, over consumption of resources, scalability problems and many more..

For example, Imagine a scenario, where client application starts avalanche series of requests to service using multithreading apparatus or invoking all one-way operations on service which will quickly inaundate service with client requests. How can we handle this situation?

Yes, You can use WCF runtime service throttling properties to prevent over consumption of resources. Reader quotas also come to our rescue in controlling amount of data being transferred between client and service. By engaging throttling, WCF infrastructure precisely controls the number of incoming requests so that the service will be able to handle them effectively with out becoming unavailable.

By default, the ServiceThrottle property of the ChannelDispatcher object is null and the WCF runtime uses its own default values for throttling behavior. To control scalability, you should arrange for the WCF runtime to create a ServiceThrottle object and explicitly set it's properties to values suitable for your environment, taking into account the expected number of concurrent client applications and the work that they are likely to perform.

There are three properties which can be configured under ServiceThrottlingBehavior section of configuration file. You can perform this task in code by creating a ServiceThrottlingBehavior object and setting properties.

MaxConcurrentCalls: The MaxConcurrentCalls setting specifies the number of concurrent messages the service host will accept. Default value is 16 calls.

MaxConcurrentSessions: This property specifies the number of concurrent session-aware channels the service will accept. Default value is 10 concurrent sessions. Long running sessions can cause other clients to block resulting in a time-out.

MaxConcurrentInstances: This property specifies the maximum number of concurrent service instances that the service host will allow. The default value is Int32.MaxValue. The impact this value will have depends on the instance context mode. if the instance context mode is perCall, this will be same as MaxConcurrentCalls property since each call will get it's own instance. if the instance context mode is perSession, this will be same as MaxConcurrentSessions property. If the instance context mode is single, the value of this property will always be 1
When throttling limits are reached, WCF runtime will throw an time out exception irrespective of which property limits are reached.

We can use above defined properties to manage WCF runtime behavior based on our environments and volume of client calls. Exercise caution when overriding default values for this properties for they can have a drastic effect on response times and throughput. For example, clients blocked by limits that are set too low can result in excessive time out errors occuring in client applications.

When throttling is enabled, WCF will check the current counters for each request that arrives. If counters are exceeded, WCF automatically places the request in a queue. When counters come down below the threshold, the requests are then retrieved from queue in the same order for processing.


Concurrency Modes:

Apart from this properties, We can also utilize other attributes to improve scalability of WCF runtime. One of them is Concurrency Mode. If client is running in a asynchronous mode, service can then limit the client to a single thread by setting ConcurrencyMode property of service to single. This property ensures service will process requests in a serial manner one at a time.

Setting ConcurrencyMode to multiple will enable service object to process multiple requests simultaneously.

Setting ConcurrencyMode to reentrant will allow thread to leave the service object and reenter the object at a later point in time.

WCF 3.5 Message Contracts

From practical usage stand point, I've rarely used WCF Message Contracts. My work a lot of time revolved around Data Contracts and Service Contracts. I feel using Message Contracts are limited in their usage except for passing around few custom headers. But, they might be of great help for other folks around. so, let's see what is a message contract?

Defining message contracts lets you gain control over entire SOAP message and NOT just over the structure of data in the body as case with data contracts. Message Contracts deal with accessing and supplying custom headers and lets you define your own message structure.

There's no partial usage of message contracts. After you introduce a message contract into an operation signature, you must use a message contract as the only parameter type and as return type of the operation instead of any data contract for parameters or return type.

Windows Commuication Foundation(WCF) Data Contract serialization techniques

WCF 3.5 supports two types of serialization techniques.
  • DataContractSerializer
  • XmlSerializer

DataContractSerializer Vs XMLSerializer

In contrast to predecessor XmlSerializer, DataContractSerializer serializes only members declared explicitly using DataMember attribute. XmlSerializer serializes all members of data contract except for members declared explicitly using NotSerialized attribute.

WCF Serialization Caveats:

.Net Built-in collection types and custom collections implementing IEnumerable or IEnumerable interfaces are represented as Arrays type when serialized across wire which means they appear as arrays to the clients.

To serialize a custom collection defined in data contract, few things we need to understand are:

  • Collection in the contract must be a concrete collection(not an interface) and is serializable
  • Collection should contain an Add operation

if we satisfy above requirements, WCF automatically serialize the collection as an array of collection's type.

If your collection doesn't meet above requirements, there's a still way to serialize the collections using CollectionDataContractAttribute. You can use this attribute to annotate your custom collections and resulting collections will always be exposed as List collection to it's WCF consumers.

As mentioned in my earlier post, Data Contracts doesn't support inheritance hierarchy. we have to explicitly annotate derived classes with DataContract attribute. Let's know something more about another important attribute in this context.

KnownTypeAttribute:

For instance, we have a derived class called car derived from base class called vehicle. When you pass in a car object instead of vehicle object, the service doesn't know how to deserialize the car object it received, because it doesn't know about car object.

Much the same way, when a car object is returned instead of a vehicle, the client doesn't know how to deserialize it, because all it about are vehicles but not cars.

The solution is to explicitly tell WCF about the car class using the KnownTypeAttribute attribute. The KnownType attribute allows you to designate sub classes for the data contract of base class.

Polymorphism is outside the paradigm of Service orientation. So, KnownTypeAttribute is required to specify derived class relationship with base class for DataContract Serializer.

We've to annotate base class with KnownTypeAttribute for all those derived classes being utilized in the data contract.

Tuesday, February 10, 2009

WCF Data contracts

Before we understand Windows Communication Foundation (WCF) Data contracts, let's first set the context of data representation and various forms of it. Dimensions of information exchange is changed drastically these days. Ok, when two parties communicate or exchange data electronically, how do they manage communication? They usually define common protocols, standards or agreements over data format or representation before they actually exchange data with each other.

That said, you may ask what are those different standards or representations. I can think for now in XML world what we call is XSD (XML Schema Definition) language. Using XSD, we define structure of data, types of data and format of the data. You may also recall DOM (Document Object Model) mechanism which is also used to represent structure.

In Service Oriented Architecture (SOA) paradigm, loosely coupled services play a major role. Consumers request services for data using standardized SOAP messages or you also invoke services using latest REST based protocol. In this request/response exchange, data needs to be represented in proper format for both client and server to interact properly.

Windows communication Foundation (WCF) defines some thing called Data Contracts. Data contracts define the structure of payload data in soap message exchanged. Data contracts enable serialization of complex types included in request /response messages.

How do we define Data Contracts?

  • Datacontract attribute is declared in System.Runtime.Serialization namespace in .Net Framework.
  • This attribute can be applied to structs, enums and classes.
  • Data contracts are not inherited. Any inherited data contract must have attribute specified explicitly
  • By Default, DataContractSerializer performs serialization of data contracts to enable transmission of data contracts between client & server.

What is Data Member?

DataMember attribute is declared in the same namespace as DataContract attribute. We can apply this attribute to members in data contract to indicate they should be included in serialization.

It supports two attributes:

IsRequired: Indicates whether a member is optional. Default value is false. this attribute is very useful in Data contract versioning scenarios. For instance, your service expects a new version of data contract in which new fields are added. The client is not aware of these changes. Clients send his request using old data contract which doesn't break at the service. Service successfully processes his request. This is happened because service is not expecting values for new fields from client since IsRequired is set to false by default on new fields unless you explicitly set them to required.

EmitDefaultValue: Controls whether default values will be included if no values are supplied for the members. Default value for this attribute is true which means all data members are serialized. If attribute is set to false, any member with out any valid data value or with any default value ( Ex: null value for reference types) will not be serialized.

These two properties work in tandem. Like, Having EmitDefaultValue property set to false and setting IsRequired to true can cause problems if you don't provide any valid value for member.

Just apply Datamember attribute to only properties if field is associated with the property.

Ordering precedence for elements in DataContracts:

If datacontract is part of inheritance hierarchy, then members of base type are listed first followed by current type members sorted alphabetically. Next are any members that have the order property set.

EnumMember Attribute:

Enumerations still should be defined using Data contract attribute as mentioned earlier. Only enumeration member should be only decorated with EnumMember attribute.

EnumMember attribute is used to declare that given member of enumeration declared with a data contract attribute should be considered as part of data contract. Only property for this attribute is Value

We'll discuss more about DataContractSerializer and XML Serializer which are used to serialize data contracts in our next post.

Hope this post is useful to you.

Sunday, February 8, 2009

Useful Visual Studio 2008 keyboard shortcuts

Guys, Do you regularly or occasionally use keyboard shortcuts while writing programs esp. when formatting, invoking menu options, invoking different windows and explorers in Visual Studio.

If you say yes, then I can say definitely you're a productive guy. If you rarely use keyboard shortcuts, then I can say your productivity suffers!! Time to wake up !!

Reduce your dependency on mouse. Get your hold on keyboard. You'll then definitely realize your increase in productivity and shows off your expertise with your co-workers.

Visual Studio environment offers many powerful features and options aimed to improve developer's productivity. I'll share few ubiquitous,important and quite unknown shortcuts/commands with you in this post.

Debug/Build commands:


  1. Build operation : Shift+Ctrl+B or F6
  2. Selective Build of any single project in the solution : Shift + F6
  3. Quick Watch window: Ctrl + D, Q
  4. Immediate Window: Ctrl + D, I
  5. Autos Window: Ctrl + D, A
  6. Callstack Window: Ctrl + D, C
  7. Start with Debugging: F5
  8. Start with out Debugging: Ctrl + F5
  9. Toggle Breakpoint: F9
  10. Enable/Disable Breakpoint: Ctrl+F9
  11. Debugging: Step into : F11
  12. Debugging: Step over : F10
  13. Debugging: Step out: Shift + F11

Visual Studio Window commands:

  1. ErrorList Window: Ctrl + W,E
  2. Output Window: Ctrl + W, O
  3. Solution Explorer Window: Ctrl + W, S
  4. Toolbox Window: Ctrl + W, X
  5. Command Window: Ctrl + W, A
  6. Code Definition Window: Ctrl + W, D or F12
  7. Server Explorer Window: Ctrl + W, L
  8. Properties Windows : F4 or Ctrl + W, P
  9. To close current active window in Visual studio: SHIFT + ESC
  10. To close current active tab in Visual Studio: Ctrl + F4
  11. To move between tabs/document windows in Visual Studio : Ctrl + TAB

Navigation shortcuts:

  1. GoTo a specific line number: Ctrl + G
  2. Find something among lines : Ctrl + F
  3. GoTo next matching item or next item in the search results: F8
  4. GoTo matching opening or closing brace in a large method: Ctrl + ]
  5. Navigate backward to previously browsed line of code: Ctrl + MINUS (-)
  6. Navigate forward to next browsed line of code: Ctrl + Shift + MINUS (-)
  7. Navigate to dropdown located at top of code editor : Ctrl + F2

Editing:


  1. Replace command: Ctrl + H
  2. Comments selected lines of code: Ctrl + K, C
  3. Uncomments selected lines of code: Ctrl + K, U
  4. Formats selected lines of code : Ctrl + K , F
  5. Formats entire code document: Ctrl + K, D
  6. Replace in files : Ctrl + Shift + H
  7. Insert snippet: Ctrl + K, X
  8. Quick Info about piece of code : Ctrl + K, I
  9. Parameter info of specific method: Ctrl + K, P
  10. List members of specific object: Ctrl + K, L
Hope this post might help in saving your time and improving productivity levels.

Changing Authentication mode in SQL Server 2005

SQL Server authentication mode is usually configured during SQL Server installation. By default, Windows Authentication mode is configured. Ok, What if we want to override the mode configured during installation.

As you may know, SQL Server 2000/2005 supports three authentication modes:

1) Windows Authentication Mode
2) SQL Authentication Mode
3) Mixed Mode Authentication (SQL/Windows)

Here're following steps to switch mode

In SQL Server Management Studio Object Explorer, right-click your server, and then click Properties.

On the Security page, under Server authentication, select the new server authentication mode, and then click OK.

Make sure, you restart the server to effect the changes.

You can restart the server from SQL Server Configuration Manager.