Wednesday, December 28, 2011

Introduce NValidator

I had spent 2 weeks before Christmas to develop a small library to do the validation. It's a lot similar to FluentValidation in syntax. Actually, the implementation and fluent syntax are inspired from this well-known library Fluent Validation. However, I implemented it using Chain of Reponsibility pattern and made it support Dependency Injection for validator classes. Here is a simple example that can help you to start: public...

Monday, November 21, 2011

Validation attribute on nested list item

I'm working on some data annotation validation stuff and today I came to a requirement to validate a type nested property which is a list of string. The requirement is quite simple, just make sure every string in the list must be numeric. NumericAttribute is a custom ValidationAttribute to validate a string and to check whether it contains only numeric. Well this is a very basic example and I'm thinking futher, may be I'll need...

Monday, November 7, 2011

WCF Validation Engine with Data Annotation style like ASP.NET MVC

    There are many articles about WCF validation. Some are about using WCF with Microsoft Application Block, many others are about using Data Annotation. But there is not any thing like the validation engine of ASP.NET MVC. Honestly, I'm very impressed of MVC Team's work. The code is very SOLID that makes it too easy to extend without changing the implementation. There are still something i don't like in the...

Saturday, October 29, 2011

Autofac, AutoMapper and custom converter with dependency injection

Whenever I work with a library or a technology, one of the question in my mind is how good is it to support dependency injection. I came back to use Autofac in a company's project recently and that question is the one I need to find the answer. Basically, Autofac is a pretty cool library to help converting between domain objects and view models. For example: Mapper.CreateMap<Order, OrderViewModel>(); Or even better: Mapper.CreateMap<Order,...

Friday, September 23, 2011

Combine logic of MVC FilterAttribute classes

    Hi mates, today I would like to write another blog about the reason why I need an Autofac IContainer as the dependency in some of my classes. In my team, we are developing a website that can allow users create and manage information about organisations. We have frontend pages for normal users to manage their organisations and we also have backend pages for the administrator to do that. So on frontend pages,...

Thursday, September 22, 2011

Resolve Autofac IContainer Dependency Injection

    Today, i come to a need of using Autofac IContainer somewhere in my code. I know it's a bad practice using IContainer this way but i'm pretty sure sometime we must violate the convention. I'll show you the reason and the case on next post. Now i just say about how to have the IContainer itself resolvable in some of the classes. I guess perhaps that's the intention of the Autofac author not to make the IContainer...

Thursday, August 25, 2011

Yet another way programming to the Interface In JavaScript, sort of

    i have just blogged about inheritance in Javascript, so next target would be programming to the interface.     I had two approaches in mind. The first solution is making an interface as a normal object that contains properties and methods. This object will be static and remain unchanged during the lifetime of the application. The second solution is making the interface using the same...

Wednesday, August 24, 2011

Yet another way for Javacript class inheritance, sort of

    Recently, I'm so interested in Node.js and i'm planning to do something cool with that new stuff. I have .NET background so i would expect I can write Javascript in OOP style or at least I want to create classes with inheritance support. Again, as usual, I did some googling :D. That leads me to following blog: http://ejohn.org/blog/simple-javascript-inheritance/     That is an elegant...

Saturday, August 20, 2011

MVC Donut hole caching for Razor View

    I was interested with Donut caching for a while and today I have a need for the Donut hole caching or partial view caching. As many of us, I tried to find whether some smart guys had solved this problem. And it seems like Mr.Haacked had mentioned this in his post. However, that solution is for ASP.NET view engine....

Thursday, July 28, 2011

Unit Test DisplayFormat attribute HtmlHelper

    Previously, I wrote about how to mock a ViewEngine for one of my unit tests. Today, I came across a question on StackOverflow asking about how to unit test the out put of DataAnnotation attribute DisplayFormat. I'm very interested in Unit Test so I decide to find the answer, another reason is to increase my reputation on StackOverflow :D. Honestly, I don't exactly know the reason why to test the output...

Tuesday, July 19, 2011

Save some lines for your Razor view

We all know that we should not put logic in the MVC view. If we have to do something base on a complex condition, we should better make a HtmlHelper method for that logic. In an attempt to remove some simple if/else statement on the view by using some extention method. Let's say we have something like below in the view: @if (!Request.IsAuthenticated) { Html.RenderPartial("LoginBox"); } I attempt to make it look like:   @{...

How to mock UrlHelper?

Sometime, you need to write unit test for an action method that needs a UrlHelper. But I'm sure you will never can mock that stuff since it's a concrete class and it's methods are not virtual. Let's say we have following action method and want to test it: [Authorize] public ActionResult LogOnAs(Guid userId, string returnUrl) { // Logic to login user by Id ... if (Url.IsLocalUrl(returnUrl)) { return Redirect(returnUrl); ...

Wednesday, July 6, 2011

Never give a chance to make something wrong

Recectly, I've penetrated the truth that if you don't want others do something (bad), never ever give them a chance to do that. It's true in both my real life and in programming. Since my blog is just about software programming, I would like to write about some funny mistakes that people often make. Well, I did make some of them in the past :D 1/ You may forget about the ability of the constructor. Let's look at following code: public...

Wednesday, June 1, 2011

Basic Unit Of Work Implementation

    I bet you must have heard about Unit Of Work. If you have not, you can find the discription here : http://martinfowler.com/eaaCatalog/unitOfWork.html.    You can change the database with each change to your object model, but this can lead to lots of very small database calls, which ends up being very slow. Furthermore it requires you to have a transaction open for the whole interaction,...

Monday, May 30, 2011

Some issues with BDD Specflow & WatIn

    Last few weeks, our team is doing alot of experiments on BDD. We read several books trying to find out the proper way to apply BDD in .NET project using Specflow and WatIn. There are some good articles out there indeed but for me, they're quite basic. These articles do the good works to help me start the first steps but after walking several steps, some issues comes up: - What is the good way to initialize...

Thursday, May 12, 2011

Defining Custom Functions in Entity Framework

Well, I's been 1 month since my last post. Today, I'm gonna write something about the well-known not supported issue of Entity Framework. I intended to write a LinQ to Entity query that would require a comparison and conversion between uniqueidentifier and string value in SQL database. The query looks like this: var items = from x in XRepository.GetAll() join y in YRepository.GetAll() on x.UniqueId.ToString() equals...

Friday, April 22, 2011

MVC RouteConstraint is the culprite

Today, several of our live product sites got a serious issue. The page that renders a list of businesses took over 1 minutes to render. We couldn't find out the problem in serveral hours. After a while keep investigating, we found that the call of below method took about 6 seconds to run:  UrlHelper.Action("action", "controller", RouteValueCollection)   That means on a page that contains 10 links to the business detail...

Tuesday, April 19, 2011

Optional Parameter Is Ugly

Optional Parameter has been introduced since C# 4.0. It's a nice feature indeed. Let's say we wanted to create a Html Helper method that can generate a Html markup of a link by providing the inner Html, controller and action name. The method would look like: HtmlLink(string innerHtml, string action, stirng controller) Definetily, there would be a requirement to provide a routeValue object to make the link in some cases like...