h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Abstract -- C# Keywords

Abstract Classes and Abstract Methods in C#.NET

Abstract Classes:

  •   Provide default functionality to its sub classes.
  •   Contain methods with complete implementation, besides abstract methods.
  •   Contains at least one abstract method, then the class must be declared as abstract class.


//Abstract class
    abstract class <Class Name>
    {

        protected float <Var Name>;

        //Abstract methods can have only declarations
        public abstract <DataType> <Method Name>();
         ///Method with Paramenter
        public abstract <DataType> <Method Name>(<DataType> obj);

    }

class  <Class Name(child Class)> : <Class Name(Parent Name)>
{
        public Override <DataType> <Method Name>();
{
/// Write your Code
}

        public Override <DataType> <Method Name>(<DataType> obj);
{
/// Write your Code
}

}

Tips to improve Entity Framework Performance

Tips to improve Entity Framework Performance


  • Avoid to put all the DB Objects into One Single Entity Model
  • Disable change tracking for entity if not needed
  • Use Pre-Generating Views to reduce response time for first request
  • Avoid fetching all the fields if not required
  • Choose appropriate Collection for data manipulation
  • Use Compiled Query wherever needed
  • Retrieve only required number of records
  • Avoid using Contains
  • Avoid using Views
  • Debug and Optimize LINQ Query

ADO.NET Entity Framework Version

ADO.NET Entity Framework Version

Version : 3.5
  This release provided basic O/RM support using the Database first development.
Version: 4.0

  • Model-first development
  • POCO support
  • Lazy Loading
  • T4 Code Generation

Version :4.1

  • Code First development
  • Introduced DbContext API
  • Data Annotations and Fluent API Validation

Version: 4.2
The EF 4.2 release included the bug fixes to EF 4.1
Version: 4.3

  • Code First Migrations
  • Automatic Migrations

Version 5.0

  • Enum Support in Code First and EF Designer
  • Spatial Data Types in Code First and EF Designer
  • Table-Valued Functions
  • Multiple Diagrams per Model

Version 6.0:

  • Async Query and Save
  • Code-Based Configuration
  • Dependency Resolution
  • Interception/SQL logging
  • Improved Connection Management
  • Improved Transaction Support


Difference between Lazy Loading and Eager Loading

Difference between Lazy Loading and Eager Loading

Lazy/Deferred Loading
       In case of lazy loading, related objects (child objects) are not loaded automatically with its parent object until they are requested.
       By default LINQ supports lazy loading.

Eager loading

      In case of eager loading, related objects (child objects) are loaded automatically with its parent object. To use Eager loading you need to use Include() method.

Type of Inheritance in Entity Framework

Type of Inheritance in Entity Framework

1) Table-Per-Hierarchy(TPH):

The TPH inheritance states that all entities, in a hierarchy of entities, are mapped to a single table in storage schema. It means, there is only one table in database and different Entity types in Entity model
public class Employee
{
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
}
public class EmployeeDetails : Employee
{
 public string EmployeeAddress { get; set; }
 public string EmployeeMobile { get; set; }
}
public class EmployeeLogin : Employee
{
 public string UserName { get; set; }
 public string Password { get; set; }
}
Entity Framework Code First Mapping for TPH
modelBuilder.Entity<Employee>()
 .Map<EmployeeDetails>(m => m.Requires("Type").HasValue("EmployeeDetails"))
 .Map<EmployeeLogin>(m => m.Requires("Type").HasValue("EmployeeLogin"));

2) Table-per-Type (TPT)
        The TPT inheritance states that each entity in the hierarchy of entities is mapped to a separate table in storage schema. It means, there is separate table in database to maintain data for each Entity Type.
public class Employee
{
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
}
public class EmployeeDetails : Employee
{
 public string EmployeeAddress { get; set; }
 public string EmployeeMobile { get; set; }
}
public class EmployeeLogin : Employee
{
 public string UserName { get; set; }
 public string Password { get; set; }
}
Entity Framework Code First Mapping for TPT
modelBuilder.Entity<Employee>().ToTable("Employee");
modelBuilder.Entity<EmployeeLogin>().ToTable("EmployeeLogin");
modelBuilder.Entity<EmployeeDetails>().ToTable("EmployeeDetails");
Table-per-Concrete-Type (TPC)
          The TPC inheritance states that each derived entity (not base entity) in the hierarchy of entities is mapped to a separate table in storage schema.
          It means, there is separate table in database to maintain data for each derived entity type.
          This inheritance occurs when we have two tables with overlapping fields in the database like as a table and its history table that has all historical data which is transferred from the main table to it.
