dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.73k stars 3.99k forks source link

Detecting Framework Related Anti-patterns using Roslyn #24994

Open mirsaeedi opened 6 years ago

mirsaeedi commented 6 years ago

I want to develop an analyzer to automatically identify entity framework related antipatterns. For example, the following is an example of loading redundant columns.

File: DataContext.cs
/// returns all users with all columns
public Collection<Student> GetTopStundets()
{
            return dbContext.Students.ToArray();
}
File: View.cs
/// Print Top Students' Names
public Collection<Student> GetTopStundets()
{
            var dataContext = new DataContext();
            var students = dataContext.GetTopStudents(); 
            foreach(vat student in students)
            {
                     Console.WriteLine(student.Name);
            }
}

As you can see we are loading all columns while we are using only the Name column. Could you please help how can I identify such patterns by employing CFG and DFG graphs? Is it possible to detect these kinda patterns in code using Roslyn?

mirsaeedi commented 6 years ago

Hi @sharwell. Would you please give me a hint on this question? is it doable?

sharwell commented 6 years ago

The CFG is a new feature. @mavasani should be able to provide information about its current state. 😄

mavasani commented 6 years ago

@mirsaeedi We do not have CFG/DFA APIs currently for you to write such an analyzer. We are working towards exposing an IOperation based CFG in near future and build upon that to expose a DFA framework to write custom dataflow analyses to track custom information. We will keep you in loop...