Domanda

I am trying to get rid of dead code in our codebase using NDepend. Since we are using dependency injection I want to find interfaces (and the implementations) only used in classes that deriving from the registry base:

public class PresenterRegistry : Registry
{
    public PresenterRegistry()
    {
        For<IExamplePresenter>().Use<ExamplePresenter>();
    }
}

Is there any way to do this?

Thanks!

È stato utile?

Soluzione

After trying a little bit I created a query that is working in the way I need:

// <Name>Interfaces registered but potentially not used</Name>
warnif count > 0 
from t in JustMyCode.Types
from i in JustMyCode.Types
where t.DeriveFrom("StructureMap.Configuration.DSL.Registry")
   && i.IsInterface
   && t.IsUsing(i)
   && i.NbTypesUsingMe < 3 // one using for implementation, one in registry
select i

Not as much code as I expected :-) This query isn't cover any eventuality but it is a good start.

Nevertheless: Patrick, thanks for your help!


You are welcome Rico :) Btw, this code rule can be rewritten this way to be ran in O(N) instead of O(N^2) (N being the number of JustMyCode.Types). This optimization is achieved thanks to the magic UsedByAny() method. This rule also provides a more detailed result.

warnif count > 0 
let registryDerived = JustMyCode.Types.Where(t => t.DeriveFrom("StructureMap.Configuration.DSL.Registry"))
from i in JustMyCode.Types.UsedByAny(registryDerived)
where i.IsInterface &&
      i.NbTypesUsingMe < 3 // one using for implementation, one in registry
select new { i, 
             registryDerivedUser = i.TypesUsingMe.Intersect(registryDerived),
             i.TypesUsingMe }

Altri suggerimenti

I am not sure to understand what you are asking for.

I want to find interfaces (and the implementations) only used in classes that deriving from the registry base: The following query matches application interfaces and classes used by any type that derive from Microsoft.Win32.Registry:

let registryDerived = Application.Types.Where(t => t.DeriveFrom("Microsoft.Win32.Registry"))
from t in Application.Types.UsedByAny(registryDerived)
select t

..and with the next query, you also get derived types and implementations of interfaces, matched in the previous query:

let registryDerived = Application.Types.Where(t => t.DeriveFrom("Microsoft.Win32.Registry"))
from t in Application.Types.UsedByAny(registryDerived)
let tDerived = t.DerivedTypes
let tImpl = t.TypesThatImplementMe
select new { t, tDerived, tImpl }

... or to list them all:

let registryDerived = Application.Types.Where(t => t.DeriveFrom("Microsoft.Win32.Registry"))

let tUsed = Application.Types.UsedByAny(registryDerived)
let tDerived = tUsed.SelectMany(t => t.DerivedTypes)
let tImpl = tUsed.SelectMany(t => t.TypesThatImplementMe)

from t in tUsed.Union(tDerived).Union(tImpl)
select t

Btw, Microsoft.Win32.Registry is sealed, so it is not class you are talking about. But you can replace it with your own class name, prefixed with namespace.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top