This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FakeHttpMessageHandler : HttpMessageHandler | |
{ | |
public virtual HttpResponseMessage Send(HttpRequestMessage request) | |
{ | |
throw new NotImplementedException("Implement this method through Moq Setup method inside your tests."); | |
} | |
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) | |
{ | |
return Task.FromResult(Send(request)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The ASP.NET MVC framework supports four different types of filters: | |
Authorization – Implements IAuthorizationFilter Attribute. | |
Action – Implements IActionFilter Attribute. | |
Result – Implements IResultFilter Attribute. | |
Exception – Implements IExceptionFilter Attribute. | |
Note: Filters are executed in the order listed above. | |
Authorization filters run very early in the action pipeline. | |
They are good for example to escape from the action pipeline when conditions are not met. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
An expression simply turns a delegate into a data about itself. So a => a + 1 becomes something like "On the left side there's an int a. On the right side you add 1 to it." | |
That's it. You can go home now. Expression allows you to look inside the delegate and see everything it's wanting to do, empowering you to translate it into whatever you want, like a SQL query. | |
Func for example won't work for DbContext above because DbContext itself is blind to what is actually in the lambda expression and will not turn it into SQL, so it will do the next best thing and will iterate that conditional through each row in the table. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public IEnumerable<T> Get(Func<T, bool> conditionLambda){ | |
using(var db = new DbContext()){ | |
return db.Set<T>.Where(conditionLambda); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here are several benefits of IHttpActionResult over HttpResponseMessage mentioned in Microsoft ASP.Net Documentation: | |
Simplifies unit testing your controllers. | |
Moves common logic for creating HTTP responses into separate classes. | |
Makes the intent of the controller action clearer, by hiding the low-level details of constructing the response. | |
But here are some other advantages of using IHttpActionResult worth mentioning: | |
Respecting single responsibility principle: cause action methods to have the responsibility of serving the HTTP requests and does not involve them in creating the HTTP response messages. | |
Useful implementations already defined in the System.Web.Http.Results namely: Ok NotFound Exception Unauthorized BadRequest Conflict Redirect InvalidModelState (link to full list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Person{ | |
public String Name{get; set;} | |
public String Email {get; set;} | |
public virtual Employer employer {get; set;} | |
} | |
public List<Person> GetPerson(){ | |
using(DbEntities db = new DbEntities()){ | |
return db.Person.ToList(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
User user = dbContext.Users.FirstOrDefault(a => a.UserId == userId); | |
UserDeatils userDetails = user.UserDetails; /* UserDetails are loaded here */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
There are options to disable Lazy Loading in an Entity Framework. | |
After turning Lazy Loading off, you can still load the entities by explicitly calling the Load method for the related entities. | |
*/ | |
User usr = dbContext.Users.FirstOrDefault(a => a.UserId == userId); | |
dbContext.Entry(usr).Reference(usr => usr.UserDetails).Load(); | |
/* | |
There are two ways to use Load method: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private readonly Func<InternalMediaItemDto> _internalMediaItemDtoFactory = () => Builder<InternalMediaItemDto> | |
.CreateNew() | |
.With(x => x.MediaItem = Builder<MediaItem>.CreateNew().Build()) | |
.Build(); | |
private readonly Func<int, IEnumerable<BundleOffer>> _bundleOffersFactory = x => Builder<BundleOffer> | |
.CreateListOfSize(x) | |
.Build(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void AllControllers_ShouldDerivedFromBaseController() | |
{ | |
var itemsToVerify = new Types(Types.InAssemblyOf<BaseController>().Where(c => c.IsSubclassOf(typeof(Controller)) && !c.IsAbstract).ToList(), "ControllersTests"); | |
TestStack.ConventionTests.Convention.Is(new MvcControllerNameAndCustomBaseClassConvention(), itemsToVerify); | |
} | |
public class MvcControllerNameAndCustomBaseClassConvention : IConvention<Types> | |
{ |
OlderNewer