public interface ICustomerService { void AddCustomer(Customers customer); }
public class CustomerService : ICustomerService {
IRepository<Customers> _customerRepository;
public CustomerService(IRepository<Customers> customerRepository) { if (customerRepository == null) throw new ArgumentNullException("customerRepository");
_customerRepository = customerRepository; }
public void AddCustomer(Customers customer) { IUnitOfWork unitOfWork = _customerRepository.UnitOfWork as IUnitOfWork;
_customerRepository.InsertOnSubmit(customer);
//Complete changes in this unit of work unitOfWork.SubmitChanges(); }
}
The Unity container shows all the registered types and their mappings but I get an Error Message:
The current type, ICustomerService, is an interface and cannot be constructed. Are you missing a type mapping?
So are you telling me there is a difference in the way you register the types?
_unityContainer.RegisterType<ICustomerService, CustomerService>) new TransientLifetimeManager());
should be the same as
_unityContainer.RegisterType(typeof(ICustomerService), typeof(CustomerService), new TransientLifetimeManager());
The reason why I'm doing it this way, is because I have created a method that does the autoregistration and I should be able to do it with the _unityContainer.RegisterType(typeof(ICustomerService), typeof(CustomerService), new TransientLifetimeManager());
My problem is why does the container shows all my registered types, but I cannot resolve them?
sillysoumare
Member
17 Points
61 Posts
Unity Container unable to resolve a type
Jun 09, 2011 04:20 AM|LINK
IUnityContainer _unityContainer = new UnityContainer();
_unityContainer.RegisterType(typeof(ICustomerService), typeof(CustomerService), new TransientLifetimeManager());
_unityContainer.RegisterType(typeof(IRepository<Customer>), typeof(Repository<Customer>));
Then I use it like this in a class
nvanhaaster@...
Star
9209 Points
1484 Posts
Re: Unity Container unable to resolve a type
Jun 09, 2011 05:28 AM|LINK
Hello sillsoumare,
I just started producing a video on the Unity containers. However i typically register my types with the following syntax.
This should fix the problem, however the TransientLifeTimeManager doesnt really do anything but ensure a new object is created every time.
Hope this helps.
My .Net Blog
My Links are shrunk by t-ny.co
sillysoumare
Member
17 Points
61 Posts
Re: Unity Container unable to resolve a type
Jun 09, 2011 12:08 PM|LINK
_unityContainer.RegisterType(typeof(ICustomerService), typeof(CustomerService), new TransientLifetimeManager());
The reason why I'm doing it this way, is because I have created a method that does the autoregistration and I should be able to do it with the _unityContainer.RegisterType(typeof(ICustomerService), typeof(CustomerService), new TransientLifetimeManager());
My problem is why does the container shows all my registered types, but I cannot resolve them?
Please help me.