public abstract class Employee
{
 public int EmployeeID { get; set; }
 public string EmployeeName { get; set; }
}
public class EmployeeDetails : Employee
{
 public string EmployeeAddress { get; set; }
 public string EmployeeMobile { get; set; }
}
public class EmployeeLogin : Employee
{
 public string UserName { get; set; }
 public string Password { get; set; }
}
Entity Framework Code First Mapping for TPC
modelBuilder.Entity<EmployeeDetails>().Map(m =>
{
 m.MapInheritedProperties();
 m.ToTable("EmployeeDetails");
});
modelBuilder.Entity<EmployeeLogin>().Map(m =>
{
 m.MapInheritedProperties();
 m.ToTable("EmployeeLogin");
});

XAML

Introduction to XAML
XAML stands for Extensible Application Markup Language.
Its a simple language based on XML to create and initialize .NET objects with hierarchical relations.
All classes in WPF have parameterless constructors and make excessive usage of properties.
Advantages of XAML
All you can do in XAML can also be done in code.
XAML is just another way to create and initialize objects.
You can use WPF without using XAML.
It's up to you if you want to declare it in XAML or write it in code.

Declare your UI in XAML has some advantages:
XAML code is short and clear to read
Separation of designer code and logic
Graphical design tools like Expression Blend require XAML as source.
The separation of XAML and UI logic allows it to clearly separate the roles of designer and developer.

Properties as Elements
Properties are normally written inline as known from XML <Button Content="OK" />.
we can use the property element syntax. This allows us to extract the property as an own child element.
Syntax:
<Button>
  <Button.Content>
     <Image Source="Images/OK.png" Width="50" Height="50" />
  </Button.Content>
</Button>

Implicit Type conversion
A very powerful construct of WPF are implicit type converters.
They do their work silently in the background. When you declare a BorderBrush, the word "Blue" is only a string.
The implicit BrushConverter makes a System.Windows.Media.Brushes.Blue out of it.
The same regards to the border thickness that is beeing converted implicit into a Thickness object.
WPF includes a lot of type converters for built-in classes, but you can also write type converters for your own classses.
Syntax:
<Border BorderBrush="Blue" BorderThickness="0,10">
</Border>

Markup Extensions
Markup extensions are dynamic placeholders for attribute values in XAML.
They resolve the value of a property at runtime.
Markup extensions are surrouded by curly braces
(Example: Background="{StaticResource NormalBackgroundBrush}").
WPF has some built-in markup extensions, but you can write your own, by deriving from MarkupExtension.
These are the built-in markup extensions:
Binding
To bind the values of two properties together.
StaticResource
One time lookup of a resource entry
DynamicResource
Auto updating lookup of a resource entry
TemplateBinding
To bind a property of a control template to a dependency property of the control
x:Static
Resolve the value of a static property.
x:Null
Return null
Syntax
<TextBox x:Name="textBox"/>
<Label Content="{Binding Text, ElementName=textBox}"/>

Namespaces
At the beginning of every XAML file you need to include two namespaces.
The first is http://schemas.microsoft.com/winfx/2006/xaml/presentation. It is mapped to all wpf controls in System.Windows.Controls.
The second is http://schemas.microsoft.com/winfx/2006/xaml it is mapped to System.Windows.Markup that defines the XAML keywords.
The mapping between an XML namespace and a CLR namespace is done by the XmlnsDefinition attribute at assembly level. You can also directly include a CLR namespace in XAML by using the clr-namespace: prefix.
Syntax:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
</Window>

Disable UAC

                As most have found out if you disable the UAC (User Account Controls) the desk top gadgets don't work. I found the registy edit that lets you disable UAC and keep the desktop gadgets. I did reboot to see if it works and it does!

using Registry Editor

1. Open Registry Editor (regedit.exe)
2. Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Sidebar\Settings
3. Create a new DWORD Value called AllowElevatedProcess
4. Set the value of the new DWORD to 1
5. Close the registry editor. Your gadgets should work now. No reboot or anything necessary.

using Command Line

C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f


Code Sign Vs SSL Certificate

Basically, a code-signing cert gives you a private key that can be verified against a public key certified by a known authority. You make a digital signature with that key, and the other end can verify you had a cert from a trusted source when you signed it.

An SSL cert is just a signed "document" that can be verified as coming from a trusted source. You can't encrypt or sign with it because it doesn't have any key material that's yours alone; it's just a signed document saying "I certify that I trust who this guy says he is."

n SSL Certificate is used for authentication of your website/application server.
A Code Signing Certificate is used for integrity/authentication of the exe,dll,... you deliver.

Get Connectionstring from app.config

Get Connectionstring from app.config



<connectionStrings>
    <add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
  </connectionStrings>

Output

 string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
            MessageBox.Show(connStr);

Get User settings

Get User settings

Input
<userSettings>
        <TestSetting.Properties.Settings>
            <setting name="CompanyName" serializeAs="String">
                <value>Gantec</value>
            </setting>
        </TestSetting.Properties.Settings>
    </userSettings>

Output
MessageBox.Show(Properties.Settings.Default.CompanyName);