New Year Wishes from CodeSpread Team

Welcome to CodeSpread! CodeSpread Team is enjoying, partying and wishing a very happy new year to all of our users,readers and...

Read More
C# – Box(ing) and Un-box(ing)
Dec31

C# – Box(ing) and Un-box(ing)

I have heard this concept from many people and their different versions so I always wanted to share some insight on it which can work as a one stop solution. What is boxing and un-boxing? The Conversion of a value type into reference type is boxing and the reverse process is un-boxing. What is Value Type? Value Types in C# are the primitive data types such as Boolean, char,numeric, enum and structures. Note: The value types are stored in stack if they are declared inside a method and are stored in heap if they are declared outside method and inside a reference type. What is Reference Type? Reference types are class, delegates, objects ,string and interface. A reference type is always stored in heap. Converting Value types into reference types: Let’s take a simple example. int num = 5;              // num is value type object obj = num;  //obj is reference type Let us go through the underlying steps to understand what really happens during boxing. Statement 1: int num = 5; The numeric constant value 5 is pushed onto stack. The value is then popped into the local variable num. Statement 2: object obj = num ; (Box) As the value type will be represented as object, the memory is allocated on heap. The amount of memory allocated will be equal to the size of the value type plus the memory sufficient to hold object and its internal structures. The Value type (num’s) value (5) is then copied to the newly allocated heap memory. The address of the object is stored on the stack which points to newly allocated heap memory.   Converting reference types to value types: From the above example it is clear that we don’t need to do any explicit conversion when converting value type to a reference type. However, to un-box casting is necessary. This is because, the object is being converted to a type and the run-time can validate the cast. int num = 5; object obj = num; int anotherNumber = (int)obj; // Un-boxing Now, let us understand what happens during un-boxing. Statement 3: Un-boxing First, The runtime validates the object, pointed by the address on the stack. If the value cannot be converted to the type specified in the instruction then, InvalidCastException is thrown. If the object is valid, then CLR proceeds further. The value of the instance (in this case 5) into the value type (int) variable (anotherNumber). .NET features automatic type handling. Why do I need to be careful about boxing and un-boxing? It is true that nothing special needs to be done to take advantage of boxing....

Read More
Freeze Business Logic Layer.
Dec30

Freeze Business Logic Layer.

We regularly work on three-layer architecture for application development and always use some standard data layer and UI layer guidelines for design but what we always ignore is business logic layer. The reason behind maybe we are not comfortable in designing something which has no limits and no boundaries defined. If I share my experience; whenever we have tried to freeze our business logic, ironically, we felt a need to extend it to accommodate new requirements or sometimes for better optimization. Though, I have no correct answer for achieving best design for business logic layer but we can explore it a bit. What is a Business Logic Layer? One of the architecture followed in application development environment is Layer Architecture.With this architecture, Applications is trusted with different layers responsible for performing a designated role. It improves isolation of concerns. Mainly Layers are divided into the Data Access layer the Business Logic layer the Graphical User Interface layer For more information on architecture,read this article. The business layer contains business entities. A business entity is a type of business component that represents a domain model object used within the service boundary. Typically, business entities represent real-world objects, such as customers and orders. It contains all the business rules that are applied to data exchange process which happens between the presentation layer and the Data access layer. Business rules describe the operations, definitions and constraints that apply to a user/community/organization. To understand it, check these examples A customer is entitled for bonus offers if he has done a purchase of minimum limit. User is not allowed to register if it does not adhere to specific guidelines. Employer can enlist constraints and conditions on employees of the organization. Legal rules Benefits of Business Logic Layer Flexibility: Business Logic Layer which provides decoupling from the application and allows the flexibility to build other applications on top of it in the future. Maintainability:  Business Logic Layer optimizes the way that the application works when deployed in different ways, and provides a clear delineation between locations where certain technology or design decisions must be made. Reusability: Business Logic Layer follows components based architecture and can be reused in different business models.Each logical layer contains a number of discrete component types grouped into sub layers, with each sub layer performing a specific type of task Scalability: Business Logic Layer can perform well in extended conditions. Guidelines for Business Logic Layer Design Loose coupling is the key: Different patterns are available to implement loose coupling like Factory Design Pattern, Singleton Pattern.The business layer should know only about the layer below (the data access layer), and not the...

