Skip to content

Instantly share code, notes, and snippets.

@kleinron
Last active April 28, 2023 19:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kleinron/7e2f4562b0df2dfb8d3ff2ec82116e04 to your computer and use it in GitHub Desktop.
Save kleinron/7e2f4562b0df2dfb8d3ff2ec82116e04 to your computer and use it in GitHub Desktop.
using System;
using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
namespace Canberra.InterfacesDemo.WebApp
{
public class Global : System.Web.HttpApplication
{
public static UnityContainer Container;
protected void Application_Start(object sender, EventArgs e)
{
Container = new UnityContainer();
var section = (UnityConfigurationSection)ConfigurationManager
.GetSection("unity");
section.Configure(Container);
}
}
}
namespace Canberra.InterfacesDemo.Lib
{
public class CustomersReminder
{
public void Remind(ICustomersFetcher customersFetcher, IEmailSender emailSender)
{
foreach (var customer in customersFetcher.GetSleepingCustomers())
{
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress);
}
}
}
}
namespace Canberra.InterfacesDemo.Lib
{
public class CustomersReminder
{
public void Remind(ICustomersFetcher customersFetcher, IEmailSender emailSender)
{
foreach (var customer in customersFetcher.GetSleepingCustomers())
{
emailSender.SendRemindMessage(customer.FirstName, customer.LastName, customer.EmailAddress);
}
}
}
}
var container = new UnityContainer();
var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
section.Configure(container);
var customersReminder = new CustomersReminder();
customersReminder.Remind(new CustomersFetcher(), container.Resolve<IEmailSender>());
var customersReminder = new CustomersReminder();
customersReminder.Remind(new CustomersFetcher(),
Global.Container.Resolve<IEmailSender>());
var customersReminder = new CustomersReminder();
customersReminder.Remind(new CustomersFetcher(), new EmailSender());
<?xml version="1.0"?>
<configuration>
<configSections>
<section
name="unity"
type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,
Microsoft.Practices.Unity.Configuration"
/>
</configSections>
<unity>
<typeAliases>
<typeAlias
alias="IEmailSender"
type="Canberra.InterfacesDemo.Lib.IEmailSender,
Canberra.InterfacesDemo.Lib"
/>
<typeAlias
alias="EmailSender"
type="MyMail.EmailSender, MyMail"
/>
</typeAliases>
<containers>
<container>
<types>
<type type="IEmailSender" mapTo="EmailSender"/>
</types>
</container>
</containers>
</unity>
</configuration>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment