Skip to main content

Posts

Showing posts with the label csharp

Convert your datatable into generic poco object in c# using linq, ado and reflections.

Follow @harshit_parshii The most common problem that we face these days is to create a common class and method that can be used across all the projects and codes. So today I will be sharing my code where you can see how to make and create a generic function without using entity framework for ado. net. The scenario is like you have an old software that uses stored procedure to return set of entities as a data-table, you do not want to re-write the back-end code as you are creating a web API in c# which needs to be delivered asap. You need to map these data tables to models as you might be using MV* pattern. So here we will be doing one to one mapping of model to data- table, and in similar fashion insert or update can also be done. So basically we are converting a data-table to list of strongly typed object model to do CRUD operations. So we have following things before hand. A helper class is referenced as the database(dbFactory) which executes ado. ne...

Reflection in Dot Net (.Net)

According to msdn , putting all the definition at one place:- Reflection provides objects (of type Type) that encapsulate assemblies, modules and types. A type represents type declarations: class types, interface types, array types, value types, enumeration types, type parameters, generic type definitions, and open or closed constructed generic types.  You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties. If you are using attributes in your code, Reflection enables you to access them. Below is the code example for it. using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace reflection {   class Program    {      static void Main( string [] args)        {        ...

Three Tier Architecture : A simple Example/Illustration

Three Tier Architecture: Very Easy to understand, the three tier are: 1. Presentation layer 2. Business Layer 3. Database Layer Simple Example: Lets take example of a Songs and Artist. 1. DataAccess Layer Below is a class of Data Access Layer, that has some dummy/fudge data, we only write DataBase calling method in DataAccessLayer. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace DataBaseLayer {     public class DataAccessLayer     {         string [] ArtistName = { "Akon" , "Shakira" , "Michael_Jackson" };         string [] Shakira = { "Waka Waka" , "Addicted to you" , "Beautiful Liar" };         string [] Akon = { "Chammak Challo" , "Lonely" , "Dont Matter" };         string [] Michael_Jackson = { "Beat It" , "Danger...

Beginners Guide to File Handling in C#, .net

Make a new project in Visual Studio (Console App) Add a new .cs file named as MySetting.cs, in that file copy paste the following code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 using System ; using System.Collections.Generic ; using System.Linq ; using System.Text ;...

Asynchronous Threading, First Code

Synchronous threading will execute each thread/command in the order they have been created/started Suppose there are 4 threads A, B, C, D Application Starts at 10:00:00 time: 10:00:00: -> Start thread A (will run for 5 seconds) time: 10:00:05: -> Start thread B (will run for 10 seconds) time: 10:00:15: -> Start Thread B1 (will run for 5 seconds) time: 10:00:20: -> Start thread C (will run for 15 seconds) end application: total run time will be 35 seconds Asynchronous threading will run the threads at the same time/ parallel processing, or we can say it will take the thread that is available at the earliest. Application Starts at 10:00:00 time: 10:00:00: -> Start thread A (will run for 5 seconds) / Start thread B (will run for 10 seconds) / Start thread C (will run for 15 seconds) time: 10:00:10: -> Start Thread B1 (will run for 5 seconds) end application: total run-time will be 15 seconds (because the longest run-time is thread C) So Async Threadi...

Inverted Pyramid, playing with stars

Playing with stars in C# *******  *****   ***     *     class Program     {          static void Main( string [] args)         {             int row, col = 0, n = 5;                                   int k = n;             for (row = 0; row <= n; row++)             {                 for ( int r = 0; r <= row; r++)                 {          ...

Using try/catch/finally --- Exception Handling

Introduction Software Programmers write code to perform some desired actions. But every software may fail to perform its desired actions under some of its internal or external failures. The exception handling system in the C# language allows the programmer to handle errors or anomalous situations in a structured manner that allows the programmer to separate the normal flow of the code from error-handling logic. An exception can represent a variety of abnormal conditions that arise from several possible external or internal conditions of software application. External conditions of execution failures includes, for example, network failures in connecting to a remote component, inadequate rights in using a file/system resource, out of memory exception or exception thrown by a web service etc. These are mainly due to failures thrown by environment components on which our application depends on e.g. operating system, .net runtime or external application or components. Internal f...

What is Software Architecture?

When you build your house, you would never think about building it without an architect, correct? However, many medium to large size software projects are build without a software architect. That seems kind of scary, and you might wonder why? Well, the role of the software architect has neither been widely understood, nor his necessity been acknowledged. Even to date there is still no agreement on the precise definition of the term “software architecture”. Matthew R. McBride writes, "a software architect is a technically competent system-level thinker, guiding planned and efficient design processes to bring a system into existence. He is viewed by customers and developers alike as a technical expert. The architect is the author of the solution, accountable for its success or failure." The term software architecture also refers to documentation of a system's software architecture. Documenting software architecture facilitates communication between stakeholders, documen...