суббота, 30 января 2016 г.

My tricky .NET interview question

At last interview i was asked the question: suppose you have two classes - base and derived, base class has virtual method and call it in constructor. Derived class overrides this virtual method and writes to console an argument, that was passed to its constructor. Then some hosting code creates instance of Derived and the question is: what will be written on the screen?

I think everyone was asked such question at least once. But i found very interesting approach to complicate this question. What if we define readonly field for derived class and write it to console to?

воскресенье, 24 января 2016 г.

Inspired by Agile!

At this thursday i was at Inspired by Agile meetup. It was a great experience: great people, great questions, great place. One hundred people talked about Agile and helped each other. In my opinion the most interesting question was: "Can Agile command open salaries of its members?". I think such command show us very high degree of trust between its members. What do you think?

PS. The next one will be in February.

суббота, 16 января 2016 г.

The correct way of assigning the CurrentPrincipal in WEB API 2.0

There're two places where you can set current principal in WEB API:
  1. Thread.CurrentPrincipal
  2. HttpContext.Current
In WEP API 1.0 you need to set them depending on the used host: in case of self-host, only Thread.CurrentPrincipal should be set, but in case of web hosting you need to set them both.
The common host independent approach of assigning current principal was this one:
 Thread.CurrentPrincipal = principalToAssign;   
 if (HttpContext.Current != null)   
 {    
   HttpContext.Current.User = principalToAssign;   
 }  
But it has some flaws:
  • Null cheking is a lack of abstraction
  • In case of self-host scenario you have dependency from System.Web assembly
In WEB API 2.0 these flaws were eliminated. You should use HttpRequestContext class which has several implementations for different hosts. Every implementation  provide its own logic of assigning current principal. You only need to set Principal property. For example, this is the implementation of Principal property of WebHostHttpRequestContext class used in web host:
 public override IPrincipal Principal  
 {  
   get  
   {  
     return this._contextBase.User;  
   }  
   set  
   {  
     this._contextBase.User = value;  
     Thread.CurrentPrincipal = value;  
   }  
 }  
So, in WEB API 2.0 you should authenticate your users in message handler as follows:
 protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)  
 {  
       request.GetRequestContext().Principal = AuthenticateRequest(request);  
       return await base.SendAsync(request, cancellationToken);  
 }  



воскресенье, 31 августа 2014 г.

Некоторые тонкости GetHashCode

При чтении "Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries" натолкнулся на такую фразу: 

"Ensure that GetHashCode returns exactly the same value regardless of any changes that are made to the object".

Хм.... подумал я, о чем это они? Перед глазами всплыла стандартная реализация, которая генерируется ReSharper'ом и я осознал, что генерируемое значение не будет  постоянным на протяжении жизни объекта при его изменениях.