Powershell RaiseAndSetIfChanged { get; set; } for Avalonia (ReactiveUI) - powershell

I am trying to build a UI using Avalonia. I have been plowing through it, but I hit a snag. This is the example code they provide:
using ReactiveUI;
public class MyViewModel : ReactiveObject
{
private string caption;
public string Caption
{
get => caption;
set => this.RaiseAndSetIfChanged(ref caption, value);
}
}
I need to set properties to "RaiseAndSetIfChanged", which require setting the get; set; of the variable within the class. I'm not sure how to do this in Powershell.
I have tried things like this:
Powershell class implement get set property
To no avail, but maybe I wasn't doing it right.
Alternatively, if there would be a different way to set RaiseAndSetIfChanged after [MyViewModel]::New() was called
Link to Example:
https://avaloniaui.net/docs/binding/change-notifications

I figured out how to use ReactiveUI in a different way, I give examples in the readme in this fork I have created of the module of Avalonia for Powershell.
https://github.com/MaynardMiner/psavalonia
It has a method called RaisePropertyChanged. I used it as a workaround to update bindings.
There is no possible way to natively create a getter/setter for C# emulation after searching.

Related

Breeze: How to make use of entity constructor code on server?

The question could also look like "Why is my initialization code of object in server not working?".
For example,
public class Order
{
public int Id { get; set; }
public int Quantity { get; set; }
public Order()
{
Quantity = 10;
}
}
From debugger, I can see the contructor is called and Quantity is set, however, it is not taking effect. I have to set Quantity on client side after the entity is created to make it work.
Is there a way to make the initialization on server work? By the way, my project is in Angular/Breeze/EF.
UPDATE: As I dig a little further, I believe, this is the general "issue" with Breeze that the server side change must be added to so-called entityInfo.OriginalValueMap, otherwise, its change is not saved. If true, how can work around this limitation because I have a lot default values I'd like to set on server?
This is how I create my entity:
var manager = new breeze.EntityManager("breeze/breeze");
manager.enableSaveQueuing(true);
function _createEntity(entityName) {
return manager.createEntity(entityName);
}
Setting any initialization code on the server in the model constructor won't work simply because the JavaScript client doesn't know anything about the C# constructor code on the server.
The DefaultValueAttribute is only honored when you're constructing a Model-First metadata. It is unfortunately ignored by EF when constructing a Code-First model metadata.
I suggest that you see Breeze - Create Entity on Server side for how another user solves a similar situation by creating a "create Endpoint" on the server that basically returns a new entity with default values set.
You don't have to create a constructor to set default values.
Just add the default value data annotation to any property you wish to set its default value:
[DefaultValue(10)]
public int Quantity { get; set; }
Also, consider not to initialize the Quantity when creating an entity at the client side.

Breeze Expand not working on WebAPI with EF

I have published a WebAPI service which returns a list of items. I am implementing Breeze and have managed to get it basically working with filtering/sorting. However, the Expand is not working.
http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=ToUser
You can see the ToUserId ForeignKey in the response above, but the ToUser properties are NULL (the user definitely exists)
You can see the ToUser EF navigation property in the metadata.
When I use .Include on the server side I can populate it with EF, but I don't want to do this.
I checked the Breeze Tutorial 2 here on Expand: http://learn.breezejs.com/
Here is it without expand: http://learn.breezejs.com/api/northwind/Products
and here it is with Expand (And you can see the additional Category info): http://learn.breezejs.com/api/northwind/Products?$expand=Category
This is what I am trying to do but mine does not fill it...
UPDATE:
I downloaded the Breeze 1.3.6 Samples and loaded the DocCode solution in VS2011.
I ran it and saw that the client-side expand works;
e.g.
http://localhost:47595/breeze/Northwind/Orders?$top=1 (no expand)
http://localhost:47595/breeze/Northwind/Orders?$top=1&$expand=Customer (expands customer correctly).
I checked the WebAPI controller code and it looks the same, except they use EF Code First instead of Model First. The Foreign key is decorated with a property:
Breeze Sample that Works
[ForeignKey("CustomerID")]
[InverseProperty("Orders")]
public Customer Customer {get; set;}
It just doesn't make sense... it is something to do with my WebAPI controller or EntityFramework relationship...
UPDATE 2
I downloaded the most basic ToDo Knockout Breeze sample and added this line to the ToDoItem class: public User ToUser { get; set; }
I am then able to Expand the WebAPI call with http://localhost:63030/breeze/todos/Todos?$expand=ToUser
So I have come to the conclusion that it is something to do with the fact that I am using EntityFramework DB First and not Code First. It definitely does seem possible to do in the current version of the WebAPI with Breeze and EF.
UPDATE 3
I have narrowed it down to my database, EF Database First and Code First differences, but still not identified the issue. I have changed from a Model to a Code First approach with the exact same result (ie. no expand).
For example: if you look at this Expand on the Breeze site that works, http://learn.breezejs.com/api/northwind/Products?%24expand=Category, try change the last param to an invalid field and it throws an error, e.g. :
http://learn.breezejs.com/api/northwind/Products?%24expand=Category1
However, in my code, it always ignores this param and returns ALL the records, and never throws an exception if the Expand param is incorrect:
http://www.ftter.com/desktopmodules/framework/api/dare/dares?$expand=To4657657User
Hence I am stumped.. I have no idea why this is not working.
My Code
[HttpGet]
[Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
public HttpResponseMessage Dares()
{
var response = Request.CreateResponse(HttpStatusCode.OK, (IQueryable<Dare>)contextProvider.Context.Dares);
return ControllerUtilities.GetResponseWithCorsHeader(response);
}
and here is the generated class from my EF model (using Database First)
public partial class Dare
{
public int DareId { get; set; }
public int ToUserId { get; set; }
public virtual User ToUser { get; set; }
}
Your URL seems to be missing the $ for the expand query option...should be $expand.
I think I have found the problem - the IQueryable with the HttpResponseMessage return type does not behave the same as a pure IQueryable return type. expand seems to work when I do not wrap it.
I have raised a new question here:
How to use Breeze IQueryable with CORS?

How do I create a custom content type as part of an Orchard module?

I would like to create a custom Package content type in Orchard 1.6. I already have a content part that represents the whole db record and UI for a Package, but now I'm wondering if I am going about this correctly.
It seems to me the next step is to use Orchard dashboard to create a new content type, and add my custom content part to the type. But, then the content type is internal to Orchard, with a dependency on my 'external' module that hosts the content part. How can I make it so that my content type is only available when my module is enabled?
For convenience, you could create the content type as part of one of the migrations in your module. This would only run when enabled. It would look something like this...
//Inside of your migration file...
public int UpdateFrom1(){
ContentDefinitionManager.AlterTypeDefinition("Package", cfg=> cfg
.Creatable()
.WithPart("YourCustomPart")
.WithPart("CommonPart")
.WithPart("Whatever other parts you want..."));
return 2;
}
Removing this content type when you disable your module would be the tricky part because it might be kind of unexpected by the user. Maybe "Package" is a type they still want to use with different parts attached. Also, if they manually delete your module without disabling, you can't really write code to respond to that event. The only reliable thing I know of is the IFeatureEventHandler. This would allow you to remove your content type if they disable the module in the admin...
public PackageRemover : IFeatureEventHandler {
private readonly IContentDefinitionManager _contentDefinitionManager;
public PackageRemover(IContentDefinitionManager contentDefinitionManager){
_contentDefinitionManager = contentDefinitionManager;
}
public void Installed(Feature feature){}
public void Enabling(Feature feature){}
public void Enabled(Feature feature){}
public void Disabling(Feature feature){
_contentDefinitionManager.DeleteTypeDefinition("Package");
}
public void Disabled(Feature feature){}
public void Uninstalling(Feature feature){}
public void Uninstalled(Feature feature){}
}

EF 4.1 DbContextGenerator object names - can they be changed?

I am using DB First EF 4.1 and I am adding DbContextGenerator tt template to my model. This is all great, but I end up with classes like this:
public partial class t_city
{
public t_city()
{
this.t_neighborhood = new HashSet<t_neighborhood>();
}
public int city_id { get; set; }
public string city_name { get; set; }
public virtual ICollection<t_neighborhood> t_neighborhood { get; set; }
}
This is super ugly. I modified the template to generate properties in camelcase, but that breaks the mapping onto tables and columns. Is there way to get clean class names and still preserve the mapping?
EDIT
Looks like it's possible by renaming the objects inside the Entity Model file. The only question remains, is it possible to automate the renaming using a function, or does it have to be done manually each time?
Thanks!
You need to do it manually but that's needed only once for each entity / property. These changes are not deleted when you update your model from the database.
The only automation can be implemented as some processing of EDMX file. It is XML with defined schema so you can process that XML in your custom tool or XSLT transformation and automatically change property and entity names in CSDL and MSL.

how to create a cmdlet?

I have written my program in c# .net. I want to convert it in to a powershell cmdlet. I was instructed to use pssnapin and getproc programs. Can anyone plz help me out..
Regards
Arun
To create a PowerShell cmdlet I would recommend you read Easy Windows PowerShell cmdlet development and debugging by Bart De Smet (B#) it's a great walk through for creating and debugging cmdlet's (Does what it says on the tin!)
Also I've found Professional Windows PowerShell Programming, ISBN 978-0470173930, (ISBN-10) 0470173939 very good for creating cmdlets and providers.
So, here's the PSCmdlet-Class[from medata], that you can inherit from.
namespace System.Management.Automation
{
public abstract class PSCmdlet : Cmdlet
{
protected PSCmdlet();
public PSHost Host { get; }
public CommandInvocationIntrinsics InvokeCommand { get; }
public ProviderIntrinsics InvokeProvider { get; }
public InvocationInfo MyInvocation { get; }
public string ParameterSetName { get; }
public SessionState SessionState { get; }
public PathInfo CurrentProviderLocation(string providerId);
public Collection<string> GetResolvedProviderPathFromPSPath(string path, out ProviderInfo provider);
public string GetUnresolvedProviderPathFromPSPath(string path);
public object GetVariableValue(string name);
public object GetVariableValue(string name, object defaultValue);
}
}
In order to get your cmdlets loaded, you need to sign them additionally, because Powershell does not execute not signed code.
Install windows powershell template thereby u will get the pssnapin program, using that you can convert your .cs file into a dll. Then search for getproc program in msdn. I don't remember exactly but there will be a method which will be executed at the first. you call your dll file in that method. I don't remeber the code, but this is the procedure to do.
Take a look at this article, Creating PowerShell Cmdlets in VB 2005. It uses VB 2005, but the process is the same for C#.
Full disclosure, I wrote the article, but I do not get paid by you looking at it. :)
Check also http://blogs.msdn.com/daiken/. In particular all the months from February 2007 to June 2007. You'll find the Visual Studio template link (for 2005, also works in Express), and several examples/labs.
The PowerTime project (http://code.google.com/p/powertime/) is open source and it implements a number of cmdlets. Good for a demo to get you going.