NDpend CQLinq to signal code rule

38 Views Asked by At

I'm a new customer of NDpend, so please excuse me my question is stupid...I wish to have a rule that shows me when I've got a constructor has a particular line of code. Consider that example

public StatusViewModel
{
   this.userService = ServiceLocator.Default.ResolveType<IUserService>();
}

Is it possible? considering that I have it to be defined in the Constructor and need to have a reference to ServiceLocator.Default.ResolveType

Thanks

1

There are 1 best solutions below

0
Patrick from NDepend team On

If you want to enforce that the constructor is accessing

ServiceLocator.Default.ResolveType<IUserService>()

The rule should look like

warnif count > 0
from m in Application.Methods
where m.IsConstructor 
&& !m.IsUsing("Microsoft.Practices.ServiceLocation.ServiceLocator.get_Default()") 
&& !m.IsUsing("Microsoft.Practices.ServiceLocation.ServiceLocator.ResolveType<T>()")
select m