Thursday, 24 September 2015

Object, Dynamic and Var in c#

In this post I am going to write the points about the three type of the variable Object, Var and Dynamic. Most of the developer not able to get what is difference between this three kind of variable.ObjectDynamicVarCan able to store any kind of value, because object is the base class of all type in
In this post I am going to write the points about the three type of the variable Object, Var and Dynamic. Most of the developer not able to get what is difference between this three kind of variable.
ObjectDynamicVar
Can able to store any kind of value, because object is the base class of all type in .net framework.Can able to store any type of the variable, similar to old VB language variable.Can able to store any type of value but it require to initialize at the time of declaration.

Compiler has little information about the type

Compiler doesn't have any information about the this type of variable.

It's compiler safe i.e compiler has all information about the stored value, so that it doesn't cause any issue at run-time.

Object type can be passed as function argument and function also can return object typeDynamic type can be passed as function argument and function also can return object typeVar type can not be passed as function argument and function can not return object type. This type of variable can work in the scope where it defined.

Require to cast object variable to original type before using it. So this assigning to object type and converting to original type called as Boxing and Un-Boxing for value type and for the reference type its casting of types. It's actually increasing the overhead when we do this both operation.
Allows to perform operation of given type once it get cast any user defined or primitive data type.
Casting is not require but you need to know the property and methods related to stored typeNo need to cast because compiler has all information to perform operation.
Cause the problem at run time if the stored value is not get converted to underlying data type.

Cause problem if the wrong method or property accessed because all the information about stored value is get resolve only at run time

Doesn't cause problem because compiler has all info about stored value.
Useful when doesn't have more information about the data type.Useful when coding using reflection or dynamic language support or with the COM objects, because we require to write less amount of code.Useful when getting result out of the linq queries. In 3.5 framework it introduce to support linq feature.

Wednesday, 23 September 2015

real world use of dynamic in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using Moq;

namespace DynamicKeyword
{
    public class Base
    {
        public void foo(int x) { Console.WriteLine(x); }
    }
    public class Derived : Base
    {
        public void foo(string x) { Console.WriteLine(x); }
    }
    class Program
    {
        static void Main(string[] args)
        {

            dynamic baseclass = new Derived();
            dynamic d = "4";
            dynamic c=1;
            baseclass.foo(d);
            baseclass.foo(c);
            //new StaticTester().Call();
            //new ReflectiveTester().Call();
            //new DynamicTester().Call();
            Console.ReadKey(false);
        }
    }

}

what is business logic?



The Business Logic layer handles all of the business rules, calculations and actual logic within your application that makes it actually "do" things and it may often use some of the objects retrieved from your data-access layer. This layer would actually consist of more actual "code" and conditions that would be used to apply your "business rules".

So you can basically think of data-access just as the name implies, it accesses and deals with sending and retrieving data from the database. The business logic layer on the other hand, deals with how you actually use some of the data from the database and ultimately what it can and cannot "do" within your application.

The term "business logic", in the context of web applications, refers to the functional requirements of a system which pertain to an organization's operating requirements. This compares with the "interface logic", used to define the mechanisms with which a person might interact with a web application, and "data model," used to specify the structure of and relationships among the different types of information pertaining to the application. 





An example of business logic would be a set of requirements where system users with a given permission are required to review and approve or reject submissions of a given type made by system users who have been given a different sort of permission. Another example would be a verification that credit card payments only be processed if a complete address is provided.





In general, the proper development of business logic for web applications requires the understanding and approval of those individuals with managerial authority over a project. Otherwise, developers may implement a system based on assumptions which conflict with the actual business requirements of the organization.

Temp Table Vs Temp Variable in sql

Temporary Table
Table Variable
Temporary tables can be used in Stored Procedures, Triggers and Batches but not in user defined functions

Table variables can be used in user defined functions, stored procedures, and batches
Local temporary tables are temporary tables that are available only to the session that created them. Global temporary tables are temporary tables that are available to all sessions and all the users.
Its scope is in the stored procedure, user defined function or batch where it is declared like any local variable we create with a DECLARE statement.
Local temporary tables are automatically destroyed at the end of the procedure or session that created them. Global temporary tables are dropped automatically when the last session using the temporary table has completed
We can also drop temporary tables explicitly using drop command similar to normal table.

Table variables are automatically cleaned up at the end of the user defined function, stored procedure, or batch in which they are defined.
Temporary table name can be of maximum 116 characters
Table variable name can be of maximum 128 characters
PRIMARY KEY, UNIQUE, NULL, CHECK etc can be implemented at the time of creating temporary tables using CREATE TABLE statement or can be added after the table has been created. FOREIGN KEY not allowed.

PRIMARY KEY, UNIQUE, DEFAULT values, NULL, CHECK can be added, but they must be incorporated with the creation of the table in the DECLARE statement. FOREIGN KEY not allowed.
Temporary table supports adding Indexes explicitly even after creation and it can also have the implicit Indexes which are the result of Primary and Unique Key constraint.
Table Variables doesn’t allow the explicit addition of Indexes after it is declared, the only means is the implicit indexes which are created as a result of the Primary Key or Unique Key constraint defined at the time of declaring Table Variable.
Temporary tables can also be directly created and data can be inserted using Select Into statement without creating a temporary table explicitly.
Table variables can’t be created using Select Into statement because being a variable it must be declared before use

The SET IDENTITY_INSERT statement is supported in temporary table
The SET IDENTITY_INSERT statement is not supported in table variables
We can’t return a temporary table from a user-defined function

We can return a table variable from a user-defined function

Temporary Table can be truncated like normal table

Table variables can’t be truncated like normal table or temporary tables.

The data in the temporary table will be rolled back when a transaction is rolled back similar to normal table

The data in the table variable will not be rolled back when a transaction is rolled back

Temporary tables used in stored procedures cause more recompilations of the stored procedures than when table variables are used.

Table variables used in stored procedures cause fewer recompilations of the stored procedures than when temporary tables are used.

A temporary table will generally use more resources than table variable

A table variable will generally use less resources than a temporary table

Temporary tables can be access in nested stored procedures

Tables variables can’t be access in nested stored procedures

Can be altered using ALTER command
Does not support ALTER command

Temporary tables should be used for large result sets.
Table variables should be used for small result sets and the everyday type of data manipulation since they are faster and more flexible than temporary tables

Dotnet Hell or Dll-Hell

Problem of Dll-Hell

Introduction 
Before some time, if we install an application then dll of that application get stored in the registry, then if we install other application that has same name .dll  that means previously installed .dll get overwrite by the same name new .dll. Ok for newly installed application but previously installed application cant get execute further. This is big problem in context of version of same application. This is Dell-Hell problem.
OR
Dll Hell refers to a set of problems caused when multiple applications attempt to share a common component like a dynamic link library (DLL).The reason for this issue was that the version information about the different components of an application was not recorded by the system.

Solution of Dll-Hell Problem 
This problem of dynamic link library (.dll) is resolved through Versioning.

Versioning:
Versioning is the technique to provide version to the .dll to prevent them from replacement. GAC (Global assembly cache) is the separate memory like cache it is used to remove load form operating system.

To add version in assembly we just write in Program
 :

versin mmmmm.GIF

We can do versioning only with shared assembly because to install .dll in GAC so we need to have strong key name.

Authentication:verification, to be verify.

Authorization  :Providing Role, to provide power ,step after authentication in role. 

To see the parts of an assembly:
open cmd prompt of Visual studio and write - ILDASM

ILDASM: is a tool to see the content (metadata) of Assembly.

What are the characteristics of C#?


C# is Object oriented programming language.
C# is high level language.
for similar kind of questions in C# refer this link

    There are several characteristics of C# are :
  • Simple
  • Type safe
  • Flexible
  • Object oriented
  • Compatible
  • Consistent
  • Interoperable
  • Modern

Can you overload static constructor?

No, because you cannot invoke a static constructor explicitly, it is invoked implicitly by the run time before the objects are instantiated. Also, you cannot pass any parameters to a static constructor(its always parameter less) therefore it cannot be overloaded.

1.It can only access the static member(s) of the class.
Reason : Non static member is specific to the object instance. If static constructor are allowed to work on non static members it will reflect the changes in all the object instance, which is impractical.
2.There should be no parameter(s) in static constructor.
Reason: Since, It is going to be called by CLR, nobody can pass the parameter to it. 3.Only one static constructor is allowed.
Reason: Overloading needs the two methods to be different in terms of method/constructor definition which is not possible in static constructor.
4.There should be no access modifier to it.
Reason: Again the reason is same call to static constructor is made by CLR and not by the object, no need to have access modifier to it