I am new to C#. I have been trying to write a xUnit Test for the method below ("ExecuteExportCsvCommand()"). However, it has some UI control as you can see below.
Therefore, I don't know how to test the function. If anyone can explain, I would be really thankful.
public void ExecuteExportCsvCommand()
{
ExecuteCancelPopOutWindowCommand();
var previousState = View.IsEnabled;
View.IsEnabled = false;
try
{
var exportDialog = new ExportPrintDataDialog();
var result = exportDialog.ShowDialog();
if (result.GetValueOrDefault())
{
var projectData = _projectService.GetProject3dObject(Project.Id);
IEnumerable<object> orderedList;
orderedList = projectData.Select((x, i) => new ExportData {
Index = i,
Time = x.Time,
LayerNumber = x.LayerNumber,
X = x.X,
Y = x.Y,
Z = x.Z,
ArcVoltage = x.ArcVoltage,
Current = x.Current,
WireFeedSpeed = x.WireFeedSpeed,
WireFeedSpeedB = x.WireFeedSpeedB,
RatioWireFeedSpeed = (x.WireFeedSpeed == 0) ? 0 : x.WireFeedSpeedB / x.WireFeedSpeed,
BuildHeight = x.WallHeight,
LeadTemperature = x.LeadTemperature,
TrailTemperature = x.TrailTemperature,
OxygenLevel = x.OxygenLevel,
GasFlowA = x.GasFlow.First(),
GasFlowB = x.GasFlow.Last(),
TempChannels = x.TempChannels //preferably last as array of 8 values
}).ToList();
//Define Column headers
var heightHeader = _options.CorriOptions.Value.UseWallHeight ? "Wall Height" : "Layer Height";
var wfsHeader = Project.ProcessEnum == EnumProjectProcess.CW_MIG
? new []{ "Hot Wire Feed Speed", "Cold Wire Feed Speed" }
: new[] { "Wire Feed Speed 1", "Wire Feed Speed 2" };
var headers = new Dictionary<string, string>()
{
{ "Index" , "Ordinal Number" },
{ "Time" , "Time" },
{ "LayerNumber" , "Layer Number" },
{ "X" , "Pos X" },
{ "Y" , "Pos Y" },
{ "Z" , "Pos Z" },
{ "ArcVoltage" , "Arc Voltage" },
{ "Current" , "Current" },
{ "WireFeedSpeed" , wfsHeader.First() },
{ "WireFeedSpeedB" , wfsHeader.Last() },
{"RatioWireFeedSpeed", "R-Ratio"},
{ "BuildHeight" , heightHeader },
{ "LeadTemperature" , "Lead Temperature" },
{ "TrailTemperature" , "Trail Temperature" },
{ "OxygenLevel" , "Oxygen Level" },
{ "GasFlowA" , "GasFlow Channel A" },
{ "GasFlowB" , "GasFlow Channel B" },
{ "TempChannels" , "End Effector Temperature (Channels 1 - 8)" }
};
var saveFileDialog = new SaveFileDialog
{
Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*",
FilterIndex = 1,
RestoreDirectory = true,
FileName = string.Concat(DateTime.Now.ToString("yyyy-MM-ddTHHmm"), "_log"),
};
if (saveFileDialog.ShowDialog().GetValueOrDefault())
{
using var writer = new StreamWriter(saveFileDialog.FileName);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.Context.RegisterClassMap(new ExportDataMapper(headers));
csv.WriteRecords(orderedList);
}
}
}
finally
{
View.IsEnabled = previousState;
ExportDataBtnChecked = false;
}
}
The test environment was set up already as you can see below. I just have to write the test for the method mentioned. If you need further details, please let me know. thanks.
public class ProjectPrintViewModelTests
{
private readonly ProjectPrintViewModel _sut;
private readonly IHost _host;
private readonly Mock<IPermissionChecker> _permissionChecker = new Mock<IPermissionChecker>();
private readonly Mock<ILogService> _logService = new Mock<ILogService>();
private readonly Mock<IAuditService> _auditService = new Mock<IAuditService>();
private readonly Mock<IProjectService> _projectService = new Mock<IProjectService>();
public ProjectPrintViewModelTests()
{
var colorList = new List<string> {
"DefaultMinColor",
"DefaultLowColor",
"DefaultNormColor",
"DefaultHighColor",
"DefaultMaxColor"
};
var app = new Application();
colorList.ForEach(x => app.Resources.Add(x, Color.FromRgb(255, 255, 255)));
_host = MockEnvironment.BuildHost();
IoCContainer.SetServiceProvider(_host.Services);
var options = _host.Services.GetRequiredService<OptionsContainer>();
var valueService = _host.Services.GetRequiredService<IValuePointService>();
var project = new ProjectViewModel(options);
_sut = new ProjectPrintViewModel(
options,
project,
_logService.Object,
_permissionChecker.Object,
_auditService.Object,
_projectService.Object,
valueService,
false);
}
/// Write Tests Below Here
[Fact]
public void ExecuteCsvFileCommand_WhenValueAreValid()
{
//Assemble
//Act
_sut.ExecuteExportCsvCommand();
//Assert
This is one of the many disadvantages of having poor design and non-clean architecture.. Separating UI code from business logic helps you in testing your business logic well.
Keep in mind that unit tests should run/return as fast as possible. Apparently, showing a SaveFileDialog will stop the test until a user selects the path, this is wrong!
You should pull all UI-related code out to a separate component that delivers UI services, For example:
public interface IUiServices
{
void CancelPopOutWindow();
void SaveViewState();
void RestoreViewState();
bool ShowExportDialog();
string ShowSaveFileDialog();
}
The most important part is, when testing, you'd mock the UIServices to not do any real UI work (Ex. opinging SaveFileDialog, it will just return a valid test path to save files in there).. So ExecuteExportCsvCommand can be look like this..
public void ExecuteExportCsvCommand()
{
_uiServices.CancelPopOutWindow();
_uiServices.SaveViewState();
try
{
var b = _uiServices.ShowExportDialog();
if (b)
{
var projectData = _projectService.GetProject3dObject(Project.Id);
IEnumerable<object> orderedList;
orderedList = projectData.Select((x, i) => new ExportData {
Index = i,
Time = x.Time,
LayerNumber = x.LayerNumber,
X = x.X,
Y = x.Y,
Z = x.Z,
ArcVoltage = x.ArcVoltage,
Current = x.Current,
WireFeedSpeed = x.WireFeedSpeed,
WireFeedSpeedB = x.WireFeedSpeedB,
RatioWireFeedSpeed = (x.WireFeedSpeed == 0) ? 0 : x.WireFeedSpeedB / x.WireFeedSpeed,
BuildHeight = x.WallHeight,
LeadTemperature = x.LeadTemperature,
TrailTemperature = x.TrailTemperature,
OxygenLevel = x.OxygenLevel,
GasFlowA = x.GasFlow.First(),
GasFlowB = x.GasFlow.Last(),
TempChannels = x.TempChannels //preferably last as array of 8 values
}).ToList();
//Define Column headers
var heightHeader = _options.CorriOptions.Value.UseWallHeight ? "Wall Height" : "Layer Height";
var wfsHeader = Project.ProcessEnum == EnumProjectProcess.CW_MIG
? new []{ "Hot Wire Feed Speed", "Cold Wire Feed Speed" }
: new[] { "Wire Feed Speed 1", "Wire Feed Speed 2" };
var headers = new Dictionary<string, string>()
{
{ "Index" , "Ordinal Number" },
{ "Time" , "Time" },
{ "LayerNumber" , "Layer Number" },
{ "X" , "Pos X" },
{ "Y" , "Pos Y" },
{ "Z" , "Pos Z" },
{ "ArcVoltage" , "Arc Voltage" },
{ "Current" , "Current" },
{ "WireFeedSpeed" , wfsHeader.First() },
{ "WireFeedSpeedB" , wfsHeader.Last() },
{"RatioWireFeedSpeed", "R-Ratio"},
{ "BuildHeight" , heightHeader },
{ "LeadTemperature" , "Lead Temperature" },
{ "TrailTemperature" , "Trail Temperature" },
{ "OxygenLevel" , "Oxygen Level" },
{ "GasFlowA" , "GasFlow Channel A" },
{ "GasFlowB" , "GasFlow Channel B" },
{ "TempChannels" , "End Effector Temperature (Channels 1 - 8)" }
};
string path = _uiServices.ShowSaveFileDialog();
if (!string.IsNullOrEmpty(path))
{
using var writer = new StreamWriter(path);
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.Context.RegisterClassMap(new ExportDataMapper(headers));
csv.WriteRecords(orderedList);
}
}
}
finally
{
_uiServices.RestoreViewState();
ExportDataBtnChecked = false;
}
}
Related
I am using Chart.Js (version 4.1) in Blazor and have managed to get a simple line graph up and running.
Now I need to add the currency symbol to the y Axis (which displays sales total), at the moment it simple displays a number as 4,222
My current Options and DataSets anonymous types are below, these are sent to Chart.JS via JS Interops. Below is what I have tried so far which fails to include the symbol in the tool tip and y axis.
var config = new
{
Type = Type.ToString().ToLower(),
Options = new
{
Responsive = true,
Scales = new
{
y = new
{
ticks = new
{
color = "white"
},
grid = new
{
color = "white",
borderColor = "white",
drawOnChartArea = false
},
scaleLabel = new
{
display = true,
labelString = "Sales Total £",
fontColor = "white"
}
},
x = new
{
ticks = new
{
color = "white"
},
grid = new
{
color = "white",
borderColor = "white",
drawOnChartArea = false
}
}
},
Tooltips = new
{
Callbacks = new
{
Label = "function(tooltipItem, data) { return '$' + tooltipItem.value; }"
}
}
},
Data = ChartData
};
What do I need to modify above to include the currency symbol in the tooltip and yAxis?
Since v3 the tooltip is located under plugins in options: here
This is how it looks in javaScript:
options:{
...
plugins: {
tooltip: {
callbacks: {
label: function (context) {
if (context.parsed.y !== null) {
return context.dataset.label + ": " +
new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(context.parsed.y);
}
return "";
}
}
}
}
...
}
For the y-axis you also can use a callback:
options:{
...
scales: {
y: {
ticks: {
callback: function (value, index, ticks) {
return new Intl.NumberFormat('de-DE', {
style: 'currency',
currency: 'EUR'
}).format(value);
}
}
}
}
...
}
Attention, note that it is called "tooltip," not "tooltips"!
Hi All i am trying to generate the word document with two different tables included in it, for this purpose i have two similar methods where i am passing word document reference and data object and table to the similar methods..
Now i am looking to make single method in generic way so that in different places i can use single method by passing parameters to it
Method 1 :
private static List<OpenXmlElement> RenderExhaustEquipmentTableDataAndNotes(MainDocumentPart mainDocumentPart, List<ProjectObject<ExhaustEquipment>> exhaustEquipment,Table table)
{
HtmlConverter noteConverter = new HtmlConverter(mainDocumentPart);
var equipmentExhaustTypes = new Dictionary<string, List<ProjectObject<ExhaustEquipment>>>();
foreach (var item in exhaustEquipment)
{
string exhaustEquipmentName = item.TargetObject.Name;
if (!equipmentExhaustTypes.ContainsKey(exhaustEquipmentName))
{
equipmentExhaustTypes.Add(exhaustEquipmentName, new List<ProjectObject<ExhaustEquipment>>());
}
equipmentExhaustTypes[exhaustEquipmentName].Add(item);
}
List<OpenXmlElement> notes = new List<OpenXmlElement>();
int noteIndex = 1;
foreach (var exhaustEquipmentItem in equipmentExhaustTypes)
{
List<string> noteIndices = new List<string>();
for (int exhaustEquipmentConditionIndex = 0; exhaustEquipmentConditionIndex < exhaustEquipmentItem.Value.Count; exhaustEquipmentConditionIndex++)
{
var condition = exhaustEquipmentItem.Value[exhaustEquipmentConditionIndex];
var row = new TableRow();
Run superscriptRun = new Run(new RunProperties(new VerticalTextAlignment { Val = VerticalPositionValues.Superscript }));
if (exhaustEquipmentConditionIndex == 0)
{
row.Append(RenderOpenXmlElementContentCell(new Paragraph(
new List<Run> {
new Run(new RunProperties(), new Text(exhaustEquipmentItem.Key) { Space = SpaceProcessingModeValues.Preserve }),
superscriptRun
}), 1,
new OpenXmlElement[] {new VerticalMerge { Val = MergedCellValues.Restart },new TableCellMargin {
LeftMargin = new LeftMargin { Width = "120" },
TopMargin = new TopMargin { Width = "80" } }
}));
}
else
{
row.Append(RenderTextContentCell(null, 1, null, null, new OpenXmlElement[] { new VerticalMerge { Val = MergedCellValues.Continue } }));
}
row.Append(RenderTextContentCell(condition.TargetObject.IsConstantVolume ? "Yes" : "No"));
row.Append(RenderTextContentCell($"{condition.TargetObject.MinAirflow:R2}"));
row.Append(RenderTextContentCell($"{condition.TargetObject.MaxAirflow:R2}"));
if (condition.TargetObject.NotesHTML?.Count > 0)
{
foreach (var note in condition.TargetObject.NotesHTML)
{
var compositeElements = noteConverter.Parse(note);
var htmlRuns = compositeElements.First().ChildElements.Where(c => c is Run).Cast<Run>().Select(n => n.CloneNode(true));
notes.Add(new Run(htmlRuns));
noteIndices.Add(noteIndex++.ToString(CultureInfo.InvariantCulture));
}
}
if (exhaustEquipmentConditionIndex == exhaustEquipmentItem.Value.Count - 1 && condition.TargetObject.NotesHTML?.Count > 0)
{
superscriptRun.Append(new Text($"({String.Join(',', noteIndices)})") { Space = SpaceProcessingModeValues.Preserve });
}
table.Append(row);
}
}
List<OpenXmlElement> notesSection = new List<OpenXmlElement>();
List<OpenXmlElement> result = RenderNotesArray(table, notes, notesSection);
return result;
}
and I am calling this method like this in below
var table = new Table(RenderTableProperties());
table.Append(new TableRow(
RenderTableHeaderCell("Name"),
RenderTableHeaderCell("Constant Volume"),
RenderTableHeaderCell("Minimum Airflow", units: "(cfm)"),
RenderTableHeaderCell("Wet Bulb Temperature", units: "(cfm)")
));
body.Append(RenderExhaustEquipmentTableDataAndNotes(mainDocumentPart, designHubProject.ExhaustEquipment, table));
Method 2:
private static List<OpenXmlElement> RenderInfiltrationTableData(MainDocumentPart mainDocumentPart, List<ProjectObject<Infiltration>> infiltration,Table table)
{
HtmlConverter noteConverter = new HtmlConverter(mainDocumentPart);
var nameByInflitrationObject = new Dictionary<string, List<ProjectObject<Infiltration>>>();
foreach (var infiltrationData in infiltration)
{
string infiltrationName = infiltrationData.TargetObject.Name;
if (!nameByInflitrationObject.ContainsKey(infiltrationName))
{
nameByInflitrationObject.Add(infiltrationName, new List<ProjectObject<Infiltration>>());
}
nameByInflitrationObject[infiltrationName].Add(infiltrationData);
}
List<OpenXmlElement> notes = new List<OpenXmlElement>();
int noteIndex = 1;
foreach (var inflitrationDataItem in nameByInflitrationObject)
{
List<string> noteIndices = new List<string>();
for (int inflitrationNameIndex = 0; inflitrationNameIndex < inflitrationDataItem.Value.Count; inflitrationNameIndex++)
{
var dataItem = inflitrationDataItem.Value[inflitrationNameIndex];
var row = new TableRow();
Run superscriptRun = new Run(new RunProperties(new VerticalTextAlignment { Val = VerticalPositionValues.Superscript }));
if (inflitrationNameIndex == 0)
{
row.Append(RenderOpenXmlElementContentCell(new Paragraph(
new List<Run> {
new Run(new RunProperties(), new Text(inflitrationDataItem.Key) { Space = SpaceProcessingModeValues.Preserve }),superscriptRun
}), 1,
new OpenXmlElement[] {new VerticalMerge { Val = MergedCellValues.Restart },new TableCellMargin {
LeftMargin = new LeftMargin { Width = "120" },
TopMargin = new TopMargin { Width = "80" }}
}));
}
else
{
row.Append(RenderTextContentCell(null, 1, null, null, new OpenXmlElement[] { new VerticalMerge { Val = MergedCellValues.Continue } }));
}
row.Append(RenderTextContentCell($"{dataItem.TargetObject.AirflowScalar.ToString("R2", CultureInfo.CurrentCulture)} cfm {EnumUtils.StringValueOfEnum(dataItem.TargetObject.InfiltrationCalculationType).ToLower(CultureInfo.CurrentCulture)}"));
if (dataItem.TargetObject.NotesHTML?.Count > 0)
{
foreach (var note in dataItem.TargetObject.NotesHTML)
{
var compositeElements = noteConverter.Parse(note);
var htmlRuns = compositeElements.First().ChildElements.Where(c => c is Run).Cast<Run>().Select(n => n.CloneNode(true));
notes.Add(new Run(htmlRuns));
noteIndices.Add(noteIndex++.ToString(CultureInfo.InvariantCulture));
}
}
if (inflitrationNameIndex == inflitrationDataItem.Value.Count - 1 && dataItem.TargetObject.NotesHTML?.Count > 0)
{
superscriptRun.Append(new Text($"({String.Join(',', noteIndices)})") { Space = SpaceProcessingModeValues.Preserve });
}
table.Append(row);
}
}
List<OpenXmlElement> notesSection = new List<OpenXmlElement>();
List<OpenXmlElement> result = RenderNotesArray(table, notes, notesSection);
return result;
}
and then i am calling this method here like as below
var table = new Table(RenderTableProperties());
table.Append(new TableRow(
RenderTableHeaderCell("Type"),
RenderTableHeaderCell("Air Flow")
));
body.Append(RenderInfiltrationTableData(mainDocumentPart, designHubProject.Infiltration, table));
i know these are lots of lines but is there any generic way to use single method out of these two similar methods and i am using .net core
Could any one please suggest any idea or suggestion how can i refactor these two methods into single method that would be very grateful.
many thanks in advance
Before we can create a single function that handles both types, achieving the highly laudable goal of removing gratuitous duplication, we should clean the code up to make it easier to see which parts, if any, are different between the two nearly identical methods. And there is a lot to clean up, even if we only had one function.
In short, your functions are too long, having too much much code in one place, and in fact too much code altogether.
In the following, the original code has been broken down into multiple functions with specific purposes and refactored to remove DIY nonsense in favor of the standard library functions and the removal of pointless code.
static IEnumerable<OpenXmlElement> RenderExhaustEquipmentTableDataAndNotes(MainDocumentPart mainDocumentPart, List<ProjectObject<ExhaustEquipment>> exhaustEquipment, Table table)
{
var equipmentByType = exhaustEquipment.ToLookup(item => item.TargetObject.Name);
List<OpenXmlElement> notes = new List<OpenXmlElement>();
foreach (var items in equipmentByType)
{
Run superscriptRun = CreateSuperScriptRun();
foreach (var item in items)
{
var row = new TableRow();
if (item == items.First())
{
row.Append(CreateFirstRowStartingCell(items.Key, superscriptRun));
}
else
{
row.Append(RenderTextContentCell(null, 1, null, null, new[] {
new VerticalMerge { Val = MergedCellValues.Continue }
}));
}
row.Append(RenderTextContentCell(item.TargetObject.IsConstantVolume ? "Yes" : "No"));
row.Append(RenderTextContentCell($"{item.TargetObject.MinAirflow:R2}"));
row.Append(RenderTextContentCell($"{item.TargetObject.MaxAirflow:R2}"));
table.Append(row);
var itemNotes = ParseNotes(mainDocumentPart, item.TargetObject.NotesHTML);
if (item == items.Last() && itemNotes.Any())
{
UpdateSuperScript(superscriptRun, itemNotes);
}
notes.AddRange(itemNotes);
}
}
List<OpenXmlElement> result = RenderNotesArray(table, notes, new List<OpenXmlElement>());
return result;
}
private static Run CreateSuperScriptRun()
{
return new Run(new RunProperties(new VerticalTextAlignment
{
Val = VerticalPositionValues.Superscript
}));
}
private static void UpdateSuperScript(Run superscriptRun, IEnumerable<OpenXmlElement> notes)
{
superscriptRun.Append(new Text($"({string.Join(",", Enumerable.Range(0, notes.Count()))})")
{
Space = SpaceProcessingModeValues.Preserve
});
}
private static IEnumerable<OpenXmlElement> ParseNotes(MainDocumentPart mainDocumentPart, IEnumerable<OpenXmlElement> notes)
{
return notes == null
? Enumerable.Empty<OpenXmlElement>()
: notes.Select(note => new HtmlConverter(mainDocumentPart).Parse(note))
.Select(note => note.First().ChildElements
.OfType<Run>()
.Select(n => n.CloneNode(true))).Select(htmlRuns => new Run(htmlRuns))
.ToList();
}
private OpenXmlElement CreateFirstRowStartingCell(string key, Run superscriptRun)
{
return RenderOpenXmlElementContentCell(
new Paragraph(new List<Run> {
new Run(new RunProperties(), new Text(key) { Space = SpaceProcessingModeValues.Preserve }),
superscriptRun
}),
1,
new OpenXmlElement[] {
new VerticalMerge { Val = MergedCellValues.Restart },
new TableCellMargin { LeftMargin = new LeftMargin { Width = "120" }, TopMargin = new TopMargin { Width = "80" } }
});
}
Now, let's tackle the second function:
static IEnunumerable<OpenXmlElement> RenderInfiltrationTableData(MainDocumentPart mainDocumentPart, IEnunumerable<ProjectObject<Infiltration>> infiltration, Table table)
{
var infiltrationsByType = infiltration.ToLookup(item => item.TargetObject.Name);
List<OpenXmlElement> notes = new List<OpenXmlElement>();
foreach (var inflitrations in infiltrationsByType)
{
Run superscriptRun = CreateSuperScriptRun();
foreach (var item in inflitrations)
{
var row = new TableRow();
if (item == inflitrations.First())
{
row.Append(CreateFirstRowStartingCell(inflitrations.Key, superscriptRun));
}
else
{
row.Append(RenderTextContentCell(null, 1, null, null, new[] {
new VerticalMerge { Val = MergedCellValues.Continue }
}));
}
row.Append(RenderTextContentCell($"{item.TargetObject.AirflowScalar:R2} cfm {item.TargetObject.InfiltrationCalculationType}").ToLower());
table.Append(row);
var itemNotes = ParseNotes(mainDocumentPart, item.TargetObject.NotesHTML);
if (item == inflitrations.Last() && itemNotes.Any())
{
UpdateSuperScript(superscriptRun, itemNotes);
}
notes.AddRange(itemNotes);
}
}
IEnumerable<OpenXmlElement> result = RenderNotesArray(table, notes, new List<OpenXmlElement>());
return result;
}
As we have seen, duplication can be massively reduced simply by extracting code into simple helper functions.
This also makes it far easier to see just where the differences are between the two functions.
It is simply a matter of
row.Append(RenderTextContentCell(item.TargetObject.IsConstantVolume ? "Yes" : "No"));
row.Append(RenderTextContentCell($"{item.TargetObject.MinAirflow:R2}"));
row.Append(RenderTextContentCell($"{item.TargetObject.MaxAirflow:R2}"));
vs.
row.Append(RenderTextContentCell($"{item.TargetObject.AirflowScalar:R2} cfm {item.TargetObject.InfiltrationCalculationType}").ToLower());
To achieve your desired goal of a single function, we can make a generic function, and require that the caller pass in a function that will take care of these differences.
static IEnumerable<OpenXmlElement> RenderTableDataAndNotes<T>(
MainDocumentPart mainDocumentPart,
IEnumerable<ProjectObject<T>> projects,
Table table,
Func<ProjectObject<T>, IEnumerable<OpenXmlElement>> createCells
) where T : ITargetObject
{
var projectsByType = projects.ToLookup(item => item.TargetObject.Name);
List<OpenXmlElement> notes = new List<OpenXmlElement>();
foreach (var items in projectsByType)
{
Run superscriptRun = CreateSuperScriptRun();
foreach (var item in items)
{
var row = new TableRow();
if (item == items.First())
{
row.Append(CreateFirstRowStartingCell(items.Key, superscriptRun));
}
else
{
row.Append(RenderTextContentCell(null, 1, null, null, new[] {
new VerticalMerge { Val = MergedCellValues.Continue }
}));
}
var itemCells = createCells(item);
foreach (var cell in itemCells)
{
row.Append(cell);
}
table.Append(row);
var itemNotes = ParseNotes(mainDocumentPart, item.TargetObject.NotesHTML);
if (item == items.Last() && itemNotes.Any())
{
UpdateSuperScript(superscriptRun, itemNotes);
}
notes.AddRange(itemNotes);
}
}
IEnumerable<OpenXmlElement> result = RenderNotesArray(table, notes, new List<OpenXmlElement>());
return result;
}
Now, when we call it for say some Exhaust Equipment, we do so as follows:
var rendered = RenderTableDataAndNotes(mainDocumentPart, exhaustProjects, table,
exhaust => new[] {
RenderTextContentCell(exhaust.TargetObject.IsConstantVolume ? "Yes" : "No"),
RenderTextContentCell($"{exhaust.TargetObject.MinAirflow:R2}"),
RenderTextContentCell($"{exhaust.TargetObject.MaxAirflow:R2}"),
});
And for infiltration projects, we would do as follows:
var rendered = RenderTableDataAndNotes(
mainDocumentPart,
infiltrationProjects,
table,
infiltration => new[] {
RenderTextContentCell($"{item.TargetObject.AirflowScalar:R2} cfm {item.TargetObject.InfiltrationCalculationType}")
.ToLower()
});
The code could still be substantially improved even now. Currently it requires that the various project types implement a common ITargetObject interface declaring the Name property used to group projects by type. If you refactored your code to reduce nesting by hoisting Name to the ProjectObject<T> type, then we could remove the constraint and the otherwise useless requirement that Infiltration and ExhaustEquipment implement the ITargetObject interface.
Note, if you can't change the types, you can adjust the code in a few ways.
For example, you can remove the type constraint on T and build the lookup outside and pass it to the function:
static IEnumerable<OpenXmlElement> RenderTableDataAndNotes<T>(
MainDocumentPart mainDocumentPart,
ILookup<string, ProjectObject<T>> projectsByType,
Table table,
Func<ProjectObject<T>, IEnumerable<OpenXmlElement>> createCells
)
Then you would call it as
var infiltrationProjectsByType = infiltrationProjects.ToLookup(project => project.Name);
var rendered = RenderTableDataAndNotes(
mainDocumentPart,
infiltrationProjectsByType,
table,
infiltration => new[] {
RenderTextContentCell($"{infiltration.TargetObject.AirflowScalar:R2} cfm {infiltration.TargetObject.InfiltrationCalculationType}").ToLower()
}
);
I have this query:
await collection.FindAsync(Builders<T>.Filter.NearSphere(s => plcace.Location, location.Longitude, location.Latitude)
& Builders<T>.Filter.Nin(s => s.Id, excludedIDList))
which works as expected.
However, now I would like to get, in addition to my results, the distance between the given location's point and the documents returned as results.
I tried a couple of approaches such as https://oraerr.com/database/mongodb/c-mongodb-driver-2-0-getting-distance-back-from-near-query/
var coll = _database.GetCollection<UrbanEntity>("mycoll");
var geoNearOptions = new BsonDocument {
{ "near", new BsonDocument {
{ "type", "Point" },
{ "coordinates", new BsonArray {location.Longitude, location.Latitude} },
} },
{ "distanceField", "dist.calculated" },
{ "maxDistance", 100 },
{ "includeLocs", "dist.location" },
{ "num", 5 },
{ "spherical" , true }
};
var pipeline = new List<BsonDocument>();
pipeline.Add( new BsonDocument { {"$geoNear", geoNearOptions} });
using(var cursor = await coll.AggregateAsync<BsonDocument>(pipeline)) {
while(await cursor.MoveNextAsync()) {
foreach (var doc in cursor.Current) {
// Here you have the documents ready to read
}
}
}
However, (following this code above by example) it returns only 4 documents.. all the time the same ones... I am not sure to understand how does it work. Even if maxDistance line gets removed, I still have the same documents.
Isn't it possible to use Builders<T> to build an aggregation instead of BsonDocument? Also, how to limit the number of documents returned?
Thanks
I could finally find what I was looking for (answer slightly edited for ND):
public async Task<List<NearbyLocation>> GetNearbyLocationsAsync(Location location, int? take, List<ObjectId> exclude)
{
var geoNearOptions = new BsonDocument {
{ "near", new BsonDocument {
{ "type", "Point" },
{ "coordinates", new BsonArray { location.Longitude, location.Latitude } }, } },
{ "query", new BsonDocument("_id", new BsonDocument("$nin", new BsonArray(exclude))) },
// "query": Here you can add stuff to your geoNear aggregation.
// In fact, when I am searching around, I don't want a couple of IDs.
{ "distanceField", "distance" },
{ "limit", take },
{ "spherical", true}
// I was first using $nearSphere because of spherical approach.
// I then set the geoNear query as spherical by setting it to "true".
};
var pipeline = new List<BsonDocument>
{
new BsonDocument { { "$geoNear", geoNearOptions } },
new BsonDocument( "$sort", new BsonDocument("distance", 1))
// As per documentation, $nearSphere is sorting by distance.
// As per my tests (maybe I am wrong), it wasn't sorted, so I added a sort option
// to my aggregation.
};
List<NearbyLocation> locations = new List<NearbyLocation>();
using (var cursor = await GetCurrentCollection().AggregateAsync<BsonDocument>(pipeline))
{
while (await cursor.MoveNextAsync())
{
foreach (var doc in cursor.Current)
{
double distanceInMeters = doc["distance"].ToDouble();
NearbyLocation location = new NearbyLocation
{
Location = (BsonSerializer.Deserialize<Location>(doc["location"].ToBson())),
KmDistance = distanceInMeters / 1000, // From meters to KMs
MileDistance = distanceInMeters * 0.000621371192 // From meters to Miles
};
locations.Add(location);
}
}
}
return locations;
}
Lucky me, in my case, the location is a subdocument, which means I just take it and I don't take the whole document, otherwise, I get deserialization error.. still working on it but it's a different issue.
I so have an issue regarding the deserialization as the distance field is actually not part of my class.. I am then looking now into put my resulted document into another field such as:
{
"distance":0.1234,
"location":{...}
}
This question already has answers here:
Create dynamic variable name
(5 answers)
Closed 3 years ago.
I have 3 variables called gxpos, gypos and gzpos. I have a method called moove(), with a string argument axis (x, y or z) which I want to be able to change the value of both 3 variables (gxpos, gypos, gzpos).
In the code sample, I have represented the locations where I want to have the axis variable by this ?.
public void moove(string axis)
{
g(?)pos = (?)pos + trkSizeStep.Value;
if (g(?)pos != m(?)pos || -g(?)pos != m(?)pos)
{
(?)pos = g(?)pos;
port.WriteLine(axis + (?)pos);
lblpos(?).Text = (?)pos.ToString();
}
else
{
errorLimit(axis, 1);
}
}
Using Dictionaries:
var gpos = new Dictionary<string, int> { { "x", 0 }, { "y", 0 }, { "z", 0 } };
var mpos = new Dictionary<string, int> { { "x", 0 }, { "y", 0 }, { "z", 0 } };
var pos = new Dictionary<string, int> { { "x", 0 }, { "y", 0 }, { "z", 0 } };
var lblpos = new Dictionary<string, Label> { { "x", lblxpos }, { "y", lblypos }, { "z", lblzpos } };
public void moove(string axis)
{
gpos[axis] = pos[axis] + trkSizeStep.Value;
if (gpos[axis] != mpos[axis] || -gpos[axis] != mpos[axis])
{
pos[axis] = gpos[axis];
port.WriteLine(axis + pos[axis]);
lblpos[axis].Text = pos[axis].ToString();
}
else
{
errorLimit(axis, 1);
}
}
This is not possible in C# without reflection or the use of external tools such as T4 Templates.
However, you may be able to get around this by using arrays:
int[] gpos = new int[] { gxpos, gypos, gzpos };
int[] pos = new int[] { xpos, ypos, zpos };
int[] mpos = new int[] { mxpos, mypos, mzpos };
string axisNames = new string[] { "x", "y", "z" };
public void moove(int axis)
{
gpos[axis] = pos[axis] + trkSizeStep.Value;
if (gpos[axis] != mpos[axis] || -gpos[axis] != mpos[axis])
{
pos[axis] = gpos[axis];
port.WriteLine(axisNames[axis] + pos[axis]);
lblpos[axis].Text = pos[axis].ToString();
}
else
{
errorLimit(axisNames[axis], 1);
}
}
Or better yet, take advantage of Vector<T> and friends
I am working on a windows phone dialler app and I have implemented prediction text in my app. When user taps on keypad, contacts that match input are generated. Prediction is too slow, it also blocks my main thread that's why I have implemented BackGroundWorker But still having problems with the performance
My code is:
private void dialer_TextChanged(object sender, TextChangedEventArgs e)
{
MainPage.DialerText = dialer.Text;
if(!bw1.IsBusy)
bw1.RunWorkerAsync();
}
void bw1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
var digitMap = new Dictionary<int, string>() {
{ 1, "" },
{ 2, "[abcABC]" },
{ 3, "[defDEF]" },
{ 4, "[ghiGHI]" },
{ 5, "[jklJKL]" },
{ 6, "[mnoMNO]" },
{ 7, "[pqrsPQRS]" },
{ 8, "[tuvTUV]" },
{ 9, "[wxyzWXYZ]" },
{ 0, "" },
};
var enteredDigits = DialerText;
var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
var regexBuilder = new StringBuilder();
foreach (var val in charsAsInts)
regexBuilder.Append(digitMap[val]);
MainPage.pattern = regexBuilder.ToString();
MainPage.pattern = ".*" + MainPage.pattern + ".*";
}
catch (Exception f)
{
// MessageBox.Show(f.Message);
}
}
void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
SearchListbox.ItemsSource = listobj.FindAll(x => x.PhoneNumbers.Any(a=>a.Contains(MainPage.DialerText)) | Regex.IsMatch(x.FirstName, MainPage.pattern));
}
BackGroundWorker also blocking my main thread, hence when I tap on the keypad there's a lag while input values are added to the TextBox. I want to add input to the TextTox without any lag, how to do it? Thank you.
You can grab a real speed-up here by moving away from exhaustive searches of the entire wordlist and instead, putting your words into a more efficient data-structure.
For lightning fast lookups over any size of word list (but more expensive in terms of memory), you should build a tree structure that contains your entire word list.
The root node represents zero dialled digits, and it is connected to (up to) ten more nodes, where the edges connecting the nodes represent one of the possible numbers pressed for 0 to 9.
Each node then contains the possible words that can be formed from the path taken through the tree from the root node, where the path is representative of the numbers pressed.
This means that a search no longer requires iterating the entire word list and can be completed in very few operations.
Here's the concept in practice with a 370000 word-list I found on the web. Search takes around about 0.02ms on my desktop. Nice and fast. Seems to take about ~50MB in memory.
void Main()
{
var rootNode = new Node();
//probably a bad idea, better to await in an async method
LoadNode(rootNode).Wait();
//let's search a few times to get meaningful timings
for(var i = 0; i < 5; ++i)
{
//"acres" in text-ese (specifically chosen for ambiguity)
var searchTerm = "22737";
var sw = Stopwatch.StartNew();
var wordList = rootNode.Search(searchTerm);
Console.WriteLine("Search complete in {0} ms",
sw.Elapsed.TotalMilliseconds);
Console.WriteLine("Search for {0}:", searchTerm);
foreach(var word in wordList)
{
Console.WriteLine("Found {0}", word);
}
}
GC.Collect();
var bytesAllocated = GC.GetTotalMemory(true);
Console.WriteLine("Allocated {0} bytes", bytesAllocated);
}
async Task LoadNode(Node rootNode)
{
var wordListUrl =
"https://raw.githubusercontent.com/dwyl/english-words/master/words_alpha.txt";
Console.WriteLine("Loading words from {0}", wordListUrl);
using(var httpClient = new HttpClient())
using(var stream = await httpClient.GetStreamAsync(wordListUrl))
using(var reader = new StreamReader(stream))
{
var wordCount = 0;
string word;
while( (word = await reader.ReadLineAsync()) != null )
{
word = word.ToLowerInvariant();
if(!Regex.IsMatch(word,#"^[a-z]+$"))
{
continue;
}
rootNode.Add(word);
wordCount++;
}
Console.WriteLine("Loaded {0} words", wordCount);
}
}
class Node
{
static Dictionary<int, string> digitMap = new Dictionary<int, string>() {
{ 1, "" },
{ 2, "abcABC" },
{ 3, "defDEF" },
{ 4, "ghiGHI" },
{ 5, "jklJKL" },
{ 6, "mnoMNO" },
{ 7, "pqrsPQRS" },
{ 8, "tuvTUV" },
{ 9, "wxyzWXYZ" },
{ 0, "" }};
static Dictionary<char,int> letterMap;
static Node()
{
letterMap = digitMap
.SelectMany(m => m.Value.Select(c=>new {ch = c, num = m.Key}))
.ToDictionary(x => x.ch, x => x.num);
}
List<string> words = new List<string>();
//the edges collection has exactly 10
//slots which represent the numbers [0-9]
Node[] edges = new Node[10];
public IEnumerable<string> Words{get{
return words;
}}
public void Add(string word, int pos = 0)
{
if(pos == word.Length)
{
if(word.Length > 0)
{
words.Add(word);
}
return;
}
var currentChar = word[pos];
int edgeIndex = letterMap[currentChar];
if(edges[edgeIndex] == null)
{
edges[edgeIndex] = new Node();
}
var nextNode = edges[edgeIndex];
nextNode.Add(word, pos+1);
}
public Node FindMostPopulatedNode()
{
Stack<Node> stk = new Stack<Node>();
stk.Push(this);
Node biggest = null;
while(stk.Any())
{
var node = stk.Pop();
biggest = biggest == null
? node
: (node.words.Count > biggest.words.Count
? node
: biggest);
foreach(var next in node.edges.Where(e=>e != null))
{
stk.Push(next);
}
}
return biggest;
}
public IEnumerable<string> Search(string numberSequenceString)
{
var numberSequence = numberSequenceString
.Select(n => int.Parse(n.ToString()));
return Search(numberSequence);
}
private IEnumerable<string> Search(IEnumerable<int> numberSequence)
{
if(!numberSequence.Any())
{
return words;
}
var first = numberSequence.First();
var remaining = numberSequence.Skip(1);
var nextNode = edges[first];
if(nextNode == null)
{
return Enumerable.Empty<string>();
}
return nextNode.Search(remaining);
}
}
There is a number of optimizations you could make to improve the speed:
Adding .* prefix and suffix to your regex pattern is not necessary, because IsMatch will detect a match anywhere in a string
Using a local Dictionary<int,string> for parts of your pattern can be replaced by a static array
Converting digits to ints can be replaced with subtraction
The foreach loop and appending could be replaced by string.Join
Here is how:
private static string[] digitMap = new[] {
""
, "", "[abcABC]", "[defDEF]"
, "[ghiGHI]", "[jklJKL]", "[mnoMNO]"
, "[pqrsPQRS]", "[tuvTUV]", "[wxyzWXYZ]"
};
void bw1_DoWork(object sender, DoWorkEventArgs e) {
try {
MainPage.pattern = string.Join("", DialerText.Select(c => digitMap[c-'0']));
} catch (Exception f) {
// MessageBox.Show(f.Message);
}
}
I suspect the reason for the blocking is that your background worker thread doesn't actually do very much.
I expect the bw1_RunWorkerCompleted method (which gets run on the MAIN thread after the background worker has completed) is far more long-running! Can you move some (all?) of this code into the bw1_DoWork method instead? Obviously "listobj" would have to be accessible to the background thread though - just pass it into your delegate and access it by the DoWorkEventArgs.Argument property for this...
private void dialer_TextChanged(object sender, TextChangedEventArgs e)
{
MainPage.DialerText = dialer.Text;
if(!bw1.IsBusy)
bw1.RunWorkerAsync(listobj);
}
void bw1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
var digitMap = new Dictionary<int, string>() {
{ 1, "" },
{ 2, "[abcABC]" },
{ 3, "[defDEF]" },
{ 4, "[ghiGHI]" },
{ 5, "[jklJKL]" },
{ 6, "[mnoMNO]" },
{ 7, "[pqrsPQRS]" },
{ 8, "[tuvTUV]" },
{ 9, "[wxyzWXYZ]" },
{ 0, "" },
};
var enteredDigits = DialerText;
var charsAsInts = enteredDigits.ToCharArray().Select(x => int.Parse(x.ToString()));
var regexBuilder = new StringBuilder();
foreach (var val in charsAsInts)
regexBuilder.Append(digitMap[val]);
MainPage.pattern = regexBuilder.ToString();
MainPage.pattern = ".*" + MainPage.pattern + ".*";
var listobj = (ListObjectType)e.Argument;
e.Result = listobj.FindAll(x => x.PhoneNumbers.Any(a=>a.Contains(MainPage.DialerText)) | Regex.IsMatch(x.FirstName, MainPage.pattern));
}
catch (Exception f)
{
// MessageBox.Show(f.Message);
}
}
void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
SearchListbox.ItemsSource = (IEnumerable<ListObjectItemType>)e.Result;
}
NB - you'll obviously need to replace the casts to ListObjectType, and ListObjectItemType with the actual types!