Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've taken the approach of creating a new method, <code>ProcessDriveInfo</code>, which is called by your two original methods. These two methods fetched a list of drives, created a server, did something with the server, and did something for each drive. I moved the first two steps into the <code>ProcessDriveInfo</code>, and passed in actions for the last two steps.</p> <p>The first action (i.e. method) does something with the server, and the second action does something for a single <code>driveInfo</code>. </p> <p>n.b. I've "answered" the question in that it removes the duplication, but it could be argued that it is less readable.</p> <pre><code> public Server ProcessDriveInfo(Action&lt;Server&gt; initialAction, Action&lt;Server, DriveInfo&gt; driveInfoAction) { var driveList = DriveInfo.GetDrives().Where(x =&gt; x.IsReady).ToList(); var server = new Server(); initialAction(server); driveList.ForEach(dl =&gt; driveInfoAction(server, dl)); return server; } public void FakeDriveInfo() { ProcessDriveInfo(WriteServerToConsole, WriteDriveInfoToConsole); } private void WriteServerToConsole(Server server) { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Server ID : {0}", server.ServerID = 0); Console.WriteLine("Server Name : {0}", server.ServerName = string.Concat(System.Environment.MachineName)); Console.WriteLine(); } private void WriteDriveInfoToConsole(Server server, DriveInfo t) { Console.WriteLine("Drive Letter: {0}", t.Name); Console.WriteLine("Total Size: {0}", FormatBytes(t.TotalSize)); Console.WriteLine("Volume Label: {0}", t.VolumeLabel); Console.WriteLine("Free Space: {0}", FormatBytes(t.TotalFreeSpace)); Console.WriteLine("Drive Format: {0}", t.DriveFormat); Console.ReadLine(); } public void RealDriveInfo() { var server = ProcessDriveInfo(InitialiseServer, WriteDriveInfoToServer); //Add the information to an SQL Database using Linq. var db = new DataClasses1DataContext(@"sqlserver"); // db.Servers.InsertAllOnSubmit(server); db.Servers.InsertOnSubmit(server); db.SubmitChanges(); } private static void InitialiseServer(Server server) { // Insert information of one server - You will need get information of all servers server.ServerID = 0; // Here is necessery put PK key. I recommend doing the SQL server will automatically generate the PK. server.ServerName = Environment.MachineName; } private static void WriteDriveInfoToServer(Server server, DriveInfo t) { var serverDrives = new ServerDrive { DriveLetter = t.Name, TotalSpace = t.TotalSize, DriveLabel = t.VolumeLabel, FreeSpace = t.TotalFreeSpace, DriveType = t.DriveFormat }; server.ServerDrives.Add(serverDrives); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload