Tuples C# 7

by

In order to return multiple values from a method in C#, we can use approaches like creating out parameters, creating a custom class with the required properties, or using tuples. 

 

However, in C# 7.0, we have the concept of tuples (quite similar to existing tuples), which can help in returning multiple values from a method.

 

Suppose we have a method, which will return the Department and Age of an employee, based on the EmployeeID. Using the old ways, we could have two out parameters for department and age, along with the EmployeeID, or create a custom class of an employee type and return the values or even use the existing tuple types. However, with new features, we can have a method with two return type values; i.e., int age and string department. The signature of the method would look, as shown below.

public static (int, string) GetEmployeeById(int employeeId)         {             int empAge = 32;             string dept = "HR";             return (empAge, dept);         }

After calling the method, we can access the return values, as shown below.

var employeeDetails = GetEmployeeById(21);  Console.Write("Age is: " + employeeDetails.Item1+ ", Department is:" + employeeDetails.Item2); 

The code does not seem to be too user friendly here, as we have to use the keywords Item1, Item2 etc. (similar to the existing tuples concept). However, we can change our method signature to include the use of names for the return values. Hence, the code will change to what is shown below.

public static (int age, string department) GetEmployeeById(int employeeId)           {               int empAge = 32;               string dept = "HR";               return (empAge, dept);           }  

Now, in order to access the elements, our code will change to what is shown below.

var employeeDetails = GetEmployeeById(21);    Console.Write("Age is: " + employeeDetails.age + ", Department is:" + employeeDetails.department);

this was about the concept of tuples in C# 7.0.

 

 

?Source :C#Corner,MSDN

C# 7 Local Functions

by

The concept of function within a function, like we normally do inside a class.

We will create a method GetName() .

The code will look like the following.

static void Main(string[] args)  {     Console.WriteLine("Name is: " + GetName());  }  public static string GetName()  {    return "Name";  }  

Next, we need to add more , will create Function called GetFullName() , will call 2 methods GetFName() and GetLName. 

This result will be added to the GetFullName() and the complete result will be returned.

 

The main point here is that if we create a public method to concate our string togather, it will be globally available. If we create a private method, the method will be available to other methods as well, which is not required. So, what we will do is create the method as a sub-method within the GetFullName function. This will be created like any other normal function. So, the code will look like the following.

namespace ConsoleApplication1  {      class Program      {          static void Main(string[] args)          {              Console.WriteLine("Full Name is: " + GetFullName());          }          public static string GetFullName()          {              var FullName= GetFName() + " " +GetLName();                return FullName;                          string GetFName()              {                  return "First Name";              }              string GetLName()              {                  return "Last Name";              }          }      }  

 we have created another two methods GetFName() and  GetLName() within a function to return FullName(). 

 

 

Onion Architecture in ASP.Net MVC ????? ??????

by

Core project

 

 

?????? ???????? , ?? ?????? ?????? ?? ???? ??????? , ?? ??????? ???? ??? ??? ?????? Web layer , Infrastructure layer , ????? ??? ??? ?????? .

 

 

?? ????? ?? ?????? ???????? core  ?? ???? ??? ?? ???? ???? .

?????? ?? ????? Core layer  ??? ?????? :

???? ???????? (Domain Entities)

?????? ?????????? ??????? (Repositories interfaces)

??? ?? ?? ???? ?? ?????? ??? ????? ?????? , ??? :

Reference of the ORM like LINQ to SQL or EF

  1. ORM like LINQ to SQL or EF
  2. ADO.NET libraries
  3. Entity Framework .

Create Entity

?????? , Entity  ????? ?????? BloodDonor entity class. .

 

?? ???? ????? ??? ?????? , ????? ????? , ??? ??? ???????? ??????? ?? ??? , ?? ???? ?????? ???? ?????? , ?????? ?? ???????  .

????? ???????? ??????? , ???? ??? ?? ????? ???? :

  1. System.ComponentModel.DataAnnotations
  2. Entity Framework fluent API.

??? ????????? ???????? , ???? ?????? . ?? ??? ???? ?????? ???????? ???? ?? ????? ?????? ??? EntityFramework , ??? ???? ??????? DataAnnotations  .

???????? DataAnnotations  ?? ?????? ?????? ?????? , ??? ????? .

System.ComponentModel.DataAnnotations

 

??? ????? BloodDonor entity class ?? DataAnnotations  .

?????? ??????? ????? ?????? ???????? ??????? repository interface

Repository interface

??? ???? ????? ?????????? repository  ,   ??? ?????????? ???? ?? ??????? ???? ?????? ?????? ???????? ??? ??????? ??? ?? ???? ???? .

?? ?????? ?????????? repository interface , ??? ??? ????? ?? ?????? ????? ???????? ???? ??????? .

 

??? ???????? ?? ????? ?? ??????? ?????????? ?? ?????? ,?????? ??????? ????? ???? ??????? infrastructure project .

Infrastructure project

 

?? ????? ?????? ??????? ,??? ???? ????? ???????? ???????? ???? ???????.

  1. Database operation
  2. Accessing outside service
  3. Accessing File systems

 

?????? ?? ???? ???????? ??????? ???? ?? ????? ?????? ???????. ??? ??????  Entity Framework ?????? ?????? ????? ????????. ??? ???? ??  Entity Framework code first .

