IUnitOfWork to DomainService constructor using Unity container - c#

I created Silverlight application and I want to realise it via WCF RIA Services. There are 3 projects in my solution:
Data access layer library which contains all db logic and entities. I will use IUnitOfWork interface to communicate with it:
public interface IUnitOfWork : IDisposable
{
IRepository<Customer> Customers { get; }
IRepository<Product> Products { get; }
IRepository<Order> Orders { get; }
void Save();
}
WCF RIA Services project where I created custom DomainService class. Its constructor takes IUnitOfWork interface parameter:
[EnableClientAccess()]
public void StoreService : DomainService
{
private IUnitOfWork _repository;
public StoreService(IUnitOfWork repository)
{
_repository = repository;
}
// ... some methods to query _repository
}
Client project (its written in WPF).
So, I want to use Unity IoC container to path interface implementation into service. I can't understand where need to create custom service factory or something like that and where to register it to be used by system. For example, I know that in ASP.NET MVC there is DefaultControllerFactory class which I need to derive. Then put my IoC bindings in it and then register it in Global.asax.cs file. Can you help me, please. Thanks.

The DomainService exposes a static property called DomainService.DomainServiceFactory.
You'll need a custom class that implements IDomainServiceFactory
interface IDomainServiceFactory
{
DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context);
void ReleaseDomainService(DomainService domainService)
}
I've copied and pasted a blog post from Fredrik Normen how to wire up Unity to DomainService.
public class MyDomainServiceFactory : IDomainServiceFactory
{
public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
{
var service = Global.UnityContainer.Resolve(domainServiceType) as DomainService;
service.Initialize(context);
return service;
}
public void ReleaseDomainService(DomainService domainService)
{
domainService.Dispose();
}
}
In your Global.asax
protected void Application_Start(object sender, EventArgs e)
{
DomainService.Factory = new MyDomainServiceFactory();
UnityContainer.RegisterType<StoreService>();
UnityContainer.RegisterType<IUnitOfWork, UnitOfWorkImpl>();
}

Related

asp.net core dependency passing dependencies along

I'm trying to inject a dependency into a class used internally in a controller,
I have
//Startup.cs
public void ConfigureServices(IServiceCollection services){
services.AddMvc();//IDocumentService is a WCF service from our legacy stack
services.AddScoped(typeof(IDocumentService),typeof(DocumentServiceClient));
}
//Controller.cs
[Route("api/eci/test/[action]")]
public class Controller{
private IDocumentService injectedDocService;
public Controller(IDocumentService client){
injectedDocService=client;
}
[HttpPost({"id"})]
public void ingestedDocs(string id){
new Logic(injectedDocService).ingest(id);
}
}
//Logic.cs
public class Logic{
private IDocumentService injectedDocServiceActualTarget;
public Logic(IDocumentService injected2){
injectedDocServiceActualTarget=injected2;
}
public void injest(string id){
injectedDocServiceActualTarget.doWork(id);
}
}
It seems a little redundant to have it injected to the target class's parent. Is this the right way of doing things?
You need to register the Logic, then inject it to the controller. DocumentService will be injected to the Logic then.
The idea behind dependency injection is to implement the IoC (inversion of control) principle. In your example, it is only partial, since you explicitly instantiate Logic in your controller. If you want to do the dependency inversion properly - all your dependencies need to be passed as constructor parameters.
You should abstract the Logic behind an interface, only exposing the members that are to be used by dependents.
public interface ILogic {
void injest(string id);
}
Have the Logic class derive from the abstraction.
//Logic.cs
public class Logic : ILogic {
private readonly IDocumentService injectedDocServiceActualTarget;
public Logic(IDocumentService injected2) {
this.injectedDocServiceActualTarget=injected2;
}
public void injest(string id) {
injectedDocServiceActualTarget.doWork(id);
}
}
The Controller should now only explicitly depend on the ILogic interface
//Controller.cs
[Route("api/eci/test/[action]")]
public class Controller {
private readonly ILogic service;
public Controller(ILogic service) {
this.service = service;
}
[HttpPost({"id"})]
public void ingestedDocs(string id) {
service.ingest(id);
}
}
With that the last thing to do is to register all dependencies with the service collection.
//Startup.cs
public void ConfigureServices(IServiceCollection services){
services.AddMvc();
services.AddScoped<IDocumentService, DocumentServiceClient>();
services.AddScoped<ILogic, Logic>();
}
So now when the controller is called all dependencies will be resolved and injected into their respective dependents.

Resolving dependencies dynamically using Autofac

Is it good to resolve the dependencies dynamically like the way i'm doing. Everywhere, it is suggested to use Constructor injection. I really don't understand the drawbacks of doing it the way i'm doing it. Code snippets as below..
Employee.cs
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public Department Department { get; set; }
}
IRepository.cs
public interface IRepository<TModel> where TModel : class
{
void Add();
IEnumerable<TModel> GetAll();
IEnumerable<TModel> GetByID();
}
Repository.cs
public class Repository<TModel> : IRepository<TModel> where TModel : class
{
public void Add()
{
throw new NotImplementedException();
}
public IEnumerable<TModel> GetAll()
{
throw new NotImplementedException();
}
public IEnumerable<TModel> GetByID()
{
throw new NotImplementedException();
}
}
EmployeeController.cs
public class HomeController : ApiController
{
IComponentContext _container;
public HomeController(IComponentContext container)
{
this._container = container;
}
public Repository<TModel> Using<TModel>() where TModel :class
{
var repository = _container.Resolve(typeof(IRepository<TModel>));
return repository as Repository<TModel>;
}
[HttpGet]
public IEnumerable<Employee> GetEmployees()
{
return Using<Employee>().GetAll();
}
}
Global.asax
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
var container = builder.Build(Autofac.Builder.ContainerBuildOptions.None);
var webApiResolver = new AutofacWebApiDependencyResolver(container);
GlobalConfiguration.Configuration.DependencyResolver = webApiResolver;
}
Say i've 5 repositories, Constructor injection will resolve all the 5 dependencies for a request i make. I might not use 5 repositories for each and every request. SO i thought of resolving dependencies dynamically by passing the type like i'm doing it in Using<TModel>(). Any suggestions would be appreciated..!! Thank you...!!
Refrain from using the container directly inside your application components; this leads to all kinds of troubles such as maintainability and testability issues. Directly resolving instances from within application code is a well-known anti-pattern known as Service Locator.
As a first refactoring, you can instead apply the Unit of Work pattern. A Unit of Work allows access to underlying repositories. For instance:
public interface IUnitOfWork
{
IRepository<TModel> Repository<TModel>();
}
public sealed class HomeController : ApiController
{
private readonly IUnitOfWork _unitOfWork;
public HomeController(IUnitOfWork unitOfWork)
{
this._unitOfWork = unitOfWork;
}
[HttpGet]
public IEnumerable<Employee> GetEmployees()
{
return this._unitOfWork.Repository<Employee>().GetAll();
}
}
Within the Composition Root (where it is allowed to access the container), we can now create an IUnitOfWork implementation that resolves repositories dynamically:
private sealed class AutofacUnitOfWork : IUnitOfWork
{
private readonly IComponentContext _container;
public AutofacUnitOfWork(IComponentContext container)
{
this._container = container;
}
public IRepository<TModel> Repository<TModel>()
{
return _container.Resolve<IRepository<TModel>>();
}
}
This pattern simplifies your application components considerably and prevents downsides that the Service Locator anti-pattern typically causes.
Although applying the Unit of Work pattern might be a useful step into the right direction, an even better approach is to skip the Unit of Work directly and simply inject a required repository directly into application components:
public sealed class HomeController : ApiController
{
private readonly IRepository<Employee> _employeeRepository;
public HomeController(IRepository<Employee> employeeRepository)
{
this._employeeRepository = employeeRepository;
}
[HttpGet]
public IEnumerable<Employee> GetEmployees()
{
return this._employeeRepository.GetAll();
}
}
Say i've 5 repositories, Constructor injection will resolve all the 5 dependencies for a request i make. I might not use 5 repositories for each and every request.
Note that from a performance perspective, you should typically not be concerned whether dependencies are used or not. Autofac is in most cases fast enough and it is unlikely that this will actually cause any performance problems in your production systems.
From a design perspective however you should be more worried if a class has many dependencies, while methods just use a few of them. This means that the methods in the class have little cohesion. This is an indication that the class should be split up into multiple smaller classes; it has multiple responsibilities.

Windsor Typed factory to create objects with same dependencies as parent

My Wpf application has a lot of different view models, that depend on repositories and command factories, registered with a transient lifestyle bound to the view model. Commands are created with a typed factory which will always create new instances of dependencies. That can be a problem because of tracking issues in ORMs when the view model depends on a repository and a command that also depends on that repository.
For that reason I want my command factories to create commands with the same instance of repositories as the view model.
How can this be solved by the container without manually creating factories?
Edit - Example with runnable code
public class Service
{
private readonly IRepository repository;
private readonly ICommandFactory commandFactory;
public Service(IRepository repository, ICommandFactory commandFactory)
{
this.repository = repository;
this.commandFactory = commandFactory;
}
public void DoIt()
{
repository.SaveChanges();
commandFactory.Create().Execute();
}
}
public interface ICommandFactory
{
ICommand Create();
}
public interface ICommand
{
void Execute();
}
public class Command : ICommand
{
private readonly IRepository repository;
public Command(IRepository repository)
{
this.repository = repository;
}
public void Execute()
{
repository.SaveChanges();
}
}
public interface IRepository
{
void SaveChanges();
}
public class Repository : IRepository
{
private static int i = 0;
private int instanceNumber = 0;
public Repository()
{
instanceNumber = i++;
Console.WriteLine("Created repository nr: " + instanceNumber);
}
public void SaveChanges()
{
Console.WriteLine("Save changes for repository nr: " + instanceNumber);
}
}
class Program
{
static void Main(string[] args)
{
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<Service>().LifestyleTransient());
container.Register(Component.For<IRepository>().ImplementedBy<Repository>().LifestyleBoundTo<object>());
container.Register(Component.For<ICommand>().ImplementedBy<Command>().LifestyleTransient());
container.Register(Component.For<ICommandFactory>().AsFactory().LifestyleTransient());
var service = container.Resolve<Service>();
service.DoIt();
}
}
As you are using transient lifestyle for all dependencies, it is obvious that you have different instances of IRepository in Service and Command classes. What you need, is to increase lifetime of IRepository and probably bind it to lifetime of your service, meaning that the instance of IRepository is created and disposed when the instance of Service is created and disposed.
Castle Windsor has Bound lifestyle:
Somewhere in the graph we have two view models, one depending on the other, and both of them depend on some other service, say a repository. You might want to bind the repository to the subgraph. In other words you might want the entire subgraph of the outermost view model (WelcomeScreenViewModel) to share the same instance of the repository, and to have the repository released when the view model itself is released.
So should register your IRepository in the following way:
Container.Register(Component.For<IRepository>().ImplementedBy<Repository>().LifestyleBoundTo<Service>());
EDIT:
The above solution will not work as ICommand is bound to ICommandFactory not to Service. If you are resolving service instance manually you can use LifestyleScoped:
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<Service>().LifestyleTransient());
container.Register(Component.For<IRepository>().ImplementedBy<Repository>().LifestyleScoped());
container.Register(Component.For<ICommand>().ImplementedBy<Command>().LifestyleTransient());
container.Register(Component.For<ICommandFactory>().AsFactory().LifestyleTransient());
using (container.BeginScope())
{
var service = container.Resolve<Service>();
service.DoIt();
}
In this case your IRepository is scoped per scope.
Note To use container.BeginScope() add using Castle.MicroKernel.Lifestyle;

Binding autofac with webapi using generic repository

