Server.MapPath vs Request.MapPath - server.mappath

Can someone please explain the difference between Server.MapPath and Request.MapPath and usage scenarios for the two ?

ASP.NET has provided at least three different public MapPath methods since version 2.0:
HttpRequest.MapPath
HttpServerUtility.MapPath
HostingEnvironment.MapPath
Internally, HttpServerUtility.MapPath calls HttpRequest.MapPath.
HostingEnvironment.MapPath is a static method, throws an ArgumentNullException if the argument is null or empty, and throws an ArgumentException if the argument is a relative path.
HttpRequest.MapPath is not a static method and does not throw the exceptions mentioned above.

Related

C# Entity Framework : include with string parameter versus include with lambda expression [duplicate]

I am using entity framework and I want to execute a query and would like to know which is the besr way to execute a query. Which is best practice and why, and which one is more per-formant.
Option 1)
return
this.Storage.Customer.OfType<Preferred>()
.Include("Order")
.Where("it.Id = #customerId AND it.CustomerType = #cusType", new ObjectParameter("customerId ", customerId), new ObjectParameter("cusType", (int)cusType))
.Execute(MergeOption.OverwriteChanges)
.SingleOrDefault();
OR
return
this.Storage.Customer.OfType<Preferred>()
.Include(b => b.Order)
.Where(cust => cust.Id == customerId && cust.CustomerType== (int)cusType)
.SingleOrDefault();
The second question is why in option 2 us the .Execute not available? It appears red.
Thanks in advance.
The performance difference should be negligible compared with the actual data access, but you need to measure it to determine for sure.
Include with a lambda just uses reflection to get the property name then calls the version with a string parameter, so the only overhead is parsing the expression. (This is an implementation detail, however, so it is subject to change)
The benefit of using a lambda is type safety - using the wrong property name in a lambda will break the build, but using the wrong string will only fail at run-time.
The reason Execute is not available is because Include with a lambda parameter is an extension method on IQueryable<T> that returns an IQueryable<T> in order to chain methods like Where. Include with a string parameter is a method on ObjectQuery<T> that returns an ObjectQuery<T>. Execute is a method on ObjectQuery<T>, not IQueryable<T> so it is not available when you use IQueryable methods.

Setters: Consumer vs. direct method call

Why would you use a Consumer
public Consumer<String> setValue();
instead of direct function call
public void setValue(String value);
for setters?
I only see disadvantages:
the direct method call is more readable
another disadvantage of the Consumer variant is, that the calling code must always handle exceptions explicitly
try {
instance.setValue().accept("newValue");
} catch (Exception e) {
// handle exception
}
Consumer is an interface intended mostly for internal use within RxJava. Since you are getting started with RxJava, you shouldn't worry about it.
The whole purpose of RxJava is to flow data, the more you use it the less you handle state or use setters.
You must use the Consumer, when you cannot use method references for some reason: e.g. when you target older Android versions, that don't support Java8.
Note: with the new Android toolchain, Java8 method references are supported directly (even without Retrolambda, etc.), so this is not required anymore.
Thanks to Jake Wharton who answered in rx-preferences #96

GWT: JsArray and JavaScript object

Am facing an issue while trying to implement an example given over a website.
One of the methods in a class has a signature like this -
private void updateTable(JsArray prices) {....}
And am trying to invoke this method from another method as -
updateTable(JsonUtils.safeEval(response.getText()));
while doing this am seeing a compilation error as -
The method updateTable(JsArray) in the type StockWatcher is not applicable for the arguments (JavaScriptObject)
Though I have just used the exact code displayed in the website, am seeing this error. Not sure what needs to be done. Please help.
The problem has been fixed by making the following change -
updateTable((JsArray)JsonUtils.safeEval(response.getText()));
introduced a casting in the above statement.

GWT - Calling instance method from external javascript

There is this $entry method that we can use in GWT to allow external javascript to execute java methods.
You can see the explanations in their documentation https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI?hl=fr#calling
However, the example there is only with static methods. I'm trying to write it for a non-static method and when I try to call it, I get an exception :
java.lang.ClassCastException: Cannot cast com.google.gwt.core.client.JavaScriptObject$ to mypackage.MyModule
Here is my code :
public native void setRefreshModuleCallback() /*-{
$wnd.refreshModule = $entry(function() {
this.#mypackage.MyModule::refreshModuleJava();
alert('test');
});
}-*/;
public void refreshModuleJava() {
logger.log(Level.WARNING, "REFRESH");
}
What I find very funny is that alert is called, I see the result in the browser, but the call just before is not performed.
Do you know if it's actually possible to do such thing ?
$entry is not about calling java, it's about ensuring a few things go well in GWT: exceptions are routed to the GWT.UncaughtExceptionHandler, and commands scheduled via Scheduler#scheduleEntry and Scheduler#scheduleFinally are correctly called.
Your problem is the this. When the function is called, this is not your MyModule class (it's most probably the $wnd object). This is why the question you linked to uses var that = this. It's about scoping.
You also need to actually call the method, not only reference it: in JSNI, the first pair of parens are for the formal parameters (to disambiguate overloads), and you need another pair passing the actual arguments: that.#mypackage.MyModule::refreshModuleJava()().

FXCop warning CA1801: "Parameter is never used..." on overridden inherited supressed property

On a continued mission to clean up the codebase I inherited, via stylecop and fxcop, and one of the warnings from fxcop was CA1801: Parameter 'value' of Something.MyProperty.set(string) is never used. Remove the parameter or use it in the method body.
The code it complains about is:
public class Something : ISomeInterface
public new string MyProperty
{
get
{
throw new InvalidOperationException("MyProperty is not implemented.");
}
set
{
throw new InvalidOperationException("MyProperty is not implemented.");
}
}
This property is defined in the interface, but in this case is not needed in the derived class - Aside from the slightly questionable use of InvalidOperationException instead of NotImplementedException, which I believe is common, I wonder if I should just exclude the warning in FXCop with a note explaining why?
I don't see what else I could do do in terms of best practice, to prevent the warning in FXCop, other than refactoring this particular property out into a second interface, and then updating all the other classes that use this interface? I think I may have just answered my own question? :D
I believe it is because of the "new" keyword that you are receiving this warning. Try replacing removing new with override and see if the warning disappears.
public class Something : ISomeInterface
public string MyProperty
BTW, I recommend using NotImplementedException instead of InvalidOperationException as well.