c# - How IAclModule works -
i want know how class iaclmodule works, mean process pass, it's instanceaded 1 each loop verify access user, or it's used same instance each time?
i inquire because need implement own logic since default authorizeattributeaclmodule , xmlrolesaclmodule have been wayyy slow.
thx.
the aclmodule instantiated di (dependency injection) container. when using internal di container, instantiated sitemapfactorycontainer.resolveaclmodule method. however, whether using internal or external di, instance kept live lifetime of sitemap object (and hence cache) beingness stored in private field of sitemappluginprovider class, in turn in private field of sitemap class. therefore, there 1 instance per sitemap default gets created each time cache expires (1 time every 5 minutes default).
the authorizeattributeaclmodule has been tested pretty thoroughly , has been optimized pretty well. however, creates instance of related controller class , authorizeattribute class each node, if controllers or custom implementations of authorizeattribute doing much work in constructor, run performance issues.
to prepare this, should aim inject dependencies each controller through controller constructor instead of doing heavy lifting within of constructor. if utilize dependency injection container in conjunction custom icontrollerfactory, can command lifetime of dependencies externally controller. shown in illustration below, when using approach utilize same instance of imyrepository every instance of mycontroller.
public class mycontroller : controller { public mycontroller(imyrepository repository) { if (repository == null) throw new argumentnullexception("repository"); this.repository = repository; } private readonly imyrepository repository; public actionresult index() { var items = this.repository.getlist() homecoming view(items); } }
that recommended best practice design of controllers, in pinch, request-cache dependencies if expensive create each controller instance won't time-consuming create if instatiated more 1 time per request.
public class mycontroller : controller { public mycontroller() { this.repository = this.getorcreaterepository(); } private readonly imyrepository repository; private imyrepository getorcreaterepository() { var key = "mycontrollerrepository"; var result = httpcontext.items[key]; if (result == null) { // if expensive dependency wasn't created request, result = new myrepository(); // save instance in request, next time controller created, // doesn't have instantiate again. httpcontext.items[key] = result; } homecoming result; } public actionresult index() { var items = this.repository.getlist() homecoming view(items); } }
also, if have custom authorizeattribute(s) should create sure not doing work except checking whether user authorized, delegating real work handler same way microsoft does.
c# mvcsitemapprovider
No comments:
Post a Comment