c# - Binding autofac with webapi using generic repository -
i trying utilize autofac repository , trying add together little generics seek reducing amount of duplicate code writing.however going round in circles trying autofac work me
so created domainservice , interface handles our 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() { homecoming repository.getqueryable(); } public virtual task<t> add(t entity) { homecoming 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 using repo nil 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 wire autofac:
var builder = new containerbuilder(); // register 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 container. var container = builder.build(); // create depenedency resolver. var resolver = new autofacwebapidependencyresolver(container); // configure web api 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 tentity : new() { protected readonly idomainservice<tentity> domainservice; protected basecontroller(idomainservice<tentity> domainservice) { domainservice = domainservice; }
i exception:
"none of constructors found 'autofac.core.activators.reflection.defaultconstructorfinder' on type 'api.endpoints.skills.skillscontroller' can invoked available services , parameters:\ \ cannot resolve parameter 'domain.repository.skillrepository skillrepository' of constructor 'void .ctor(domain.repository.skillrepository)'."
is there obvious doing wrong?
it cannot resolve dependency because it's looking concrete type never registered skillsrepository
that. alter registration register concrete type wouldn't best approach.
a improve approach register skillsrepository interfaces:
builder.registertype<skillrepository>() .as<iskillsrepository>() .instanceperrequest();
and define iskillsrepository
inherit other interfaces iskill
want.
public interface iskillsrepository : iskill, idomainservice<skill> { }
don't register objects concrete types , don't depend on concrete types in constructors.
public skillscontroller(iskillrepository skillrepository) : base(skillrepository) ...
if utilize concrete types dependencies create classes cannot tested using mocking frameworks.
your utilize of skillrepository : domainservice<skill>, iskill
perplexing too. why both skill , domain service skills? doesn't create much sense.
c# asp.net-web-api dependency-injection autofac
No comments:
Post a Comment