Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resharper Refactor field of type T to Lazy<T>

Is there an easy way to refactor a field/property of type T to be a Lazy, and replace all useages of that field to use FieldName.Value instead?

I have a controller with a bunch of dependencies that are stored in private backing fields, but only a few of the dependencies are need on any given function call. In order to speed up construction of this controller, I'd like to make all the dependencies Lazy, but its just an irritating amount of work to do by hand. Any advice?

like image 789
hermitt Avatar asked Apr 27 '14 18:04

hermitt


2 Answers

  1. Tools > Create GUID

  2. Resharper > Refactor > Rename

    Foo => Foo_299E4ADB-5770-458C-B030-E40E19B0FFAF

  3. Edit > Find and Replace > Replace in Files

    _299E4ADB-5770-458C-B030-E40E19B0FFAF => .Value

like image 174
Ilya Kozhevnikov Avatar answered Nov 05 '22 05:11

Ilya Kozhevnikov


if previously you had,

public class LazyClass
{
 // initialized in constructor etc.
 public MyType MyTypeProperty { get; set; }
}

then you can make it lazy loaded, without affecting the callers as follows:

public class LazyClass
{
 private Lazy<MyType> myType = new Lazy<MyType>(() => new MyType());

 public MyType MyTypeProperty
 {
  get { return this.myType.Value; }
 }
}

i doubt if Resharper has this re-factoring built in. anyways, you don't want all callers to know it is Lazy and refer to it as LazyType.Value. don't expose the Lazy type.

like image 2
Raja Nadar Avatar answered Nov 05 '22 06:11

Raja Nadar