Use parameters in Elsa CompositeActivity.Build - elsa-workflows

I'm currently trying to accomplish following thing in Elsa: a CompositeActivity should take a parameter called Path, then build a workflow where it is waiting for a SignalReceived on Path and then do some other stuff.
[ActivityInput]
public string Path { get; set; }
public override void Build(ICompositeActivityBuilder builder)
{
builder
.SignalReceived(Path)
.SomeOtherStuff();
}
However, Path gets set after Build is called when constructing a workflow, thus Path is null while setting the signal.
Is there a way to have parameters in the Build method of a CompositeActivity? Does not necessarily need to be an ActivityInput.
I tried using ActivityInput to set Path. Did not work via the GUI nor via a custom .WithPath() method using the Extensions pattern. The documentation for CompositeActivity is currently under construction and I could not find an example in the Elsa repo related to my problem.

Related

Using ApplicationDbContext in an extension method

I am currently working on an ASP.NET Core application for internal use. This application should list dates and depending on the date some operations are permitted while others are not. I can perfectly use the scaffolded views and they display exactely what I want with one exception:
If an operation is not permitted I want to display them greyed out.
My idea was to add an extension method named IsSpecialOperationPermitted to DateTime so I can easily use dateTimeInstance.IsSpecialOperationPermitted.
The method "IsSpecialOperationPermitted" depends on some calculations and another table in the database. Now, to use my extension method I would like to access that table. Usually I'd create an instance of ApplicationDbContext and query that table.
For ASP.NET Core I have a feeling this is not the correct approach but that I should request an already existing ApplicationDbContext. Usually this ApplicationDbContext is injected more or less automatically but I don't have an ApplicationDbContext in my extension method.
What would be the "correct" way to request an ApplicationDbContext in my extension method?
You are trying to build an abstraction above a DateTime type which is perfectly fine. However, if you require a db context to perform your operation, you should explicitly take a DbContext as argument to signal that your method needs at least to read from it.
You could write something like this
public static class DateTimeExtensions
{
public static bool IsSpecialOperationPermitted(this DateTime value, ApplicationDbContext context)
{
// do here your computation
}
}
On the other hand, you could reverse it, and extend the DbContext:
public static class DbContentExtensions
{
public static bool IsSpecialOperationPermitted(this ApplicationDbContext context, DateTime value)
{
// do here your computation
}
}
The latter, in my opinion, is clearer.
Keep in mind that extension methods are designed to work with documented side effects and certain operations that theoretically could be "nice to have" as extension methods, could result too cryptic in the long run.
Also, you should be aware that if the caller has pending changes to the context, your method won't see them unless you inspect the Local property.

Map old field name and new field name from mongodb document to the same c# field

Does the MongoDB C# driver have the ability to map two field names from the server to a single field in the C# class?
The reason that we need this is that we've renamed a field in our code and would like to support both the old and new field name for a period of time.
This is a typical data migration scenario and it would be very helpful if we could establish a pattern for doing this via mapping without having to add special version logic to the data entity class or to the logic code.
We're currently using the official c# driver v2.3 with server v3.2.
I don't think that is very much possible. What you can do is
Create a new property that is not saved in the bson. Use BsonIgnore tag thusly and populate the data from both of your property and expose that in the JSON response you are providing. Use JsonIgnore to make sure you provide proper ignoring directives there.
Go for migration techiniques in Mongodb. You can use a migration tool like this one. As you are using the C# driver 2.3.0 I would presume you want to use that and you can use my fork here for C# driver 2.3.0 compatibility.
Code sample for number 1:
class Data
{
[JsonIgnore]
public string OldProperty { get; set; }
public string NewProperty
{
get
{
// Return this one or the old one based on your logic
}
set;
}
}

How can I modify the fixture my custom theory data attribute creates for AutoFixture?

