Wednesday, October 19, 2011

[C# and Language] Compiler as a Service (CaaS)
Roslyn CTP released

Microsoft announces the release of its Roslyn CTP, the first example of the implementation of CaaS (Compiler as a Service) that I presented some time ago. This release marks a significant step to how Microsoft thinks of compilers. The C# and VB compilers are no longer black boxes.  All that rich information about code is now exposed as an object model that can be easily consumed. In addition, Microsoft has released a preview of the first-ever Interactive window for C# that contains full IDE support, including IntelliSense and even automatically detecting missing using directives.

You can download it here:
http://msdn.com/roslyn

image


Share/Save/Bookmark

Monday, October 17, 2011

[Tutorial] Code First with Entity Framework 4.1
Part5: Concurrency Management & Conclusion

Assuring the consistence and coherence of data in the database is a very important topic. Concurrency problems come up when multiple source (clients, systems, etc…) try to modify data at the same time.Your application must be able to choose whether to accept or reject any data changes that arrive simultaneously.

Simultaneous execution of transactions must provide the same results as sequential execution of those same transactions.If you achieve this goal then your system is well prepared for correct concurrency handling.

Entity Framework 4.1 includes a very complete concurrency approach. The DbContext class that we already saw before allows for efficient concurrency handling.

First Wins

This resolution strategy defines that the first modification wins if there are concurrency conflicts. All following modifications on the same data are ignored and clients get exceptions that tell them that their transactions did not complete because the data was already changed.

You have to determine which properties might be subject to concurrency problems. You may have properties which do not need any concurrency handling. A property that contains the last modified date might not need to implement any concurrency handling for example.

All properties that must be checked for concurrency need to have the [ConcurrencyCheck] attribute set as shown below:

image

If you don't like using attributes, you may also use the CodeFirst Fluent API on your properties to activate concurrency checking:


image

You have to add concurrency checks only on properties that really need it since this will have some impact on performance within your code. The more properties with activated concurrency checks you have the worse your performance might be depending on your implementation !

image

In the example above we retrieve a manager object from the database, store it in memory and change some property values (but only in memory). We then change some values on the same data directly in the database to simulate concurrency as if two clients did modifications at the same time.

When we call the SaveChanges() method you will see that we get an exception of type DbUpdateConcurrencyException. This is where you need to add your custom concurrency conflict resolution logic. In this example we decided to guard the first modification (First Wins). In the last step we update the context in memory with the data that was changed in the database via ex.GetEntry(context).Reload().

Last Wins

This is the default resolution strategy of EF 4.1 in case of concurrency conflicts. It defines that the last modification wins if there are concurrency conflicts. All previous modifications on the same data are overwritten. Clients only get informed if they use the [ConcurrencyCheck] attribute, some kind of log, some event or other treatment.

image

The example above shows how to implement a Last Wins approach. If there are exceptions due to concurrency conflicts we store the new initial value in the context thus synchronizing with the source. We then retry saving the data until it is saved to the database and the processing is successfully completed.

Conclusion

I hope this tutorial gave you a first insight on the new features of EF 4.1 which are quite powerful even for code purists. With the new features presented in this tutorial EF becomes one of the most complete products on the ORM market. Today, it is the only product that proposes all 3 approaches
CodeFirst, Database First and Model First at the same time.

Some features are missing like auto-synchronization of the database if the class structure changes and stored procedure integration but they will be added in the next weeks / months.

Share/Save/Bookmark

[.NET 4.5] In-Place Upgrade to .NET 4.0 SP1

After the BUILD conference we now have more information on how Microsoft plans to do the upgrade from .NET 4.0 SP1 to .NET 4.5 and what this upgrade will imply.

Contrary to all other upgrades in the last years this will not be a side-by side upgrade but instead an in-place update. The .NET 4.0 SP1 framework will be replaced on the system during the upgrade process. Furthermore it seems that you won’t be able to go back once you have done the upgrade.

To allow such an update Microsoft will have to assure that old code that ran under .NET 4.0 SP1 will still run correctly under .NET 4.5 which is not an easy task. But the Microsoft staff is currently working hard to assure everything will work seamlessly when the final version of .NET 4.5 is shipping.

I will post a migration tutorial when the final version will be published and try to help you in having a good migration strategy.


Share/Save/Bookmark