A simple C# dependency resolver class.
//
// Uttils/SimpleResolver.cs
// https://github.com/dropbox/DropboxBusinessAdminTool/blob/master/Source/DfBAdminToolkit.Common/Utils/SimpleResolver.cs
namespace DfBAdminToolkit.Common.Utils
{
using System;
using System.Collections.Generic;
public class SimpleResolver
{
public static readonly SimpleResolver Instance = new SimpleResolver();
private readonly Dictionary<Type, Type> _container;
private readonly Dictionary<Type, object> _singleton;
private static readonly object _lock = new object();
private SimpleResolver()
{
_container = new Dictionary<Type, Type>();
_singleton = new Dictionary<Type, object>();
}
public void Bind<ContractType>(Type implementer)
{
lock (_lock)
{
_container.Add(typeof(ContractType), implementer);
}
}
public ContractType Get<ContractType>(params object[] args)
{
lock (_lock)
{
if (!_singleton.ContainsKey(typeof(ContractType)))
{
ContractType obj = (ContractType)Activator.CreateInstance(_container[typeof(ContractType)], args);
_singleton.Add(typeof(ContractType), obj);
}
return (ContractType)_singleton[typeof(ContractType)];
}
}
public void Dispose()
{
lock (_lock)
{
_singleton.Clear();
_container.Clear();
}
}
}
}
//
// Service Registration Example
//
// https://github.com/dropbox/DropboxBusinessAdminTool/blob/master/Source/DfBAdminToolkit/Program.cs
namespace DfBAdminToolkit
{
using DfBAdminToolkit.Common.Utils;
using DfBAdminToolkit.Model;
using DfBAdminToolkit.Presenter;
using DfBAdminToolkit.View;
using Microsoft.Win32;
using System;
using System.Windows.Forms;
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Bind();
// check legal acceptance status
// if user hasn't accepted license term yet, force user to land on legal page first.
bool userPreviouslyAcceptedLegalTerm = false;
RegistryKey key = RegistryUtils.FindKey(ApplicationResource.RegistryEntryPoint);
if (key != null)
{
// check acceptance status
string value = RegistryUtils.GetKeyValue(key, ApplicationResource.RegistryKey);
bool.TryParse(value, out userPreviouslyAcceptedLegalTerm);
}
if (userPreviouslyAcceptedLegalTerm)
{
IMainView appView = new MainView();
IMainModel appModel = new MainModel();
IMainPresenter presenter = SimpleResolver.Instance.Get<IMainPresenter>(
new object[] { appModel, appView }
);
Application.Run(appView as MainView);
}
else
{
// show legal view
ILegalView legalView = new LegalView();
ILegalModel legalModel = new LegalModel();
ILegalPresenter presenter = SimpleResolver.Instance.Get<ILegalPresenter>(
new object[] { legalModel, legalView }
);
Application.Run(legalView as LegalView);
}
Unbind();
}
private static void Bind()
{
SimpleResolver.Instance.Bind<IMainPresenter>(typeof(MainPresenter));
SimpleResolver.Instance.Bind<ILegalPresenter>(typeof(LegalPresenter));
SimpleResolver.Instance.Bind<ITextSearchPresenter>(typeof(TextSearchPresenter));
SimpleResolver.Instance.Bind<IDumpUserContentPresenter>(typeof(DumpUserContentPresenter));
SimpleResolver.Instance.Bind<IProvisioningPresenter>(typeof(ProvisioningPresenter));
SimpleResolver.Instance.Bind<IGroupsPresenter>(typeof(GroupsPresenter));
SimpleResolver.Instance.Bind<ITeamFoldersPresenter>(typeof(TeamFoldersPresenter));
SimpleResolver.Instance.Bind<IPaperPresenter>(typeof(PaperPresenter));
SimpleResolver.Instance.Bind<IDevicesPresenter>(typeof(DevicesPresenter));
SimpleResolver.Instance.Bind<ITeamHealthPresenter>(typeof(TeamHealthPresenter));
SimpleResolver.Instance.Bind<ITeamAuditingPresenter>(typeof(TeamAuditingPresenter));
SimpleResolver.Instance.Bind<IDataMigrationPresenter>(typeof(DataMigrationPresenter));
SimpleResolver.Instance.Bind<ISettingsPresenter>(typeof(SettingsPresenter));
}
private static void Unbind()
{
SimpleResolver.Instance.Dispose();
}
}
}
//
// View/MainView.cs
//
// https://github.com/dropbox/DropboxBusinessAdminTool/blob/master/Source/DfBAdminToolkit/View/MainView.cs
private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
{
ISettingsPresenter presenter = GetSettingPresenter();
presenter.ShowSettings(this);
}
private DataMigrationView CreateDataMigrationView()
{
IDataMigrationModel model = new DataMigrationModel();
IDataMigrationView view = new DataMigrationView();
IDataMigrationPresenter presenter = SimpleResolver.Instance.Get<IDataMigrationPresenter>(
new object[] { model, view }
);
view.ShowView();
return view as DataMigrationView;
}
private ISettingsPresenter GetSettingPresenter()
{
ISettingsModel model = new SettingsModel();
ISettingsView view = new SettingsView();
ISettingsPresenter presenter = SimpleResolver.Instance.Get<ISettingsPresenter>(
new object[] { model, view }
);
return presenter;
}