Privately declared properties in C# don't show up in TypeScript in SPA - properties

So, i'm not sure what i'm doing wrong here but for some reason the callback function in TypeScript that i have doesn't have anything but _proto in the response's .data property whenever i set private properties in C# and new up an object that is filled with constructed properties. However, if the properties are public and i don't use a constructor then i can see the response's .data property is filled like i would expect it to be. Here is an example of what works:
public class ThisWorks{
public string MyProperty{get;set;}
}
Inside application layer:
ThisWorks example = new ThisWorks();
example.MyProperty = myReflectedProperty;
return example;
However, this does not work:
public class ThisDoesNotWork{
private string MyPrivateProperty {get;set;}
public ThisDoesNotWork(string myPrivateProperty){
MyPrivateProperty = myPrivateProperty;
}
}
What's causing this to happen? My TypeScript service has not changed but for some reason the data isn't coming across from the service call...Any help would be greatly appreciated!

Related

AutoFixture sometimes failing to generate a value for a property marked with RangeAttribute

I have a very strange behaviour in my testing code at the moment... It all started with decorating a string property with the RangeAttribute. I'm unit testing model validation in ASP.NET Core using the Validator class, so in the example below MyClass is a request object.
As i've understood AutoFixture will take into consideration the attribute a property is marked with. This is what the (pseudo) code looks like:
private void RunTest()
{
var sut = Fixture.Freeze<MyClass>();
sut.MyPropery = "12345";
// all tests from here on fails randomly
}
private class MyClass
{
[Required]
[Range(10000, 99999)]
public string MyPropery { get; set; }
}
Sometimes, the value generated is 10000 (woho!), and sometimes I get the following error, and i'm not sure why:
AutoFixture was unable to create an instance from
Ploeh.AutoFixture.Kernel.RangedNumberRequest, most likely because it
has no public constructor, is an abstract or non-public type.
I'm writing a unit test using "AutoFixture.AutoNSubstitute": "3.49.0"
Any ideas what might be wrong here?

how can we call ninjects service locator in code?

first time on stackoverflow + ninject (IoC's)
I have a situation in which I have Business Objects Implemented in a way that they have Models in them... i.e.
Public Class Whatever
Implements IWhatEver
Public Property Id as Integer
Public Property Name as String
Public Function SetWhatEver() as Whatever
'Do Whatever Settings
End Function
End Class
I am using ninject for DI (Dependency Injection) however the issue was that I couldn't use an Interface as a model being passed in an action, therefore I am trying to make a custom modelbinder and want to use the bindingContext.ModelType to pass to ninject and ninject to resolve it for me and so I can do my binding with metadata
Public Overrides Function BindModel(controllerContext As ControllerContext, bindingContext As ModelBindingContext) As Object
Dim modelType = ninjectPleaseResolve(bindingContext.ModelType)
Dim metaData = ModelMetadataProviders.Current.GetMetadataForType(Nothing, modelType)
bindingContext.ModelMetadata = metadata
Return MyBase.BindModel(controllerContext, bindingContext)
End Function
I hope this makes sense... I have tried looking for answers BTW and nothing online is making sense to me, so please can you explain in simple terms..
EDIT
I am adding the controller code below to give you a better understanding of what I am trying to do.. I don't want to use the Whatever class instead I want to use the IWhatever in the controller for my processing... please see below an example...
Public Class MainController
Inherits System.Web.Mvc.Controller
Dim repository As IWhatever
Public Sub New(pWhatever As IWhatever)
repository = pWhatever
End Sub
Function Index(myValues As IWhatever) As ActionResult
'So I can process these values to my liking...
repository.SetWhatEver(myValues)
' and then perhaps other functions like...
repository.Save()
Return View()
End Function
I hope this makes a little sense now..
the issue was that I couldn't use an Interface as a model being passed
in an action
You shouldn't pass in services through the action method. You should pass services in through the constructor. This way your container can build up the controller and all related objects for you and this way you don't have to write a custom model binder.

AOP - Injecting a property with a dynamically computed value

(or "Using LocationInterceptionAspect and IInstanceScopedAspect together")
Using Postsharp I'm trying to inject a property into a target class using 'IntroduceMember' and then using the 'OnGetValue' functionality of LocationInterceptionAspect dynamically give it a value on inspection.
Originally I thought that I'd need two separate aspects, one for the field injection and one for the location interception but managed to combine the two by implementing the IInstanceScopedAspect interface and inheriting from LocationInterceptionAspect.
The problem is that if I set a breakpoint I will see the property that's been injected, but if I set another breakpoint in the OnGetValue method (that gets fired for each property on the class) I can't see it...
Here's some sample code:
[Serializable]
class DALDecoratorWrapper : LocationInterceptionAspect, IInstanceScopedAspect
{
public override void OnGetValue(LocationInterceptionArgs args)
{
if (args.LocationName == "Type")
{
args.Value = "computed value here";
}
args.ProceedGetValue();
}
[IntroduceMember(OverrideAction = MemberOverrideAction.OverrideOrFail)]
public String Type { get; set; }
I was also hoping there was a better way of doing this than overriding OnGetValue as that's called for each getter where really I want to only target the getter of the property that's been injected
Cheers

Accessing Request in Automapper Custom Resolver

I am working on an ASP.NET Web API project.
I use Auto-mapper for mapping from my domain objects to DTOs
How do pass in a request parameters into a Custom ValueResolver ?
I saw a couple of similar questions on stackoverflow posted a TWO years back which mention that this cannot be done. Is this the same situation now or has this been resolved ?
Link to similar question raised a TWO years ago : How to pass values to a Custom Resolver in Automapper?
There is a ConstructedBy method which can be used to inject your own Resolver object , but I don't how to access pass in Request
Thanks
I used the AfterMap() feature for the time being. I am hoping someone has a better solution.
For simplicity if I reduced my source and destination classes to
public class Source {
public string Value {get;set;}
}
public class Destination{
public string Value {get;set;}
private bool _reset;
public Destination(bool reset = false){
_reset = reset;
}
public void TryReset(){
if(!_reset) return;
Value = string.Empty;
}
}
I added a AfterMap() in the Mapping configuration to call the reset method.
Mapper.CreateMap<Source, Destination>()
.AfterMap( (source, dest) => dest.TryReset());
In the controller I pass the reset flag from the Request directly as
var destination = Mapper.Map(new Source { Value ="Hello" },
new Destination(flag));

asp.net c# Automap a class from within that class

To best describe what I want to happen, i'll show what i'm doing, as to me it makes sense that this would work ...
public class foo()
{
public foo()
{
MyContext db = new MyContext();
foobar = db.foobar.first();
this = Mapper.Map<bar, foo>(foobar);
}
}
Basically, I want to use automapper within the destination class to map from the source class within the destination classes constructor.
Is there a way to do this?
You cannot do this because this is read only in C#. You cannot assign this a value in the constructor. Not cool to try to change the reference of an object in its constructor. You will have to do the mapping manually and assign each individual property. I would also question if it as a good practice to assign an object values from a database or service in a default constructor. It is not very transparent to the user of the object what is going on and you can get an exception in your constructor.