UnityConfig + .RegisterType<IService<T>,Service<T>)

Oct 17, 2014 at 3:05 PM
Edited Oct 17, 2014 at 3:12 PM
Just a matter of clarification for your line of thinking, but why can I do this:
container.RegisterType<IRepositoryAsync<T>, Repository<T>>()
But not this:
               container.RegisterType<IService<T>, Service<T>>()
Coordinator
Oct 17, 2014 at 3:11 PM
Because Service.cs is an abstract class, which means it can only be inherited and cannot be directly instantiated, this is by design, so that you will have a place in your architecture/design to place business logic pre and post of your CRUD.
Marked as answer by lelong37 on 10/17/2014 at 7:11 AM
Coordinator
Oct 17, 2014 at 3:18 PM
Example: https://genericunitofworkandrepositories.codeplex.com/wikipage?title=Services%20Pattern%20Example&referringTitle=Documentation

If you absolutely know that you will not have any specific implementation in the future and don't need Services pattern for business logic (few teams can predict this and end up refactoring down the line) you can just inject IRepository or IRepositoryAsync which you can register:

container.RegisterType<IRepositoryAsync<T>, Repository<T>>()

based on which DI/IoC framework you are using.
Marked as answer by lelong37 on 10/17/2014 at 7:18 AM
Oct 17, 2014 at 3:28 PM
Understood. I suppose I'm trying to only use the Services pattern, for that very reason. But I don't know the specific BL yet, so I was hoping to be able to use a generic service in my own Service project.

For instance, suppose I wanted to always soft delete instead of hard delete data for every model. I could override the Delete method in a single generic Service to set a common IsDeleted flag instead of _repo.Delete()...

To do so would require modifying the abstract Entity class. What else might be required for such a use case?
Coordinator
Oct 17, 2014 at 3:47 PM
You could implement this in abstract Service or Repository/RepositoryAsync classes however this would dictate that every entity would do a soft delete moving forward, I would still implement the full Service pattern and for each entity that requires a soft delete, override Delete in each Service e.g. IProductService, ProductService and in ProductService do a soft delete vs. a hard one.
Marked as answer by lelong37 on 10/17/2014 at 7:47 AM
Oct 17, 2014 at 3:49 PM
Okay. Thanks for your help and quick responses! I'm very glad to have found it. Good stuff!