Tag Archives: C#

Generic Types with Complex Constraints

I am working on a WPF app, leveraging Prism and Unity.

I have a scenario where I have lots of similar views/viewmodel. Not exactly alike, but 90%. I have 22 dimension-type datatypes. Instead of building out 88 individual MVVM scenarios, I am doing a cut-n-paste for the 22 instance of each specific activity (CRUD/Navigation). I needed to figure out how to define a generic base viewmodel that enforced inheritance for the generic type.

BTW: This is a throw-away app with an expected <3yr lifespan. It’s not worth the time/effort in building up something truly dynamic. I need to slam it out and pass it off to off-shore support. If it’s too complicated, I won’t be able to hand it off. This falls under, ‘Just get it done,’

All of the samples I could find were ‘ClassName<T,U> where T : class where U class’ examples. That doesn’t work for what I need to do. Below is how I solved the class definition issue.

public class DimensionBase : BindableBase, IChangeTracking 
{
...
}
public abstract class CollectionViewModelBase<T> : BindableBase, INavigationAware, IActiveAware where T : DimensionBase 
{
...
}
public abstract class DetailViewModelBase<T> : BindableBase, INavigationAware where T : DimensionBase 
{
...
}
public abstract class EditViewModelBase<T> : BindableBase, INavigationAware, IConfirmNavigationRequest where T : DimensionBase 
{
...
}

What I learned is that you can tack on the where clause to the end of the class definition to enforce constraints on the ‘T’ after all of the constraints are applied to the base class itself. I had been trying to manage the ‘T’ constraints first which is following my understanding of the ‘standard’ examples of where T … where U …

The delete action is managed by the collection view model, by way of the confirmation dialog hosted in the shell. I use events to manage that process.

There one more thing I want to clarify. I am using the region manager to handle all view-based navigation. I am using the event aggregator for passing messages around the system. The biggest one is telling all of the views to refresh their data when the target environment has changed. Getting navigation/confirmation dialog to fire, leverages events because the views hosted by separate modules (24 so far) that cannot directly access the notification/clarification dialogs hosted in the shell. Nor does Prism enable hosting these dialogs in a region (as far as I can tell). So there is only one way to do it. Events.

Enjoy.

Yet Another Service Locator

I have been writing a bunch of small samples apps, trying to improve my understanding of volatility-based decomposition solutions.  As part of this effort, I needed a proxy to link calls to services without the client directly calling the services.  This is a fundamental detail in any microservice implementation and there are libraries to enable this, if I where building Service Fabric applications.  But that isn’t what I am trying to do.  I need a tool that is light-weight, super-easy to use, and something that at least vaguely follows the SF communication patterns.   Continue reading Yet Another Service Locator

RegEx for LDAP

I have been working on a POC for injecting bulk entitlements into our Access Management System.  I guess you could think of our system as a big loop.  Entitlements are created in the AMS.  Users request an entitlement, which kicks off a workflow.  Once the entitlement is approved, it flows into AD.  The part I have been noodling around with is reading the permission out of AD.

I’ve been playing with this for years, literally.  I wrote some simple code a few years back.  For this POC, I dusted it off and showed it around.  I still had to migrate the code into the POC solution, so I had a chance to clean it up a bit.  No problem.  My old code uses UserPrincipals to pull the data out of AD.

var ctx = new PrincipalContext(ContextType.Domain);

var userPrincipal = await Task<UserPrincipal>.Factory.StartNew(() => UserPrincipal.FindByIdentity(ctx, request.UserId)) ?? new UserPrincipal(ctx);
response.User = userPrincipal.Convert();

It can be a bit slow.  At least the first query is slow.  It gets faster after the spin up.  Still…  I think I can make it better.

After poking around for a bit, I found the information I needed to access the AD container via LDAP.  Amazingly enough, the security team posted all of the needed info on the internal Wiki.  I’m not going to go into too much detail because I don’t really want to share any details about our network.

Anyway, once I pulled the data, I needed to parse the data into something I could use for my POC.  I looked around online and I could not find a single reference that showed a complete RegEx sample.  I saw some fragments, but nothing complete.  So here is what I came up with…

var regex = new Regex($@”CN=(?<CN>[\w]+)(,OU=(?<OU>\w+( \w+)*))+(,DC=(?<DC>\w+))+);

That’s it.  I only need the “CN” portion of the string but I thought I’d parse everything for future use.

Enjoy.

PS: I’ll fix the broken images when I have a chance.

Setting Theme/Accent with MahApps.Metro

I’m building up a sample app that leverages the MahApps.Metro libraries.  One of the challenges is setting the AppTheme and Accent colors dynamically.   I looked through more than a few online solutions and none of them worked for me.  Usually, the syntax was from an earlier version and didn’t match the ThemeManager object that I downloaded from NuGet (v1.0.0.0).

After comparing the sample app (v4.5) against my code in VS, I noticed the results from ThemeManager.DetectAppStyle() was always null in my app and valid in the sample app.   Continue reading Setting Theme/Accent with MahApps.Metro

Business object base class

I have three different projects converging.  They all need a more feature rich base class in the business layer.

I haven’t settled on a name for this base class.  EntityBase? BusinessBase?  BusinessObject?  I expect something will stick in the near future.

Proposed interfaces:

You should notice that most of these interfaces are pre-existing Microsoft interfaces.  ISupportUndo and ITrackStatus are inspired by Rockford Lhotka’s  CSLA framework.  The details are changed, but I really like the syntax to make the interface more readable.  “I support undo.”  I can’t think of a better way to express what the interface does.  Can you?

I’m still working on the implementation details.  I’ll share more once I have a stable sample app.