inversion-of-control

MVC3, Ninject, MvcSiteMapProvider - How to inject dependency to overridden method

做~自己de王妃 提交于 2019-12-05 04:25:07
I have an MVC3 application that is using Ninject and MvcSiteMapProvider . I have created this class which MvcSiteMapProvider uses to dynamically add nodes to my sitemap: public class PageNodeProvider : DynamicNodeProviderBase { public override IEnumerable<DynamicNode> GetDynamicNodeCollection() { // need to get repository instance var repository = // how do I get this??? foreach (var item in repository.GetItems()) { yield return MakeDynamicNode(item); } } } The MvcSiteMapProvider instantiates this type itself, so I'm not sure how to inject my repository into it. I thought about using service

Name of Design Pattern: get class from class level

我怕爱的太早我们不能终老 提交于 2019-12-05 04:14:57
Especially in unittests we use this "design pattern" I call "get class from class level" framworktest.py: class FrameWorkHttpClient(object): .... class FrameWorkTestCase(unittest.TestCase): # Subclass can control the class which gets used in get_response() HttpClient=FrameWorkHttpClient def get_response(self, url): client=self.HttpClient() return client.get(url) mytest.py: class MyHttpClient(FrameWorkHttpClient): .... class MyTestCase(FrameWorkTestCase): HttpClient=MyHttpClient def test_something(self): response=self.get_response() ... The method get_response() gets the class from self not by

Best practices for IoC in complex service layer [duplicate]

大兔子大兔子 提交于 2019-12-05 02:48:13
问题 This question already has answers here : How to avoid Dependency Injection constructor madness? (9 answers) Closed 4 years ago . I'm developing an MVC application, I'm using Unity for IoC. My Application basically consists of a UI layer, a services layer and a repository layer. My typical controller is: public class TestController : Controller { private ITestService testServ; public TestController(ITestService _testServ) { testServ= _testServ; } public ActionResult Index() { testServ

How do I use Ninject with ActionResults while making the controller IoC-framework-agnostic?

送分小仙女□ 提交于 2019-12-05 02:46:27
问题 Nearly all of the Ninject examples I've seen explain how to use it with ASP.NET MVC, which will automatically inject dependencies into controllers. How would I use Ninject manually though? Let's say I have a custom ActionResult: public class JsonResult : ActionResult { [Inject] public ISerializer Serializer { get; set; } public JsonResult(object objectToSerialize) { // do something here } // more code that uses Serializer } Then in my controller, I'm using JsonResult in a method like this:

I am looking for a simple yet practical and robust IOC/DI framework for .net

天大地大妈咪最大 提交于 2019-12-05 02:28:09
问题 I am going to use it in a project with less-experienced developers so a complex framework such as Spring.NET is not an option. I was thinking about: Ninject Castle Windsor StructureMap Which would present a moderate learning curve without losing flexibility? and another question - where is the correct place to put the configuration? Since the kernel/configuration on a 3-tier ASP.NET application (not MVC!!! all examples are using MVC :) ) 回答1: The great thing about proper use of DI is that you

UnitOfWork in Action Filter seems to be caching

僤鯓⒐⒋嵵緔 提交于 2019-12-05 01:37:26
I have an MVC 3 site that uses IoC (Unity), and my model is generated w/ EF4 and POCOs. I am using an action filter to commit my UnitOfWork: public class UseUnitOfWorkAttribute : ActionFilterAttribute, IActionFilter { private readonly IUnitOfWork _unitOfWork; public UseUnitOfWorkAttribute() { _unitOfWork = IoCFactory.Instance.CurrentContainer.Resolve<IUnitOfWork>(); } void IActionFilter.OnActionExecuted(ActionExecutedContext filterContext) { _unitOfWork.Commit(); } void IActionFilter.OnActionExecuting(ActionExecutingContext filterContext) { } } However, even though the Commit() seems to be

Restlet server resource with constructor parameters needed

守給你的承諾、 提交于 2019-12-05 01:34:03
Getting this error in restlet: ForwardUIApplication ; Exception while instantiating the target server resource. java.lang.InstantiationException: me.unroll.forwardui.server.ForwardUIServer$UnsubscribeForwardUIResource And I know exactly why. It's because my constructor looks like this: public UnsubscribeForwardUIResource(MySQLConnectionPool connectionPool) { And Restlet accesses the resource like so: router.attach(Config.unsubscribeUriPattern(), UnsubscribeForwardUIResource.class); Problem is I actually need that ctor argument. How can I make it accessible? (Note I'm not using any IOC

Logging framework being injected into classes

那年仲夏 提交于 2019-12-05 01:18:18
问题 Should a logging framework be injected into classes that need them or should every class that needs it just know the framework and use it directly? I am using Log4Net and currently I am injecting a service that wraps that framework into the classes that need to be able to log, knowing logging is probably not going to change and that most pieces need it, is injecting the answer in this sense? 回答1: Injection is more flexible in the long run, since you can easily selectively inject into certain

How to inject dependencis into WCF Attribute with Simple Injector

廉价感情. 提交于 2019-12-05 00:20:28
问题 I have a bunch of WCF services that works with REST and SOAP. I have created an WCF attribute who checks if the current httpcontext exists, if exists it use cookie authentication, other way it use custom WCF authentication. My attribute looks like this: Public Class AuthRequired Inherits Attribute Implements IOperationBehavior, IParameterInspector Public Sub AddBindingParameters(operationDescription As OperationDescription, bindingParameters As Channels.BindingParameterCollection) Implements

Using Ninject IOC to replace a factory

為{幸葍}努か 提交于 2019-12-04 23:40:13
I've got a factory method inside a parser. Essentially as I load a token I look up the handler for that token, or drop through to the default handler. I've implemented this as a switch and as a Dictionary<string,Type> but both approaches require me to store the mapping somewhere else than the handler class. We are using Ninject for IOC and so I've realized I can also do it using kernel.Get<ITokenHandler>(tokenName); but that doesn't save me storing the information on what token the handler can deal with in 2 locations. Is there a way I can decorate the handler so this gets mapped automatically