 ???? ??? ????? ?????? ??????? ???????:

  1. Create the data context class
  2. Implement the repository class
  3. Create the database initializer class

??? ????? data context class , ??? ????? ???? Entity Framework , ?? Nuget Package Manager .

DataContext class
 

????? ?????? Data context class  .

 

Connection string

< connectionStrings >< add name = "BloodDonorContextConnectionString" connectionString = "Data Source= LocalDb)\v11.0;Initial Catalog=BloodDonors;Integrated Security=True;MultipleActiveResultSets=true" providerName = "System.Data.SqlClient" / >< /connectionStrings>

 

???? ......

 

Source:c-sharpcorner,debugmode

Onion Architecture in ASP.Net MVC ????? ?????

by

Onion Architecture in ASP.Net MVC

 

???? Onion Architecture ?

?? ??????? ??????? ??  ????? ????????? ?????? ?????? ????????? ???????? ?????????? ?? ????? ??????? ??? ????? ???????? ????????.  ?? ??????? ??? ??????? ???? ??? ?? ??? Jeffery Palermo ?? ?????? ?? ??? 2008

 

????? Onion Architecture

- ?? Onion Architecture  ??????? ????? ?????? ????? ??????? ???????? (Interface) .

- ?? ?????? ??? ??????? ???????? ??? ?????? ??? ????? ???????? ? Web Service  ????? ?? ??? ?? ??????? ????????.

- ???? ??????? ?? ???? ???????? ?? ???? UI  ?? ??? ?? ??????? ???????? .

- ??????? (Obj) ???? ???? ?????? ?? ??? ?? ??????? ????????.

- ???? ??????? ???????? ?? ????? ??? ????? ???????? ?? ???????? ??? .

- ?? ????? ?? ????? ??? ???? ?????? ??? ??????? ????????.

- ?????? (domain object)???? ?? ?????? ?? ?????? , ??????  ?????? ??? ?? ?? ????? ???????? ?????? ????? ????????.

- ?? ???????? ??? ?? ???? ??? ?????? ???????? , ?? ?????? ??????? ??????? ?? ????? ??? ??????? ???????? .

- ?? ??????? ?? ???? ???? ????? ??? ????? ??????? ???????? .

 

???? ???????

 

????? ?? ????? ???? ??????? . ??? ???? ?? ???????? ??????? ??????? :

  1. Core project (Class library)
  2. Infrastructure project (Class library)
  3. Web project (MVC project)

 

 Core Project

 ?????? ??? ?????? (domain entities) ????????? (repositories interfaces) .

Infrastructure project

???? ??????? ??? ????? ??? ?????? ???? ?? ??????? ???????? .

Web project

?????? ???????? ??? ????? ??? MVC Controllers  ?HTML  .

???? ??????? ??? ?? ???? ??? ????? ?????? :

???? ........

 

Source:c-sharpcorner,debugmode

Methods In C#.NET

by


Methods are very useful in C#. Since any code is to be used at many places, we can define that in a method and can use the code only by calling that method .

Method signature

A method signature contains various things, which are stated below.

[Attributes]    (e.g.: [WebMethod],[HttpPost],[HttpGet])Public void TestMethod(string parameters)


   Explanation

       In the method shown above, Public signature defines the Access modifier.

       Void defines the no return type (in case of void, No return type is there and In case of string, its string) of the method.

       If the return type is defined as string, then the method must return some string as its output. If we do not want a method to return anything, we can define it as void.

TestMethod

It defines the name of that method.

The string parameters defines the parameter that this method will expect during a call. 

 

 class Program      {  public static void Main(string[] args)          {    InstanceMethodClass  p = new InstanceMethodClass();              p.InstanceMethod();  }    }    public class InstanceMethodClass      {  //This is an Instance method.  public void InstanceMethod()          {              Console.WriteLine("This is a test instance method.");          }  } 

Static Method

We do not need to create the object of the class in which method is defined. We can invoke the method by using it directly.

class Program      {          public static void Main(string[] args)          {              ClassForStaticMethod.StaticMethod();          }  }     public class ClassForStaticMethod      {             public static void StaticMethod()          {               Console.WriteLine("This is a test static method.");          }      }  

Let's take an example of method with the parameters.

In the example shown above, we need to pass a string argument because "PrintParameter" method expects the string as parameter.

Let's take an example of return type method.

class Program      {          public static void Main(string[] args)          {  InstanceMethodClass i=new InstanceMethodClass();  i.PrintParameter("Argument passed.");  }  }   public class InstanceMethodClass{      public void PrintParameter(string parameter)          {              Console.WriteLine(parameter);          }  }  

In the example shown above, we need to pass a string argument because "PrintParameter" method expects the string as parameter.

Let's take an example of return type method.

class Program      {          public static void Main(string[] args)          {  InstanceMethodClass i=InstanceMethodClass();   int sum=i.ReturnSum(2,6);              Console.WriteLine(sum);    }  }     public class InstanceMethodClass      {  public  int ReturnSum(int a,int b)          {              int sum = a + b;              return sum;          }  }  

In the example shown above, we used int as return type instead of void. Thus, the method must return an integer. When we invoke this method, we can get the returned result in an Int variable.

In the two examples stated above, it will remain the same for static method but the only difference is the method needs to be defined as static and invoked without using a class instance. 

Thank you.

Source c-sharpcorner , MSDN