I'm really appreciating the power of AutoFixture coupled with XUnit's theories. I've recently adopted the use of encapsulating customizations and providing them to my tests via an attribute.
On certain occasions, I need a one-off scenario to run my test with. When I use an AutoDomainDataAttribute, like above, can I ask for an IFixture and expect to get the same instance created by the attribute?
In my scenario, I'm using MultipleCustomization by default for collections, etc. However, in this one case, I want only a single item sent to my SUT's constructor. So, I've defined my test method like so:
[Theory, AutoDomainData]
public void SomeTest(IFixture fixture) {
fixture.RepeatCount = 1;
var sut = fixture.CreateAnonymous<Product>();
...
}
Unfortunately, I'm getting an exception when creating the anonymous Product. Other tests work just fine, if I ask for a Product as a method parameter with those attributes. It's only an issue in this particular case, where I'm hoping that the fixture parameter is the same one created by my AutoDomainDataAttribute.
Product's constructor expects an IEnumerable that normally gets populate with 3 items, due to the customizations I have in-place via AutoDomainData. Currently, my DomainCustomization is a CompositeCustomization made up of MultipleCustomization and AutMoqCustomization, in that order.
The exception is: "InvalidCastException: Unable to cast object of type 'Castle.Proxies.ObjectProxy' to type 'Product'."
If you need the same Fixture instance as the one active in the attribute, you can inject the Fixture into itself in a Customization, like this:
public class InjectFixtureIntoItself : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Inject(fixture);
}
}
Just remember to add this to your CompositeCustomization before AutoMoqCustomization, since IFixture is an interface, and if AutoMoqCustomization comes first, you'll get a Mock instance instead - AFAICT, that's what's currently happening with the dynamic Castle proxy.
However, if you really need a Fixture instance, why not just write a regular, imperative test method:
[Fact]
public void SomeTest()
{
var fixture = new Fixture().Customize(new DomainCustomization());
fixture.RepeatCount = 1;
var sut = fixture.CreateAnonymous<Product>();
// ...
}
That seems to me to be much easier... I occasionally do this myself too...
Still, I wonder if you couldn't phrase your API or test case in a different way to make the whole issue go away. I very rarely find that I have to manipulate the RepeatCount property these days, so I wonder why you would want to do that?
That's probably the subject of a separate Stack Overflow question, though...

Command Pattern in .NET MVC 3 (removing junk from the controller)

I am trying to implement this Command Pattern on my .NET MVC 3 application, specifically for saving edits to a Thing. I am undecided on how to proceed. Before I get to the actual question, here is the simplified code:
public class ThingController
{
private readonly ICommandHandler<EditThingCommand> handler;
public ThingController(ICommandHandler<EditThingCommand> handler)
{
this.handler = handler;
}
public ActionMethod EditThing(int id)
{
...build EditThingViewModel and return with View...
}
[HttpPost]
public ActionMethod EditThing(int id, EditThingViewModel vm)
{
var command = new EditThingCommand
{
...not sure yet...
};
this.handler.Handle(command);
...redirect somewhere...
}
}
My EditThingViewModel is wholly disconnected from my domain, which consists of POCO classes. It seems like my EditThingCommand should look like this:
public class EditThingCommand
{
Thing ModifiedThing;
}
However, building ModifiedThing would then still be happening in my controller. That's the majority of the work in this case. By the time ModifiedThing is built (and the "old" timestamp applied to it for optimistic concurrency checking), all that's left is for command to call Update on my data context.
Clearly there is value in being able to easily decorate it with other commands, but I'd also like to be able to move the construction of ModifiedThing outside of my controller. (Perhaps this question is really just about that.) EditThingCommand is in my domain and doesn't have a reference to EditThingViewModel, so it can't go there. Does it make sense to have another command in my presentation layer for mapping my viewmodel to my poco entity?
I created an EditThingPostCommand outside of my domain, which takes the EditThingViewModel as a parameter. The EditThingPostCommandHandler is responsible for creating the EditThingCommand and calling its handler.
It works, but I'm not going to assume that's the best answer to my question. Arguably most of what the EditThingPostCommandHandler is doing could be done in a custom AutoMapper configuration, which would still serve the purpose of cleaning up the controller action method.
After several months of using this pattern on other projects, it is apparent to me that the commands on this particular project were simply too general and therefore too complex, requiring too much setup. It would have been better to create, for example, an EditThingTitleCommand and a MoveThingPiecesCommand and so on, and call them from their own ActionMethods.
In other words, when using the command pattern, don't just use the commands as replacements for typical CRUD operations. With more specificity comes more benefit.

Visual Studio: Is it possible to define custom functions for use in one's own Code Snippets?

I want to write a Visual Studio snippet which allows me to quickly create a class of the form:
public SomeClass
{
public SomeType SomeProperty { get; private set; }
public SomeClass(SomeType someProperty) { SomeProperty = someProperty; }
}
I want the parameter on the constructor to have the same name as the property, but lower-cased. I don't want to have to type the name twice. How do I do this?
I've already looked at the XML for snippets and I discovered that there are certain functions that can be used. For example, the built-in "switch" code snippet contains the following instruction:
<Function>GenerateSwitchCases($expression$)</Function>
According to this MSDN documentation page, there are three built-in functions that you can use in this <Function> tag. Unfortunately, neither of them does what I want (lower-case the first letter). Is it possible to define a function that I can then use in my snippet?
No this is not possible in Visual Studio today. The definition and execution of snippet functions is directly controlled by the C# language service. It is not currently an extensibility point. It is being considered for a future version of the product though.

Resources