I am trying to use autofac with a repository and I am trying to add a little generics to try reducing the amount of duplicate code I am writing.However I am going round in circles trying to get autofac to work for me
So I created a domainservice and interface that handles our the standard crud operations
public class DomainService<T>:IDomainService<T>
{
protected readonly IDomainService<T> Repository;
public DomainService(IDomainService<T> repository)
{
Repository = repository;
}
public IQueryable<T> GetQueryable()
{
return Repository.GetQueryable();
}
public virtual Task<T> Add(T entity)
{
return Repository.Add(entity);
}
Interface:
public interface IDomainService<T>
{
IQueryable<T> GetQueryable();
Task<T> Add(T entity);
Task<bool> Delete(T entity);
Task<T> Update(T entity);
Task<T> GetById(int id);
Task<T> GetByUID(Guid id);
}
I am using my repo is nothing special
public class SkillRepository : DomainService<Skill>, ISkill
{
private DataContext _db = new DataContext();
private readonly ILogger _log = null;
public SkillRepository(IDomainService<Skill> repository, ILogger log) : base(repository)
{
_log = log;
}
}
Finally where I wire up autofac:
var builder = new ContainerBuilder();
// Register the Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// Register other dependencies.
builder.Register(c => new Logger()).As<ILogger>().InstancePerApiRequest();
builder.RegisterType<SkillRepository>()
.As<IDomainService<Skill>>()
.As<ISkill>()
.InstancePerRequest();
// Build the container.
var container = builder.Build();
// Create the depenedency resolver.
var resolver = new AutofacWebApiDependencyResolver(container);
// Configure Web API with the dependency resolver.
GlobalConfiguration.Configuration.DependencyResolver = resolver;
My web api controller looks like
public class SkillsController : BaseController<Skill>
{
private readonly ISkill _skillRepository;
public SkillsController(SkillRepository skillRepository) : base(skillRepository)
{
_skillRepository = skillRepository;
}
}
BaseController
public abstract class BaseController<TEntity> : ApiController
where TEntity : new()
{
protected readonly IDomainService<TEntity> DomainService;
protected BaseController(IDomainService<TEntity> domainService)
{
DomainService = domainService;
}
I get an exception:
"None of the constructors found with
'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type
'Api.EndPoints.Skills.SkillsController' can be invoked with the
available services and parameters:\ \ Cannot resolve parameter
'Domain.Repository.SkillRepository skillRepository' of constructor
'Void .ctor(Domain.Repository.SkillRepository)'."
Is there something obvious that I am doing wrong?
It cannot resolve the dependency because it's looking for the concrete type but you never registered SkillsRepository as that. Now you could change the registration to register the concrete type but that wouldn't be the best approach.
A better approach is to register SkillsRepository as its interfaces:
builder.RegisterType<SkillRepository>()
.As<ISkillsRepository>()
.InstancePerRequest();
And define ISkillsRepository to inherit all the other interfaces like ISkill that you want.
public interface ISkillsRepository : ISkill, IDomainService<Skill> { }
Don't register objects as concrete types and don't depend on concrete types in constructors.
public SkillsController(ISkillRepository skillRepository) :
base(skillRepository) ...
If you use concrete types as dependencies you create classes that cannot be tested using mocking frameworks.
Your use of SkillRepository : DomainService<Skill>, ISkill is perplexing too. Why is it both a skill and a domain service for skills? Doesn't make much sense.
Exception clearly states:
Cannot resolve parameter 'Domain.Interfaces.ISkill skillRepository' of constructor 'Void .ctor(Domain.IDomainService`1[Model.Skill], Domain.Interfaces.ISkill)'.
You have only IDomainService registered. But no ISkill (the line is commented).
Also why does the ctor require 2 parameters? SkillRepository implements both IDomainService<Skill> and ISkill so you should be able to pass it along:
public SkillsController(SkillRepository skillRepository) : base(skillRepository)
P.S.
I'd name it this way:
public class SkillRepository : ISkillRepository, IDomainService<Skill>
And I prefer everything to be either plural (SkillsControllers, SkillsRepository) or everything singular (SkillController, SkillRepository).
In my opinion you should first sort out names for your classes which is making it hard to understand the code itself. secondly your repository is implementing domain service interface and ISkill and things like that is adding more confusion. i am pretty sure if you organise your classes properly then you will find solution to your problem.
For instance ApiController should use domain service, domain service should use repository and repository should deal with enties.
public class SkillsDomainService:ISkillsDomainService
{
public void AddSkill(string name){}
public void DeleteSkillById(int id){}
..... etc
}
public class Repository:IRepository
{
public T Get(int id){}
public IEnumerable<T>GetAll(){}
}
Then you need to bind your interfaces to concrete classed in ioc. things should work that way i am pretty sure.

MVC 4 Autofac and Generic Repository pattern

I am utilizing the Unit Of Work and Generic Repository pattern in my MVC 4 app. The problem I am trying to solve is creating Repository stubs for every entity in my system. In order to utilize the Autofac Ioc I am having to create a repository class and interface for every entity so that I can register it in Autofac.
app start...
builder.RegisterType<SchoolDetailRepository>().As<ISchoolDetailRepository>().InstancePerHttpRequest();
Repository class
public class SchoolDetailRepository : RepositoryBase<SchoolDetail>, ISchoolDetailRepository
{
public SchoolDetailRepository(IDatabaseFactory databaseFactory) : base(databaseFactory)
{
}
}
Interface
public interface ISchoolDetailRepository : IRepository<SchoolDetail>
{
}
It seems like a lot of extra work.
Is there a way to register the generic repository of Type rather than creating all these empty classes?
Then in my service class I can just have the generic type passed into the constructor via Ioc like...
public class SchoolService : ISchoolService
{
private readonly IRepository<SchoolDetail> _schoolRepository;
private readonly IUnitOfWork _unitOfWork;
public SchoolService(IRepository<SchoolDetail> schoolRepository, IUnitOfWork unitOfWork)
{
this._schoolRepository = schoolRepository;
this._unitOfWork = unitOfWork;
}
}
Container config
// Autofac iOC
var builder = new ContainerBuilder();
// register controllers
builder.RegisterControllers(Assembly.GetExecutingAssembly());
// register services
builder.RegisterType<MembershipService>().As<IMembershipService>().InstancePerHttpRequest();
builder.RegisterType<SchoolService>().As<ISchoolService>().InstancePerHttpRequest();
builder.RegisterType<StudentService>().As<IStudentService>().InstancePerHttpRequest();
builder.RegisterType<ClassRoomService>().As<IClassRoomService>().InstancePerHttpRequest();
builder.RegisterType<CourseService>().As<ICourseService>().InstancePerHttpRequest();
builder.RegisterType<SchoolYearService>().As<ISchoolYearService>().InstancePerHttpRequest();
builder.RegisterType<EnrollmentService>().As<IEnrollmentService>().InstancePerHttpRequest();
builder.RegisterType<TeacherService>().As<ITeacherService>().InstancePerHttpRequest();
// register data infrastructure
builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();
builder.RegisterType<DatabaseFactory>().As<IDatabaseFactory>().InstancePerHttpRequest();
// register repositories
builder.RegisterType<SchoolRepository>().As<ISchoolRepository>().InstancePerHttpRequest();
builder.RegisterType<TeacherRepository>().As<ITeacherRepository>().InstancePerHttpRequest();
builder.RegisterType<MembershipRepository>().As<IMembershipRepository>().InstancePerHttpRequest();
builder.RegisterType<RoleRepository>().As<IRoleRepository>().InstancePerHttpRequest();
builder.RegisterType<ProfileRepository>().As<IProfileRepository>().InstancePerHttpRequest();
builder.RegisterType<UserRepository>().As<IUserRepository>().InstancePerHttpRequest();
builder.RegisterType<StudentRepository>().As<IStudentRepository>().InstancePerHttpRequest();
builder.RegisterType<ClassRoomRepository>().As<IClassRoomRepository>().InstancePerHttpRequest();
builder.RegisterType<CourseRepository>().As<ICourseRepository>().InstancePerHttpRequest();
builder.RegisterType<EnrollmentRepository>().As<IEnrollmentRepository>().InstancePerHttpRequest();
builder.RegisterType<SchoolYearRepository>().As<ISchoolYearRepository>().InstancePerHttpRequest();
builder.RegisterType<GradeLevelRepository>().As<IGradeLevelRepository>().InstancePerHttpRequest();
//builder.RegisterType<SchoolDetailRepository>().As<ISchoolDetailRepository>().InstancePerHttpRequest();
builder.RegisterGeneric(typeof(RepositoryBase<SchoolDetail>)).As(typeof(IRepository<SchoolDetail>));
// build and setup resolver
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
the exception is coming from the above code where the expression you gave me runs..
builder.RegisterGeneric(typeof(RepositoryBase<SchoolDetail>)).As(typeof(IRepository<SchoolDetail>));
RepositoryBase
public abstract class RepositoryBase<T> where T : class
{
private LearningCompactPilotContext _dataContext;
private readonly IDbSet<T> _dbset;
protected RepositoryBase(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
_dbset = DataContext.Set<T>();
}
protected IDatabaseFactory DatabaseFactory
{
get; private set;
}
protected LearningCompactPilotContext DataContext
{
get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
}
... more code
}
You need the open generics feature of Autofac:
builder.RegisterGeneric(typeof(RepositoryBase<>))
.As(typeof(IRepository<>));
Then you use your repositories exactly as you described:
public class SomeService
{
private readonly IRepository<SomeEntity> _repository;
public SchoolService(IRepository<SomeEntity> repository)
{
this._repository= repository;
}
}

Categories