Knowledge Enhancement - RushKar

C#, .Net, MVC, SQL, BizTalk, Angular, JAVA

NAVIGATION - SEARCH

Software Development – Not an Easy Task

Hello friends, today I am going to explain that, what is the exact role of developer in this process, generally we think that it’s an easy task with a limited time period, but it’s not like that.

When we started Software Development process, first of all we need to understand the whole process, what we are going to develop, as in today’s fast world, people are more interested in fast output compare to better output, as everybody in hurry, but “the good things are not build in a day” and these hurries always spoils the output.

Software Development process starts with an idea, an idea comes into a mind of a client, now a days everybody wants customize software for their business, and it should be, as data managing and smart work always doubles the output, so with this idea client goes in the market, and of course client has some specific budget, based on his idea and budget client try to finds suitable person/company for his requirements. So he post the requirements on any freelancing website or do Google for software companies, after some basic analysis he send requirements to some company, and in case of freelancing site, client choose developer on bases of rating and his past work experience, once client finalize the developer or company, he enters to the process, the process which called “Software Development”.

At this stage client becomes more firm and eager to get output, as he selected a partner, which helps him to make his dream true. Now the role of developer is started at this point, as per my experience and market study, many developers do not understand the value of his efforts and role, as developer is only the person, who can make client’s dream true.

Requirement Gathering, this is the most important and difficult part of this process. As client is always not a technical person, so the big role of developer is to get the exact idea from the client, in actual what client wants. Most of the developers lack in this point, as developer has some experience and based on this past experience developer make his mind with a particular thing and tries to drive client requirement in that direction, rather than treat this development as a fresh copy. So “New Client, New Game”

Also always make practice to take things on paper, here the role of paper is not to finding fault, but its role is to stuck on particular things, as development process take months (in some cases it takes years), and in between this time, there is some possibilities that client or developer messed up with requirements.

In some cases, client is confused, as he has clear idea in his mind, but he can’t explain it to developers, because he is not that much technically sounds, and not that much friendly with software domain words and tools, so as a developer, we always understand requirement with an example. Always take example from client, with particular cases and the same way tries to explain with an example to client, so he can get the proper idea.

In some cases, it’s good to ask “WHY” to the client, so rather than blindly follow the requirements, first understand it, if the developer can’t understand what he is developing then how can we expect that user understands the software, and there is a strong possibilities that developer can suggest some better option/way to the client, so as a developer we always need to understand what we are developing.

The same way this process is very tough for the client also, as he has make up his mind with some specific output, and tries to cover all his requirements in single phase, also he is not a technically sounds, and also not having a long vision that he can identify different phases, so again developer’s role comes in the action, and he should guide to client in proper direction, and always make try to first understand what he actually wants, and the best way to start with a mock up, mock up is always very useful when we are driving a big project. As client get some basic idea that what developers are developing, and

Also the another important factor is budget, as client is bounded to some specific budget, so many time all the requirements can’t be developed in that budget, and most of the client make mistake in this type of situation, so rather than fit all the requirements in this budget, try to get in to incremental model, divide this requirements in small part, and then cover some of the parts in first phase with budget, this way client will get the better output.

So as a developer always remember that we are not performing any task, we are developing software which will be used by many people to simplify their processes, and with this responsibility, in addition of technically challenges, developer needs to fulfill client’s dream also, and all these things make software development process tough.

ASP.NET Web API Versioning

ASP.NET Web API is a framework to build HTTP services for multiple clients, including browsers and mobile devices. ASP.NET Web API is platform to build RESTful applications on top of .NET Framework. If creating ASP.NET Web API for multiple clients then it could be required to have versioning in API which allows you to alter behavior between different clients.

Why need versioning?

  • API versioning should be required if any change in data contract such as renaming or deleting parameter or change in format of the response is required.
  • Change in URL can also be considered for versioning. E.g. when it changes from api\user?id=123 to api\user\123 can be considered change in version.
  • API versioning will introduce complexity for both service implementation and client using API.

REST does not provide any versioning but it can be achieved with help of one of the following more commonly used approaches,

URI Path

    1. Add API version in URI. e.g. api/v1/user/123 and api/v2/user/123
    2. It is very common and most straightforward approach though it violets REST API design who insist a URI should refer to unique resource.
  1. Custom Request Header
    1. In this approach, customer header allows you to use URIs with versions which will be duplicate of content negotiation behavior implemented by existing Accept header. e.g. api-version: 2
  2. Content Negotiation
    1. It most recommended approach nowadays, it allows clean set of URIs with help of accept header with some complexity to server different version of content/resource. e.g. Accept: application/vnd.domain.v2+json

