Skip to main content

Posts

Showing posts from 2016

Why Interface For Decoupled System? A Note On How To Design Decoupled System in ASP.NET MVC

An Interface At first, interfaces came into focus because there was no multiple inheritance supported by C#, meaning you could not inherit from multiple classes, but you could implement multiple interfaces. The interfaces are grouping of object in terms of behavior. An interface contains only signatures. There is no implementation, only the signatures of the functionality the interface provides. An interface can contain signatures of methods, properties, indexers & events. Interface doesn't do anything like classes and abstract classes, it just defines what the class will perform if some class inherits it. An interface can also be inherited from another interface. If we take an example of USB (Universal Serial Bus), it's an interface which has signature of Connection functionality. The USB interface only knows I have to connect with Desktop Computer or laptop or anything, but it does not know what implementation will come to connection. It may be Pen d

Generic Repository Pattern MVC

The good architecture is at the heart of any project. The developer is always looking for great architecture that reduces repetitive code and separates the Data Access and Business Logic. So we will be creating our generic repository using ASP.NET MVC and Entity Framework. If you don’t know about Entity Framework, please click here to get started with Entity Framework. So before getting started with repository pattern, firstly we have to understand what is the repository pattern? and why we want it? Repository and Unit of Work Pattern In simple words, repository means, the abstraction between your Data Access and Business Logic layers which will very useful for unit testing or test-driven development (TDD). By using repository, your system will be more loosely coupled. In programming terms, we have to create one interface for each repository or entity. e.g. Student entity has One interface ( IStudentInterface ) in which all methods are declared like Insert , Update , Delete , Ge

Check Session Timeout by Using ActionFilters in MVC

In a dynamic web application, the session is crucial to hold the information of current logged in user identity/data. So someone without authentication cannot have access to some Page or any ActionResult , to implement this kind of functionality, we need to check session exists (is not null ) in every action which required authentication. So, the general method is as follows: [HttpGet] public ActionResult Home() { if(Session["ID"] == null) return RedirectToAction("Login","Home"); } We have to check the above 2 statements each time and in each ActionResult , but it may cause 2 problems. Repeat Things : As per the good programming stranded, we don't have to repeat the things. Create a module of common code and access it multiple times/repeatedly Code missing : We have to write code multiple times so it might happen some time we forget to write code in some method or we missed it. How To Avoid? The ASP.NET MVC provides

How To Create Custom HTML Helper in ASP.NET MVC? Here Is The Way.

In this article, I will explain how to create a custom HTML helper as per your requirement. If you know the MVC and Helpers already, you can skip the next paragraph and if you don't know, here is an explanation about helpers. What are HTML Helpers in MVC? An HTML helper is a method that is used to render the specified html content in View. It can be implemented as an extension method. If you know the ASP.NET webforms, you might know the concept of web controls. e.g. <asp:TextBox /><asp:label /> If you use these controls in web form on browsers, they will converted to their equivalent HTML controls like TextBox will be <input type="text" /> and label will be <span></span> HTML helpers methods are the same as web controls it allows to render appropriate HTML in view. Let's take an example of how HTML helper looks like: @Html.TextBox("Myname","Mayur Lohite") This HTML helper will render the following HTML