I am new to dependency injection and Microsoft Unity IoC. My scenario is the follwing:
Employee-Class:
class Employee
{
private readonly ISalaryCalculation _salaryCalculationCalculator;
public Employee(ISalaryCalculation salaryCalculationCalculator)
{
_salaryCalculationCalculator = salaryCalculationCalculator;
BaseSalary = 42;
}
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
public int BaseSalary { get; set; }
public int CalcSalary()
{
return _salaryCalculationCalculator.CalcSalary(BaseSalary);
}
}
I have two different implementations of my SalaryCalculation-Interface
interface ISalaryCalculation
{
int CalcSalary(int baseSalary);
}
My Unity setup is:
static void Main(string[] args)
{
UnityContainer unityContainer = new UnityContainer();
unityContainer.RegisterType<Employee>();
unityContainer.RegisterType<ISalaryCalculation, BossSalaryCalculation>("Boss");
unityContainer.RegisterType<ISalaryCalculation, NormalSalaryCalculation>("Normal");
var employee = unityContainer.Resolve<Employee>(new DependencyOverride(typeof(ISalaryCalculation),unityContainer.Resolve<ISalaryCalculation>("Boss")));
Console.WriteLine(employee.CalcSalary());
}
Is there a smarter way to achiev this by using Unity? Because whenever I instantiate a new Employee, I have to pass its salaryCalculation logic.
You can register only one implementation depending on some condition:
static void Main(string[] args)
{
UnityContainer unityContainer = new UnityContainer();
unityContainer.RegisterType<Employee>();
if (someCondition)
{
unityContainer.RegisterType<ISalaryCalculation, BossSalaryCalculation>("Boss");
}
else
{
unityContainer.RegisterType<ISalaryCalculation, NormalSalaryCalculation>("Normal");
}
var employee = unityContainer.Resolve<Employee>();
Console.WriteLine(employee.CalcSalary());
}
Another way - to use ResolvedParameter:
static void Main(string[] args)
{
UnityContainer unityContainer = new UnityContainer();
unityContainer.RegisterType<Employee>();
unityContainer.RegisterType<ISalaryCalculation, BossSalaryCalculation>("Boss");
unityContainer.RegisterType<ISalaryCalculation, NormalSalaryCalculation>("Normal");
var employee = unityContainer.Resolve<Employee>(new ParameterOverride("salaryCalculationCalculator", new ResolvedParameter<ISalaryCalculation>("Boss")));
Console.WriteLine(employee.CalcSalary());
}
Related
I'm working on Unit test. I create TestBuilder class, where I create method SetupTown method.
When I tried call this method in my main test class - i have error( Error CS0118'TestBuilder' is a namespace but is used like a type). I read about it and recommend to call a class with method. I tried do it, but It doesn't help.
public partial class TownServiceTests
public partial class TownServiceTests
{
private class TestBuilder
{
public Mock<ITownRepository> MockTownRepository { get; set; }
//public Mock<IClientViewModelBuilder> MockClientPropertyModelBuilder { get; set; }
public Mock<IMapper> MockMapper { get; set; }
public TestDataGenerator TestDataGenerator;
private readonly string _jsonDataPath = #"../../../TestData/Town/TownTestData.json";
private string _jsonDataKey;
private TownViewModel Towns { get; set; }
public TestBuilder(string jsonDataKey)
{
MockTownRepository = new Mock<ITownRepository>();
//MockClientPropertyModelBuilder = new Mock<IClientViewModelBuilder>();
MockMapper = new Mock<IMapper>();
TestDataGenerator = new TestDataGenerator(_jsonDataPath);
_jsonDataKey = jsonDataKey;
TestDataGenerator.LoadData(_jsonDataKey);
}
public ITownService Build()
{
return new TownService(MockTownRepository.Object,
MockMapper.Object);
}
public TestBuilder SetupTowns()
{
var towns = TestDataGenerator.GetTestData<Town>(_jsonDataKey, "Towns");
MockTownRepository.Setup(r => r.InsertTown(It.IsAny<string>()))
.ReturnsAsync(towns.FirstOrDefault().Id);
return this;
}
}
}
}
Please check method public TestBuilder SetupTowns
Here my TestClass
[TestClass]
public partial class TownServiceTests
{
[TestMethod]
public async Task ValidInsertTown()
{
var builder = new TestBuilder("Data").SetupTowns; //Problem
var service = builder.Build();
var expectedTowns = builder.TestDataGenerator.GetTestData<Town>("Data", "Towns");
var result = await service.InsertTown(expectedTowns);
Assert.IsNotNull(result);
Assert.IsNull(result);
}
}
Could toy tell me what I do wrong?
Example
public partial class ClientServiceTests
{
private class TestBuilder
{
public Mock<IClientRepository> MockClientRepository { get; set; }
public Mock<IClientViewModelBuilder> MockClientPropertyModelBuilder { get; set; }
public Mock<IMapper> MockMapper { get; set; }
public TestDataGenerator TestDataGenerator;
private readonly string _jsonDataPath = #"../../../TestData/Client/ClientTestData.json";
private string _jsonDataKey;
public TestBuilder(string jsonDataKey)
{
MockClientRepository = new Mock<IClientRepository>();
MockClientPropertyModelBuilder = new Mock<IClientViewModelBuilder>();
MockMapper = new Mock<IMapper>();
TestDataGenerator = new TestDataGenerator(_jsonDataPath);
_jsonDataKey = jsonDataKey;
TestDataGenerator.LoadData(_jsonDataKey);
}
public IClientService Build()
{
return new ClientService(MockClientRepository.Object
, MockClientPropertyModelBuilder.Object
, MockMapper.Object);
}
public TestBuilder SetupClients()
{
var clients = TestDataGenerator.GetTestData<ClientSummary>(_jsonDataKey, "Clients");
MockClientRepository.Setup(r => r.GetClientBySearchCriteria(It.IsAny<string>()))
.ReturnsAsync(clients);
var clientViewModels = TestDataGenerator.GetTestData<ClientViewModel>(_jsonDataKey, "ClientViewModel");
MockClientPropertyModelBuilder.Setup(r => r.GetClientViewModel(clients))
.Returns(clientViewModels);
return this;
}
public TestBuilder SetupInvalidInputClients()
{
MockClientRepository.Setup(r => r.GetClientBySearchCriteria(It.IsAny<string>()))
.ReturnsAsync(new List<ClientSummary>());
MockClientPropertyModelBuilder.Setup(r => r.GetClientViewModel(new List<ClientSummary>()))
.Returns(new List<ClientViewModel>());
return this;
}
}
}
TestClass (here works good)
[TestMethod]
public async Task GetClientBySearchCriteria_ValidInput_ReturnClients()
{
var searchParameter = "1";
var builder = new TestBuilder("Data").SetupClients();
var service = builder.Build();
var expectedClients = builder.TestDataGenerator.GetTestData<ClientSummary>("Data", "Clients");
var result = await service.GetClientBySearchCriteria(searchParameter);
Assert.IsNotNull(result);
Assert.AreEqual(2, result.Count);
Assert.AreEqual(expectedClients.FirstOrDefault().Name, result.FirstOrDefault().Name);
}
namespace of the file
I think, the issue is happened because you have Something.TestBuilder.Something namespace and compiler is trying to use it instead of class.
You have the TestBuilder folder and a few classes inside it. It may be that classes inside TestBuilder folder contains TestBuilder in their namespaces and compiler trying to access this namespace instead of class.
So playing with my own test Dependency Injector class. (yeah tons out there but this is just for fun)
Works decent but I don't know how to get the correct constructor based on the Interface passed in.
internal class DiContainer
{
private readonly Dictionary<Type, RegistryRecord> registry = new Dictionary<Type, RegistryRecord>();
private static DiContainer instance;
private DiContainer()
{
}
public static DiContainer GetInstance()
{
return instance ??= new DiContainer();
}
public void Register<T, C>() where C : class, T
{
registry.Add(typeof(T), new RegistryRecord
{
InterfaceType = typeof(T),
ConcreteType = typeof(C),
IsSingleTon = false
});
}
public void Register<C>() where C : class
{
Register(typeof(C));
}
public void Register(Type t)
{
registry.Add(t, new RegistryRecord
{
InterfaceType = t,
ConcreteType = t,
IsSingleTon = false
});
}
public void RegisterSingleton<T, C>(C instance = null) where C : class, T
{
registry.Add(typeof(T), new RegistryRecord
{
InterfaceType = typeof(T),
ConcreteType = typeof(C),
IsSingleTon = true,
Instance = instance
});
}
public T Get<T>()
{
return (T) Get(typeof(T));
}
public object Get(Type t)
{
ConstructorInfo constructor;
RegistryRecord r = null;
if (t.IsInterface && registry.ContainsKey(t))
{
r = registry[t];
if (r.IsSingleTon && r.Instance != null) return r.Instance;
constructor = r.ConcreteType.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
}
else
{
//todo how do we select the correct constructor?
constructor = t.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
}
var parameters = constructor.GetParameters();
//recurse to build dependency chain
var objects = parameters.Select(parameter => Get(parameter.ParameterType)).ToList();
var obj = constructor.Invoke(objects.ToArray());
if (r != null && r.IsSingleTon)
{
r.Instance = obj;
}
return obj;
}
}
internal class RegistryRecord
{
public Type InterfaceType { get; set; }
public Type ConcreteType { get; set; }
public object Instance { get; set; }
public bool IsSingleTon { get; set; }
}
So the problem is
constructor = t.GetConstructors(BindingFlags.Instance | BindingFlags.Public)[0];
I am just assuming the first constructor which is awful. But I have the definition of the interface I could be using.
How do I get the parameters of my interface and check them against the constructor?
Would like to select a constructor that matches the interface, or at least partially matches optimally.
edit
An example
using System;
namespace DITest
{
internal class Program
{
private static void Main(string[] args)
{
var di = DiContainer.GetInstance();
//Register classes / interfaces
di.Register<IPhoneResolver, PhoneResolver>();
di.Register<Customer>();
//Get class where dependency should be injected
var x = di.Get<Customer>();
Console.WriteLine(x.resolver.Name);
Console.Read();
}
}
public class Customer
{
//Remove this and everything is ok. Because we select the first one not the right one
public Customer()
{
}
public Customer(IPhoneResolver resolver)
{
this.resolver = resolver;
}
public IPhoneResolver resolver { get; set; }
}
public interface IPhoneResolver
{
string Name { get; set; }
bool DoesSomething();
}
public class PhoneResolver : IPhoneResolver
{
public string Name { get; set; } = "test";
public bool DoesSomething()
{
return true;
}
}
}
So because the first constructor is null there is an issue.
I need a way to resolve the correct constructor. I have the interface via the RegistryRecord and (type) InterfaceType. I need to find a way to get a constructor that matches that types parameters.
I cant make working the code below.. Do I need other class that impolement my IComponent with paratmeterless consturctor?
public class Program
{
public static void Main()
{
var lazy = new Lazy<IComponent>();
IComponent comp = lazy.Value;
var client = new ComponentClient(comp);
client.Run();
}
}
public interface IComponent
{
void Something();
}
public class LazyComponent : IComponent
{
public Lazy<IComponent> _LazyComponent { get; set ;}
public LazyComponent(Lazy<IComponent> lazyComponent)
{
_LazyComponent = lazyComponent;
}
public void Something()
{
_LazyComponent.Value.Something();
}
}
public class ComponentClient
{
public IComponent _Component { get; set; }
public ComponentClient(IComponent component)
{
_Component = component;
}
public void Run()
{
_Component.Something();
}
}
You need to tell the Lazy how to construct the component, by giving it a factory method.
https://learn.microsoft.com/en-us/dotnet/api/system.lazy-1?view=netframework-4.8
public class Program
{
public static void Main()
{
var lazy = new Lazy<IComponent>(() => new RealComponent());
var lazyComponent = new LazyComponent(lazy);
var client = new ComponentClient(lazyComponent);
client.Run();
}
}
First time using MS Unity. I have a controller with the following constructor:
protected IAdministrationService AdministrationService { get; set; }
public GenerateCacheController(IAdministrationService administrationService)
{
AdministrationService = administrationService;
}
I get the following error when trying to run the project:
Make sure that the controller has a parameterless public constructor.
In my Bootstrpper.cs file I have the following in the RegisterTypes method:
container.RegisterType<GenerateCacheController>();
I still get the error. Am I missing anything else? I'm using ASP.NET MVC 5 and Unity 3.
Here's my Boostrapper.cs file:
public static class Bootstrapper
{
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterInstance(container);
var im = new InjectionMember[0];
container.RegisterType<IAdministrationService, AdministrationService>("AdministrationService", im);
container.RegisterType<ILookupMapper, LookupMapper>("LookupMapper", im);
container.RegisterType<IEmailService, EmailService>("EmailService", im);
container.RegisterType<GenerateCacheController>();
var provider = new UnityServiceLocator(container);
ServiceLocator.SetLocatorProvider(() => provider);
}
}
Abbreviated version of the AdministrationService class:
public class AdministrationService : IAdministrationService
{
protected ILookupMapper LookupMapper { get; set; }
protected IEmailService EmailService { get; set; }
public AdministrationService(ILookupMapper lookupMapper, IEmailService emailService)
{
LookupMapper = lookupMapper;
EmailService = emailService;
}
}
Found the issue.
I commented out the line:
var im = new InjectionMember[0];
container.RegisterType<IAdministrationService, AdministrationService>("AdministrationService", im);
and added:
container.RegisterType<IAdministrationService, AdministrationService>();
And that worked because the previous developers were doing something like this:
private IUnityContainer Container { get; set; }
public AdministrationService()
{
Container = Microsoft.Practices.ServiceLocation.ServiceLocator.Current.GetInstance<IUnityContainer>();
}
instead of
protected ILookupMapper LookupMapper { get; set; }
protected IEmailService EmailService { get; set; }
public AdministrationService(ILookupMapper lookupMapper, IEmailService emailService)
{
LookupMapper = lookupMapper;
EmailService = emailService;
}
I have to go back to their way to not break existing code. I'll get around to refactoring one day.
Original Source Code
I've got a simple business object in my BusinessObjects.dll file:
namespace BusinessObjects
{
public class MyClass
{
public MyClass()
{
DateTime = DateTime.Now;
}
public DateTime DateTime { get; set; }
}
}
In my SharedUI.dll I've got this "Context-provider" class, that I use to hold a referece to the currently selected MyClass - remember this is a simplyfied example :)...
namespace SharedUI
{
public class AppContext
{
[Export]
public MyClass SelectedMyClass { get; private set; }
public void SetupContext(MyClass myClass)
{
SelectedMyClass = myClass;
}
public static AppContext Context
{
get
{
if (context == null)
{
context = new AppContext();
}
return context;
}
}
private static AppContext context;
}
}
My MefTest.exe has this class:
namespace MefTest
{
public class Program
{
[Import]
public MyClass MyClass { get; set; }
private void Compose()
{
var ventSystem = new MyClass();
AppContext.Context.SetupContext(ventSystem);
var executingAssembly = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var contextAssembly = new AssemblyCatalog(Assembly.LoadFile(string.Format(#"{0}\SharedUI.dll", Environment.CurrentDirectory)));
var catalog = new AggregateCatalog(executingAssembly, contextAssembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
private void Run()
{
Compose();
// MyClass is always null in the next line?
Console.WriteLine(MyClass.DateTime.ToString());
Console.ReadKey();
}
private static void Main(string[] args)
{
var p = new Program();
p.Run();
}
}
}
I'm a MEF rookie so please bear with me :)
UPDATED Source Code with suggestions from Daniel Plaisted
MyClass source is the same...
SharedUI.dll now looks like this:
namespace SharedUI
{
[Export]
public class AppContext
{
[Export(typeof(MyClass))]
public MyClass SelectedMyClass { get; private set; }
public void SetupContext(MyClass myClass)
{
SelectedMyClass = myClass;
}
}
}
MefTest.exe now looks like this:
namespace MefTest
{
public class Program
{
[Import]
public MyClass MyClass { get; set; }
[Import]
public AppContext AppContext { get; set; }
private void Compose()
{
var executingAssembly = new AssemblyCatalog(Assembly.GetExecutingAssembly());
var contextAssembly = new AssemblyCatalog(Assembly.LoadFile(string.Format(#"{0}\SharedUI.dll", Environment.CurrentDirectory)));
var catalog = new AggregateCatalog(executingAssembly, contextAssembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
var myClass = new MyClass();
AppContext.SetupContext(myClass);
}
private void Run()
{
Compose();
// AppContext.SelectedMyClass is NOT null in the next line... which is good I guess :)
Console.WriteLine(AppContext.SelectedMyClass.DateTime.ToString());
// MyClass is always null in the next line?
Console.WriteLine(MyClass.DateTime.ToString());
Console.ReadKey();
}
private static void Main(string[] args)
{
var p = new Program();
p.Run();
}
}
}
What am I doing wrong since I can't get it working?
When MEF needs to get an Export which is on a property of a class, it will create an instance of the class and call the property getter. So MEF is creating a new instance of your AppContext, different than the static AppContext.Context instance. The instance MEF creates doesn't have the SelectedMyClass property set on it, which is why your import ends up being null.
The problem is:
[Import] public MyClass MyClass { get; set; }
There are no [Export]s defined for MyClass. MEF will compose this appplication based on stuff it "knows", and since it does not know "MyClass"...
I noticed this one:
[Export] public MyClass SelectedMyClass { get; private set; }
This means you are trying to trick MEF into updating one of its parts from time to time? The solution to this would be to create a custom Catalog which contains "runtime" objects, in which you can updated the exported value for MyClass whenever you want. The current implementation will never resolve MyClass...
[edited:]
You can decorate a member as well, but you'll have to add the class type there. So this will work:
[Export(typeof(MyClass)] public MyClass SelectedMyClass { get; private set; }
You put your Export attribute in the wrong place.
You should put it on the definition of MyClass like so:
namespace BusinessObjects
{
[Export]
public class MyClass
{
public MyClass()
{
DateTime = DateTime.Now;
}
public DateTime DateTime { get; set; }
}
}
And then use the [Import] attribute wherever you want an instance of this class.
Remark: You cannot use MEF to move a specific instance of a class (not like this).
MEF is used to create instances of a requested type and inject them at indicated places.
To learn more about MEF check out the project's page at CodePlex.