Read More
Try one ORM: Entity Framework
Dec30

Try one ORM: Entity Framework

As promised in the last article, We are going to share our experience of Entity Framework Ver 4.1. First of all, we have to add and configure Entity Framework in visual studio. We are going to use “NuGet” manager. Steps to check whether “NuGet” manager is already installed. 1)Open Visual Studio 2010, Go to Tools menu and click on Library Package Manager. If NuGet is not installed,Follow the below steps to install it through Extension Manager. 1)Open Visual Studio 2010, Go to Tools menu and click on Extension Manager. 2) On left hand side, you will find Online Gallery, Click on it. 3) Download NuGet Package Manager and install it. What is NuGet Package Manager? From codeplex “NuGet is a free, open source developer focused package management system for the .NET platform intent on simplifying the process of incorporating third-party libraries into a .NET application during development.” We use several third party libraries in the project development and have to each location for the library and integrate the library in our project which is quite a painful process in the long run. NuGet heals the process by introducing a package management which streamline the process of sharing and using open source libraries. Steps to add Entity Framework: 1)Go to Project,Click on Manage NuGet Packages. 2) Search for Entity Framework and Install. 3) Our recommendation is to download POCO also. Open Extension Manager again and Go to online Gallery, Here search for EF. 2)Download and Install EF 4.x POCO Entity generator for c# We will provide a more detailed article for POCO sometime in future as it can’t be covered in one or two lines. To see the results of successful installation, Add new item to the project and see Entity Data Model Template in the window. Later, we will use these models to demonstrate “Model First Approach” and “Code First Approach”....

Read More
Magic Wand: Object-relational mapping
Dec29

Magic Wand: Object-relational mapping

As a developer, we are always looking for reusable components which can ease our life and I can suggest ORM as the developer’s magic wand because it wipes off the overhead of dealing directly with the database and encourages OOP way of data  operations. What is Object-relational mapping? Wikipedia says “Object-relational mapping (ORM, O/RM, and O/R mapping) in computer software is a programming technique for converting data between incompatible type systems in object-oriented programming languages.This creates, in effect, a “virtual object database” that can be used from within the programming language.” The concept is easy to say and difficult to imagine so we first of all try to draw it to have a better clarity. To create a mapping, we require two ends so what are the two ends here? One of the end is data and other is objects. Don’t you think, we should know both these terms first, before creating a mapping between the two ends. Data is maintained in databases and these databases usually follows relational model like- we can have different tables in our database and these tables can be referenced by each other by following one of the relationships- One-to-One One-to-Many Many-to-One many-to-many Objects are real-word entities having some state and behavior. State of an object is represented by the fields and behavior by methods. Like, if we want to know the name of a user, we just have to call User.Name where User is an object and Name is the state of the object. Now, if we look from the developer’s point of view, then it is clearly visible that how easily we can operate an object. Just imagine, in similar way,if we can operate on our data then our life will become much more comfortable. To achieve that, we need to create a mapper between our data and objects like- Here,Mapper does a transition between those database and objects. For example, it transits all related records from a table to the mapped object . Mapper is the layer of abstraction that hides all the implementation details, we don’t handle all the sql overhead but just work with the generated useful Objects,the OOP way. What is the need of Object-relational mapping? or What are the benefits of Object-relational mapping? Simple: We have already discussed, how easy it is, to work with objects than a relational database. Less Code :  Less code as compared to manual queries, handwritten stored procedures, triggers,functions Reduced development and maintenance costs: No big shots code required for regular database operations. Caching: Object caching provides improved system performance. Concurrency: Multiple users can do operations at the same time. Loading of...

Read More
Page 1 of 10123456...Last »