Because I can

Without exception, my best and worst ideas are motivated by the reason “because I can”. This is likely poor motivation but I stand by it. Unfortunately, this is not this post’s motivation, which is “because it could be useful”. So, I’m taking a detour from my usual topic of my video game.

Today’s topic is rather customized Data Transfer Object (DTO) generation. This is definitely a matter of utility.

This part is where “because I can” comes in. I shall use C#, WPF, Akka.NET, and Visual Studio 2015 RC.

The minimal viable product for me is the ability to connect to a database, and then dump all tables into a DTO C# class. Now the first question you might ask is “is that not the entire scope of this?” And you would be mostly right, except that I also have a wishlist a mile long but that will come later if at all.

So here I go:BecauseICan_2

First order of business is the NuGet Package for one of my favorite libraries: MvvmLight. BecauseICan_3

Actually, I’ve installed a couple of packages.BecauseICan_4

Data Connection Dialog because I’m lazy and this seemed the easiest and quickest way for me to get a generic sql connection going. MahApps.Metro because they are great and make visual design easier on me. Akka.NET because I’m planning on farming out the file creations to Actors (ha, never heard of actors farming). MvvmLight to simplify my WPF. And the others are supporting packages.

I’ve made a very simple form to start off with BecauseICan_5

The creator of MvvmLight has an interesting blog post here which I used and butchered the concept for my own purposes.

There is now a static resource ViewModelLocator, of which one is in App.xaml

      <locator:ViewModelLocator x:Key="VmLocator"/>

That class looks like this

    public class ViewModelLocator
    {
        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
 
            SimpleIoc.Default.Register<ActorSystem>(() => ActorSystem.Create("LeastCost"), true);
            SimpleIoc.Default.Register<AnalysisViewModel>();
        }
        public AnalysisViewModel AnalysisVm
        {
            get
            {
                return ServiceLocator.Current.GetInstance<AnalysisViewModel>();
            }
        }
    }

And is used like this

<Controls:MetroWindow x:Class="LeastCost.MainWindow"
                      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                      xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
                      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                      xmlns:local="clr-namespace:LeastCost"
                      mc:Ignorable="d"
                      DataContext="{Binding Source={StaticResource VmLocator}, Path=AnalysisVm }"
                      Title="LeastCost DTO Generation" Height="350" Width="525">
  <Grid>
    <Button Content="Connect to DB" Command="{Binding CmdConnect}" Width="150" Height="30"/>
  </Grid>
</Controls:MetroWindow>

The viewmodel being used in the window is this

    public class AnalysisViewModel
    {
        private readonly ActorSystem _actorSystem;
 
        public AnalysisViewModel(ActorSystem actorSystem)
        {
            _actorSystem = actorSystem;
            CmdConnect = new RelayCommand(DialogConnect);
        }
 
        public RelayCommand CmdConnect { get; private set; }
 
        private void DialogConnect() ....
    }

So when I click the button I get
BecauseICan_6
BecauseICan_7

Next post on this topic will deal with how to use that sql connection to get all the data I need and dump it to custom C# class files.

Before I forget, all my code on this is here on github.