0
votes

My "view" code-behind typically looks something like this, with the view-model being injected into its constructor:-

public partial class CustomerView : UserControl
{
    public CustomerView(CustomerViewModel viewModel)
    {
            InitializeComponent();
            DataContext = viewModel;
    }
}

A view-model looks something like this, with any dependencies being injected into its constructor:-

public class CustomerViewModel
{
    ...
    public CustomerViewModel(SomeDependency someDependency)
    {
        // etc...

In this example all three classes (the view, view-model, and SomeDependency) reside in the same assembly, and ideally should all be internal as I don't want someone grabbing the assembly and start instantiating things. The trouble is, I can't make the view internal as it's a partial class. And if I leave it public, it won't compile as you can't pass an internal type to the constructor of a public class.

To get around this, I tried making the constructors internal instead. This compiled, but Castle Windsor throws a runtime exception as it's not possible to register types with internal constructors! So I seem to keep hitting these dead ends - is there any solution, is my class design wrong, or am I worrying unnecessarily about making everything internal?

1
"as I don't want someone grabbing the assembly and start instantiating things". Is this really a problem? making stuff internal is usually only needed when you create a reusable library. Are those classes part of a reusable library that gets shipped to customers or third parties?Steven
@Steven no this is just a desktop UI application, so I suspect I'm worrying over nothing (but I guess in theory someone could take the assembly and try to use it?).Andrew Stephens
Making classes internal doesn't help you with that though. It's quite easy to alter the assemblies IL to make types public again. There are even tools that help you with that.Steven
If I might make a suggestion, use Calibun.Micro to to the View/ViewModel wiring. It is cleaner and easier to maintain.Marwijn

1 Answers

0
votes

For the record it seemed that I was getting overly concerned about trying to "hide" the classes and methods, even though this is only an exe and not some kind of library for customers. Based on comments here and elsewhere I've decided against trying to change the visibility.