API versioning, which to choose?

  • Considering the continuous change in API, any versioning approach could be wrong at some point.
  • A real challenge with versioning is managing the code base which serving multiple versions of resource. If all the versions are kept in same code base then older versions could be vulnerable at some point for unexpected changes. If code base are different then very high chances of escalation in maintenance.
  • A versioning could be obstacle to improvements as version change are restricted.
  • Versioning with content negotiation and custom headers are popular nowadays, but versioning with URL are more common now as it's easier to implement.
  • It’s a pragmatic decision, but API should have version from the first release.

 Resource versioning?

  • With versioning of API it should be required to version resources as different version may return resource with change. It can be easily achieved with help of inheritance.

ASP.NET Web API versioning can be achieved by extending DefaultHttpControllerSelector, following code illustrates versioning with content negotiation

public class ContentNegotiationVersioningSelector : DefaultHttpControllerSelector
    {
        private HttpConfiguration _HttpConfiguration;
 
        public ContentNegotiationVersioningSelector(HttpConfiguration httpConfiguration) : base(httpConfiguration)
        {
            _HttpConfiguration = httpConfiguration;
        }
 
        /// <summary>
        /// extesnsion to default controller selector
        /// </summary>
        public override HttpControllerDescriptor SelectController(HttpRequestMessage request)
        {
            HttpControllerDescriptor controllerDescriptor = null;
 
            // get list of controllers provided by the default selector
            IDictionary<string, HttpControllerDescriptor> controllers = GetControllerMapping();
            // get request route data
            IHttpRouteData routeData = request.GetRouteData();
 
            if (routeData == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
 
            // get api version from accept header
            var apiVersion = GetVersion(request);
 
            //check if this route is actually an attribute route
            IEnumerable<IHttpRouteData> attributeRoutes = routeData.GetSubRoutes();            
 
            if (attributeRoutes == null)
            {
                string controllerName = GetRouteVariable<string>(routeData, "controller");
                if (controllerName == null)
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
 
                string versionControllerName = String.Concat(controllerName, "V", apiVersion);
 
                if (controllers.TryGetValue(versionControllerName, out controllerDescriptor))
                {
                    return controllerDescriptor;
                }
                else
                {
                    throw new HttpResponseException(HttpStatusCode.NotFound);
                }
            }
            else
            {
                // find all controller descriptors whose controller type names end with
                // the following suffix (example: ControllerV1)
                string versionControllerName = String.Concat("V", apiVersion);
 
                IEnumerable<IHttpRouteData> filteredAttributeRoutes = attributeRoutes
                    .Where(attrRouteData =>
                    {
                        HttpControllerDescriptor currentDescriptor = GetControllerDescriptor(attrRouteData);
 
                        bool match = currentDescriptor.ControllerName.EndsWith(versionControllerName);
 
                        if (match && (controllerDescriptor == null))
                        {
                            controllerDescriptor = currentDescriptor;
                        }
 
                        return match;
                    });
 
                routeData.Values["MS_SubRoutes"] = filteredAttributeRoutes.ToArray();
            }
 
            return controllerDescriptor;
        }
 
        /// <summary>
        /// gets the controller descriptor for attribute routes.
        /// </summary>
        private HttpControllerDescriptor GetControllerDescriptor(IHttpRouteData routeData)
        {
            return ((HttpActionDescriptor[])routeData.Route.DataTokens["actions"]).First().ControllerDescriptor;
        }
 
        /// <summary>
        /// gets value from the route data, if present.
        /// </summary>
        private static T GetRouteVariable<T>(IHttpRouteData routeData, string name)
        {
            object result = null;
            if (routeData.Values.TryGetValue(name, out result))
            {
                return (T)result;
            }
            return default(T);
        }
 
        /// <summary>
        /// gets the type of the version
        /// Accept: application/vnd.domain.v{version}+json
        /// </summary>
        private string GetVersion(HttpRequestMessage request)
        {
            var acceptHeader = request.Headers.Accept;
 
            var regex = new Regex(@"application\/vnd\.domain\.v([\d]+)\+json", RegexOptions.IgnoreCase);
 
            foreach (var mime in acceptHeader)
            {
                Match match = regex.Match(mime.MediaType);
                if (match.Success == true)
                {
                    return match.Groups[1].Value; // change group selection based on regex if requried
                }
            }
 
            return "1"; // return latest version if not accept header provided (should be configured)
        }
    }

Add following line to replace default controller selector,

config.Services.Replace(typeof(IHttpControllerSelector), new ContentNegotiationVersioningSelector(config));

Controller hierarchy for versioning

Versioning resource or POCO classes

Similarly, URL and custom header based versioning can be implemented by extending DefaultHttpControllerSelector as above.

Micro ORM vs ORM

What is O/RM?

Object/Relational Mapping (O/RM) is a mechanism to store and/or retrieve data from domain objects without considering how they are related with their data sources, which makes the application maintainable and extendable. Object/Relational Mapping (O/RM) encapsulates the code which needed to manipulate the data that allows programmer to interact with data in same language, so programmer no longer has to use SQL. ORM manages the mapping between set of domain objects and relational database objects. ORM automates standard CRUD operation (Create, Read, Update & Delete) so that the programmer doesn't need to write it manually.​

What is Entity Framework?

Entity framework is an Object/Relational Mapping (O/RM) framework, an enhancement to ADO.NET which provides a mechanism for accessing & storing the data in the database. It can be used in ASP.NET applications for data related operations.

Why Micro ORM?

Most of the ORM tools like NHibernate or Entity Framework has many features those are implemented in years as required, but in a real world, developers are using a small subset of them and complexity of unused features may downgrade the performance of an application.

Micro ORM only provides few set of features like read data from the database and map them to domain objects. Micro ORM requires very fewer configurations and are easy to use compared to full ORM.

E.g. Micro ORM dapper provides extension methods to DbConnection class and adding a reference to dapper will allow accessing database using those extension methods and if consider the performance it provides a result with time near to ADO.NET.

Why ORM?

ORM supports relationship for domain objects means related objects can be loaded automatically if required. To achieve this with Micro ORM query needs to be formed accordingly which will add complexity in code and query.

Entity framework provides designer to create domain classes and relationship between those classes while with Micro ORM classes and relationship needs to be constructed manually.

Entity framework provides caching for returning cached result set if a similar request has been made recently. 

What to choose?

Micro ORM

  • If having a requirement to produce the best performance, prefer dapper over Entity Framework.
  • If having a requirement to migrate legacy code of ADO.NET, will help to improve code with minimum changes especially with dapper which provides extension methods to DbConnection.

Entity Framework

  • If want to use relationships for domain classes, to load related objects Entity Framework is preferred.
  • If want to use OData which works over IQueryable and to make it more effective Entity Framework can be used to gain performance.

 Integrate Dapper micro ORM with ASP.NET MVC

  1. Create Student and Address classes
public class Student
    {
        public Student()
        {
            this.Address = new List<Address>();
        }
 
        public int Id { get; set; }
 
        public string FirstName { get; set; }
 
        public string LastName { get; set; }
 
        public string Email { get; set; }
 
        public List<Address> Address { get; set; }
}

public class Address
    {
        public int Id { get; set; }        
        public string AddressType { get; set; }
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }
        public int StudentId { get; set; }
    }

