Tuesday, November 22, 2016

Performance issues with Entity Framework -

Hi Folks,

This has been a burning question and issue with almost all .Net developers -

I also faced this and researched for a long time and have read so many blogs and posts about this issue stating that entity framework is a bottle neck in terms of performance.

Yes, that's absolutely correct if you don't use it properly and without any vision of your DB size and the operations which you would need to perform on the data.

Few important tips -

# Use DBContext wisely as this can completely blow off your application performance.

# DO NOT select all columns and fetch only required columns from tables

# Use relationships intelligently

# Use lazy loading and eager loading wherever required per situation

# Ignore costly queries which fetches too many rows from DB, instead use Stored Procedures if you can

# Identify slow running queries using EF Profiler, this is a very good tool to get the details

# Verify that DB connections are properly closed and disposed

# Verify all DB objects including parameters are being disposed properly

# Use "Using" while using DB connections

# Don't span DBContext for a long time

# Use LINQ properly

# Make use of joins

# Use data filters and fetch limited rows from DB itself rather than filtering them on client side

# Use DB side paging while fetching and displaying large row set

# Don't hesitate using stored procedure wherever needed

# Use views if you have large data in tables

# Tools which be used to fine tune your EF queries - EF profiler, Server dumps, Query debugger, Performance monitors, VS Performance analyzer

# Continuously perform load and performance testing of your application rather than doing it at the end of the project

Happy programming

Should I use ADO.Net Or Entity Framework

Hi Folks,

This has been a topic of many debates, should I use ADO.Net OR Entity Framework?

It really depends on many factors -

1) Performance
2) Speed of Development
3) Neat/Maintainable code
4) Flexibility
5) Learning curve

You gotta select your preferences, lets see these one by one -

First off, if you're starting a new project, go with Entity Framework ("EF") - it now generates much better SQL (more like Linq to SQL does) and is easier to maintain and more powerful than Linq to SQL ("L2S"). I consider Linq to SQL to be an obsolete technology. MS has been very open about not continuing L2S development further.

1) Performance

This is tricky to answer. For most single-entity operations (CRUD) you will find just about equivalent performance with all three technologies. You do have to know how EF and Linq to SQL work in order to use them to their fullest. For high-volume operations like polling queries, you may want to have EF/L2S "compile" your entity query such that the framework doesn't have to constantly regenerate the SQL, or you can run into scalability issues. (see edits)

For bulk updates where you're updating massive amounts of data, raw SQL or a stored procedure will always perform better than an ORM solution because you don't have to marshal the data over the wire to the ORM to perform updates.

2) Speed of Development
In most scenarios, EF will blow away naked SQL/stored procs when it comes to speed of development. The EF designer can update your model from your database as it changes (upon request), so you don't run into synchronization issues between your object code and your database code. The only time I would not consider using an ORM is when you're doing a reporting/dashboard type application where you aren't doing any updating, or when you're creating an application just to do raw data maintenance operations on a database.

3) Neat/Maintainable code

Hands down, EF beats SQL/sprocs. Because your relationships are modeled, joins in your code are relatively infrequent. The relationships of the entities are almost self-evident to the reader for most queries. Nothing is worse than having to go from tier to tier debugging or through multiple SQL/middle tier in order to understand what's actually happening to your data. EF brings your data model into your code in a very powerful way.

4) Flexibility

Stored procs and raw SQL are more "flexible". You can leverage sprocs and SQL to generate faster queries for the odd specific case, and you can leverage native DB functionality easier than you can with and ORM.

5) Learning curve

If you are an ADO.Net developer and ready to learn new techniques then EF would make your life easier by reducing the amount of code you write. If you are a beginner then definitely you can start your learning from EF since this is the present and future.

5) Overall

Don't get caught up in the false dichotomy of choosing an ORM vs using stored procedures. You can use both in the same application, and you probably should. Big bulk operations should go in stored procedures or SQL (which can actually be called by the EF), and EF should be used for your CRUD operations and most of your middle-tier's needs. Perhaps you'd choose to use SQL for writing your reports. I guess the moral of the story is the same as it's always been. Use the right tool for the job. But the skinny of it is, EF is very good nowadays. Spend some real time reading and understanding it in depth and you can create some amazing, high-performance apps with ease.

EF 5 simplifies this part a bit with auto-compiled LINQ Queries, but for real high volume stuff, you'll definitely need to test and analyze what fits best for you in the real world.

Wednesday, October 22, 2014

VPN client failed to enable virtual adapter

This issue got me nuts but was able to resolve it by following solution -

Just to update, the legacy Cisco VPN client (5.0.07.0440 for x64, 5.0.07.0410 for x86) is working for some people. You need to apply a small workaround as explained below –

·    Open Registry editor by typingregedit in Run prompt
·    Browse to the Registry Key HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\CVirtA

·    Select the DisplayName to modify, and remove the leading characters from the value data upto "%;" i.e.

o    For x86, change the value data from something like "@oem8.inf,%CVirtA_Desc%;Cisco Systems VPN Adapter” to "Cisco Systems VPN Adapter”

o    For x64, change the value data from something like "@oem8.inf,%CVirtA_Desc%;Cisco Systems VPN Adapter for 64-bit Windows” to "Cisco Systems VPN Adapter for 64-bit Windows”
·    Try connecting again

BINGO

Wednesday, November 20, 2013

Working with Sencha EXTJS and WCF Service (Fetch Data)

JavaScript frameworks are the next future in application development. They provide a very effective, fast and modern way to develop and maintain the web application which gives it a desktop application look and feel.

Sencha EXTJS is one of the many available such frameworks.

