Software Outsourcing Company India - RushKar

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

NAVIGATION - SEARCH

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.