Now Create IStudentRepository.cs interface and StudentRepository.cs classes for data access.

public interface IStudentRepository
    {
        List<Student> GetAll();
        Student Get(int id);
        Student Insert(Student student);
        Student Update(Student student);
        bool Remove(int id);
        Student GetDetail(int id);
    }

public class StudentRepository : IStudentRepository
    {
        private IDbConnection _db;
 
        public StudentRepository()
        {
            this._db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        }
 
        public List<Student> GetAll()
        {
            return this._db.Query<Student>("SELECT S.Id, S.FirstName, S.LastName, S.Email FROM dbo.Student S").ToList();
        }
 
        public Student Get(int id)
        {
            return this._db.Query<Student>("SELECT S.Id, S.FirstName, S.LastName, S.Email FROM dbo.Student S WHERE S.Id = @Id", new { Id = id }).SingleOrDefault();
        }
 
        public Student Insert(Student student)
        {
            var query = "INSERT INTO Student (FirstName, LastName, Email) VALUES(@FirstName, @LastName, @Email); " + "SELECT CAST(SCOPE_IDENTITY() as int)";
            student.Id = this._db.Query<int>(query, student).Single();
            return student;
        }
 
        public Student Update(Student student)
        {
            var query = "UPDATE Student SET FirstName = @FirstName, LastName  = @LastName,  Email = @Email WHERE Id = @Id";
            this._db.Execute(query, student);
            return student;
        }
 
        public bool Remove(int id)
        {
            var affectedRecords = this._db.Execute("DELETE FROM Student WHERE Id = @Id", new { Id = id });
            return affectedRecords == 1;
        }
 
        public Student GetDetail(int id)
        {
            using (var results = this._db.QueryMultiple("dbo.GetStudent", new { Id = id }, commandType: CommandType.StoredProcedure))
            {
                var student = results.Read<Student>().SingleOrDefault();
                var addresses = results.Read<Address>();
 
                if (student != null && addresses != null)
                {
                    student.Address.AddRange(addresses);
                }
 
                return student;
            }
        }

Use above repository in ASP.NET MVC StudentController and create an instance for StudentRepository

public class StudentController : Controller
    {
        private IStudentRepository _studentRepository;
 
        public StudentController()
        {
            this._studentRepository = new StudentRepository();
        }
 
        // GET: /Student/Detail/1
        public ActionResult Detail(int id)
        {
            return this.View(this._studentRepository.GetDetail(id));
        }
    }

 

Similarly insert, update and delete can be implemented in ASP.NET MVC using student repository of dapper micro ORM data access layer.