I'm into MS technologies mainly ASP.NET.
Following is a basic example of how to connect to WCF services using EXTJS into ASP.NET application.

On WCF service project -

Create a WCF service which returns a list of users to the consumer (If you are looking for the EXTJS with WCF solution then I think I dont need to tel how to create a WCF service :))

The most important thing to do at WCF side -

Add following code in the global.asax of WCF solution -

  protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
            {
                HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, x-requested-with");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }

This code is required to handle request with uri having additional params.

The Store   
Ext.define('My.store.Users', {
    extend: 'Ext.data.Store',
    model: 'AM.model.User',
    autoLoad: true,
    proxy: {
        type: 'ajax',       
            url: 'http://localhost/EXTJSService/RestService.svc/getUsers',
        reader: {
            type: 'json',
            root: 'GetUsers'
        }
    }
});

Here url is the path of your WCF service, in this example its hosted on the same machine, localhost.

root is the root element if the resultant JSON data (in most of the examples you will find it "d:")

View -
Ext.define('My.view.user.List', {
    extend: 'Ext.grid.Panel',
    alias: 'widget.userlist',
    title: 'All Users',
    store: 'Users',
    initComponent: function () {
        this.columns = [
            { header: 'ID', dataIndex: 'ID', flex: 1 },
            { header: 'Name', dataIndex: 'FirstName', flex: 1 },
            { header: 'Email', dataIndex: 'Email', flex: 1 }
        ];
        this.callParent(arguments);
    }
});

Important things to note is the mapping of fields in the grid, these should map to the field names in the JSON data.

Hope you find it useful.
Enjoy....

Thursday, December 6, 2012

YAGNI Principle - An Xtreme Programming approach

"You ain't gonna need it" or “You aren't gonna need it”(acronym: YAGNI) is a principle of extreme programming (XP) that states that a programmer should not add functionality until it is deemed necessary.

"Always implement things when you actually need them, never when you just foresee that you need them."

YAGNI is a principle behind the XP practice of "doing the simplest thing that could possibly work". It is meant to be used in combination with several other practices, such as continuous refactoring, continuous automated unit testing and continuous integration.

Used without continuous refactoring it would lead to messy code and massive rework. Continuous refactoring in turn relies on automated unit tests as a safety net and continuous integration to prevent integration problems.

YAGNI is not universally accepted as a valid principle, even in combination with the supporting practices. The need for combining it with the supporting practices, rather than using it standalone, is part of the original definition of XP.

It was never claimed that standalone YAGNI was a good idea.

According to those who advocate the YAGNI approach, not following YAGNI may lead to following disadvantages:

 The time spent is taken from adding, testing or improving necessary functionality.

The new features must be debugged, documented, and supported.
 Any new feature imposes constraints on what can be done in the future, so an unnecessary feature may preclude needed features from being added in the future.
 Until the feature is actually needed, it is difficult to fully define what it should do and to test it. If the new feature is not properly defined and tested, it may not work correctly, even if it eventually is needed.
 It leads to code bloat; the software becomes larger and more complicated.
 Unless there are specifications and some kind of revision control, the feature may not be known to programmers who could make use of it.
 Adding the new feature may suggest other new features. If these new features are implemented as well, this may result in a snowball effect towards feature creep.

Difference between Passimistic and Optimistic locking

Optimistic Locking is a strategy where you read a record, take note of a version number and check that the version hasn't changed before you write the record back.

When you write the record back you filter the update on the version to make sure it's atomic. (i.e. hasn't been updated between when you check the version and write the record to the disk) and update the version in one hit.
If the record is dirty (i.e. different version to yours) you abort the transaction and the user can re-start it.

This strategy is most applicable to high-volume systems and three-tier architectures where you do not necessarily maintain a connection to the database for your session. In this situation the client cannot actually maintain database locks as the connections are taken from a pool and you may not be using the same connection from one access to the next.


Pessimistic Locking is when you lock the record for your exclusive use until you have finished with it. It has much better integrity than optimistic locking but requires you to be careful with your application design to avoid Deadlocks.

To use pessimistic locking you need either a direct connection to the database (as would typically be the case in a two tier client server application) or an externally available transaction ID that can be used independently of the connection.

In the latter case you open the transaction with the TxID and then reconnect using that ID. The DBMS maintains the locks and allows you to pick the session back up through the TxID.

Thursday, August 30, 2012

WCF Interview Question


What are the various ways of hosting a WCF service?

How do we host a WCF service in IIS?

What are the advantages of hosting WCF Services in IIS as compared to self-hosting?

What are the major differences between services and Web services?

What is the difference WCF and Web services?

What is one-way operation?

Can you explain duplex contracts in WCF?

How can we host a service on two different protocols on a single server?

Can you explain transactions in WCF?

What different transaction isolation levels provided in WCF?

What is a poison message?

What is service and client in perspective of data communication?

What is address in WCF and how many types of transport schemas are there in WCF?

What are the important principles of SOA (Service oriented Architecture)?

What are ends, contract, address, and bindings?

Which specifications does WCF follow?

What are the main components of WCF?

Explain how Ends, Contract, Address, and Bindings are done in WCF?

What is a service class?

What is a service contract, operation contract and Data Contract?

How to define a service as REST based service in WCF?

What is the address formats of the WCF transport schemas?

What is Proxy and how to generate proxy for WCF Services?

What are different elements of WCF services Client configuration file?

What is Transport and Message Reliability?

How to configure Reliability while communicating with WCF Services?
 
What are meta MAX attributes in WCF?
 
How to return an exception from WCF to the client?
 
What is the use of Message Inspector in WCF?