Use of "#this" in Moles delegates - visual-studio-2010

When I set a property of a moled type, it looks like they always require, as the first parameter, an object of the original moled type. I also noticed that some of the examples in the Moles Reference Guide assign this parameter as #this and I am trying to figure out why.
For instance, the original class looks something like this:
public class ProductDAO
{
internal void Insert(Product product, string id)
{
...
}
}
When I go to mole this method, the property is expecting a delegate whose first parameter is always the type of the moled object, in this case, a ProductDAO object. So in this case, the property is expecting a delegate of:
Action<ProductDAO, Product, string>
So do I always have to provide that moled object as the first parameter of my lambda expression? If so, what's the difference in using a regular variable name versus #this? What does #this mean/do?
MProductDAO.AllInstances.InsertProductString = (dao, product, id) => { };
versus
MProductDAO.AllInstances.InsertProductString = (#this, product, id) => { };

Related

Why do Static Methods & Enumerations have unexpected behavior in Xamarin Forms? Objects Passed to Static Methods become null & enums are stored as int

I have been using C#/.NET/Visual Studio since 2000, but I just started working with Xamarin Forms recently. It's a pretty great platform, however I have run across a couple of issues that I cannot figure out. (I'm using Visual Studio 2017 Community). I have been researching this behavior extensively, but have had absolutely no luck in obtaining a resolution. I have created a small application which easily reproduces both of these behaviors and would greatly appreciate any expertise that would help me fix my code so it works as expected. The two issues are these:
1) After creating a custom object using new in Xamarin Forms, the object is not null. However, if that object is passed via the parameter list into a static method, it becomes null inside the method. (Note: In my sample applications I placed the static method inside the Form1 and MainPage classes. I did this only to simplify the samples. The behavior is the same when I put the static method into a separate utility class, either static class or normal class.)
2) Custom enumerations are treated as integers instead of the actual enumerated type. For example, if an instance of an enumerated type is given a value of the first item in the enumeration, its value is 0, an integer. Whereas in a Windows application, the value would be the actual enumeration value (e.g., EnumValue1), and its type would be the enumerated type. This can be a problem if one is trying to do comparisons with strings. For example, aEnum.ToString() == "EnumValue1" will return true in a Windows application, but will return false in Xamarin Forms, because aEnum.ToString() will evaluate to "0". This behavior also breaks the tenet of encapsulation in object-oriented programming, in that you aren't supposed to have to know the underlying implementation of a data type in order to properly use it.
Below is the code that reproduces the issues. There are 2 separate projects, one Windows Forms, the other Xamarin Forms. (The Xamarin Forms project was created with .NET Standard code sharing strategy). Both projects use 2 utility classes Enumerations and CustObj, where the only differences are the namespace definitions (namespace WinFormApp.Utilities and namespace XamarinFormsApp.Utilities, respectively). They both reside in a Utilities folder under their respective projects.
Windows Forms "Form1.cs"
using System.Windows.Forms;
using WinFormApp.Utilities;
namespace WinFormApp
{
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// ENUMERATION TEST
ACustomEnumeration aCE = ACustomEnumeration.EnumValue1;
string aceStr = aCE.ToString();
// PASSING OBJECT TO STATIC METHOD
CustObj aCustObj = new CustObj();
int ii = aCustObj.i;
string ss = aCustObj.s;
Form1.Foo(aCustObj);
}
public static void Foo(CustObj custObj)
{
}
}
}
Xamarin Forms "MainPage.xaml.cs"
using Xamarin.Forms;
using XamarinFormsApp.Utilities;
namespace XamarinFormsApp
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
// ENUMERATION TEST
ACustomEnumeration aCE = ACustomEnumeration.EnumValue1;
string aceStr = aCE.ToString();
// PASSING OBJECT TO STATIC METHOD
CustObj aCustObj = new CustObj();
int ii = aCustObj.i;
string ss = aCustObj.s;
MainPage.Foo(aCustObj);
}
public static void Foo(CustObj custObj)
{
}
}
}
Utility class "Enumerations.cs"
namespace WinFormApp.Utilities (or XamarinFormsApp.Utilities)
{
public enum ACustomEnumeration
{
EnumValue1,
EnumValue2
}
class Enumerations
{
}
}
Utility class "CustObj.cs"
namespace WinFormApp.Utilities (or XamarinFormsApp.Utilities)
{
public class CustObj
{
public ACustomEnumeration _aCEMember;
public int i;
public string s;
public CustObj()
{
this.i = 99;
this.s = "Hello!";
}
}
}
When stepping through the WinFormApp in Visual Studio, one observes the following variables before the call to static method Foo:
aCE with value of EnumValue1 and Type ACustomEnumeration
aceStr with Value of "EnumValue1" and Type string
aCustObj with Type CustObj and data members:
_aCEMember with Value of EnumValue1 and Type ACustomEnumeration
i with Value of 99 and Type int
s with Value of "Hello!" and Type string
ii with Value of 99
ss with Value of Hello!
After stepping into Foo, the parameter is as expected:
custObj with Type CustObj and data members:
_aCEMember with Value of EnumValue1 and Type ACustomEnumeration
i with Value of 99 and Type int
s with Value of "Hello!" and Type string
All variables evaluate as expected. Enumerations evaluate as the actual enumerated value. The custom object is instantiated and its data members are given their appropriate initial values. When the object is passed into the static method, it is passed correctly as a reference type, and retains all of its data inside the method.
Conversely, when stepping through the XamarinFormsApp in Visual Studio, one observes the following variables before the call to static method Foo:
aCE with Value of 0 and Type int (violates encapsulation)
aceStr with Value of "0" and Type string (this demonstrates the problem with how enumerations are evaluated)
aCustObj with Type CustObj and data members:
_aCEMember with no Value or Type at all
i with no Value or Type at all
s with no Value or Type at all
ii with Value of 99
ss with Value of Hello!
After stepping into Foo, the parameter is NOT as expected:
custObj with Value of null and Type CustObj
The problems with enumerations are fairly obvious. But I am very confused as to what is really going on with the custom object. It makes no sense that when I examine an object after its instantiation, that it doesn't show its data members as being initialized. Although it is strange how variables ii and ss are actually given the correct values.
More importantly, the fact that when I pass the object into the static method, it becomes null, is really mind-boggling.
Does anyone have any ideas as to what is happenning, and how to fix it?

How to detect if JavaScript (ES6) class has a non-default constructor?

I would like to know by some form of reflection or other means if a given ES6 class has a user-written non-default constructor, or not.
Assuming that user-provided constructor has one argument or more, you can do that by checking the length property of the function(class). But if the constructor takes no argument, there is simply no way as far as I know
function Person(fName, lName) {
this.firstName = fName;
this.lastName = lName
}
console.log(Person.length);
function Person2() {}
console.log(Person2.length);
class Person3 {
constructor(f,l) {}
}
console.log(Person3.length);
class Person4 {
}
console.log(Person4.length);
You can invoke the Classname.prototype.constructor.toString() (where Classname is the inspected class name) and get the source string for the class. Which you can then parse and see if it was a constructor declared or not.
Presumably, you need a decent parser for that, but it's another story.
References:
http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype.tostring

How to set different WriteConcern for different conditions on the basis of some MongoAction property?

What is the possible way to set different WriteConcern values based on some condition of the MongoAction?
For example, i want if the MongoAction contains some value like "ABD", i want there should be no WriteConcern. And if it contains "PAYMENT", then WriteConcern should be SAFE.
What is the possiblie cases i can achieve this?
The above can be done implementing the WriteConcernResolver and overriding the resolve method as follows:
class MyAppWriteConcernResolver implements WriteConcernResolver {
#Override
public WriteConcern resolve(MongoAction action) {
if (action.getEntityClass().getSimpleName().contains("ADB")) {
return WriteConcern.NONE;
} else if (action.getEntityClass().getSimpleName().contains("PAYMENT")) {
return WriteConcern.SAFE;
}
return action.getDefaultWriteConcern();
}
}
The passed in argument, MongoAction, is what you use to determine the WriteConcern value to be used. MongoAction contains the collection name being written to, the java.lang.Class of the POJO, the converted DBObject, as well as the operation as an enumeration (MongoActionOperation: REMOVE, UPDATE´, ´INSERT, INSERT_LIST, SAVE) and a few other pieces of contextual information.

NHibernate: Using a LINQ expression with IQueryOver

I'm trying to use the IQueryOver interface of a NHibernate session object together with a LINQ expression as a criteria for selecting records in a static class. The LINQ expressions are defined in a mapping class as Expression<Func<T, object>> to get a value for an object T:
public void SearchParameter(Expression<Func<T, object>>)
These parameters get added by extending the mapping class:
public MyMapping : FindMap<MyNHibernateMappedObject>
{
public MyMapping()
{
this.SearchParameter(x => x.SomeColumn);
}
}
My Find class defines static methods for getting the previous and next record of the same type on the time axis. Each search parameter has to be identical in both records.
The Find class gets the search parameters from the mapping configuration and compiles the expressions with .Compile(). So I have the GetQueryWithSearchParameters method:
private static Func<T, object> searchParameter;
...
public static IQueryOver<T, T> GetQueryWithSearchParameters(ISession session, T current)
{
var query = session.QueryOver<T>()
.Where(x => searchParameter(x) == searchParameter(current));
return query;
}
However when building the query, I get the following exception:
System.InvalidOperationException: variable 'x' of type MyNHibernateMappedObject' referenced from scope '', but it is not defined
I don't know exactly what is going on here, but I suspect that x is not available in the delegate somehow. What am I doing wrong here?
session.QueryOver().Where(...) takes an expression, so it's going to try evaluate your expression and translate it to a query - ie. it will try to convert searchParameter(x) == searchParameter(current) into a SQL query, which it won't know how to do.
To get this to work you will need to construct the expression in code (not using a lambda expression). However I think that this is going to be a painful exercise and I think you will find it much easier to build a Criterion and add that to the QueryOver.

Set value in model when dynamically creating object of model

I have a bunch of models that may or may not have a property "CommonProperty". I want to set that property when I am creating a new object of a selected model. What I have so far, which works is:
ModuleItem model = db.ModuleItems.Find(ModuleItemID);
object o = GetModuleType(model.ControllerName);
private object GetModuleType(string ModelName)
{
string projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
Type classtype = Type.GetType(string.Format("{0}.Models.{1}", projectName, ModelName));
PropertyInfo[] properties = classtype.GetProperties();
var classObject = classtype.GetConstructor(new Type[] { }).Invoke(null);
return classObject;
}
What I want to achieve is to set CommonProperty:
o.CommonProperty = DistinctValue;
I tried creating a class that all of my models inherit from with a virtual method and each model then has an override method. Because its not static I can't call it directly and if I create a new ModelBase then when calling the method it doesn't get overriden by the type that object "o" is. I looked at creating an interface but 1 I don't even know how these work, and 2 (probably because of 1) I am not even able to create a way of doing this without build errors.
When stepping through the code I can see all the properties of "o" using the quickwatch or intellisense or whatever it's called. Clearly it is being recognised as the correct type. I can't seem to be able to call the method though because it's not a recognised (or set) type during build only during runtime and therefore I can't build the solution.
What I would do is create a base model (eg: BaseModel) for viewmodels that have the property CommonProperty then check whether the model is of type BaseModel then set the property
if (obj is BaseModel)
{
obj.CommonProperty = DistinctValue;
}
As it turns out I was way overthinking this. Typically the answer is ridiculously simple. No base model necessary, no overrides, no virtuals, no instances, dump it in a try catch and just set the value of the context object.
object o = GetModuleType(model.ControllerName);
db.Entry(o).State = EntityState.Added;
try
{
db.Entry(o).CurrentValues["CommonProperty"] = DistinctValue;
}
catch (Exception err) { }
I did have to make sure I set the state BEFORE setting a current value otherwise it wasn't in the context (or something like that).

Resources