与此不完全相同:
如何使用Ninject注入依赖项,其中实例从json反序列化
答案是,您反序列化的数据类无论如何都不需要服务.有没有办法使用依赖注入与派生的类JsonConverter?例如,如果你有这个:
[JsonConverter(typeof(MyCustomConverter))]
public class Foo
{
public string SomeProp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和:
public class MyCustomConverter : JsonConverter
{
private readonly IMyService myService;
public MyCustomConverter(IMyService _myService)
{
myService = _myService;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var bar = myService.SomeFunctionThatMightEffectDeserialization();
//...
}
}
Run Code Online (Sandbox Code Playgroud)
反正有没有想到JSON.Net实例化如何让它MyCustomConverter让Ninject做它的事情?
编辑这不是Foo像建议的欺骗那样注入服务.这只是注入,MyCustomConverter以便它可以反序列化Foo.
我有一个简单的过滤器.
public class IsAdmin : ActionFilterAttribute, IAuthenticationFilter
{
private string _roleName;
IBusinessIdentity _identity;
public IsAdmin(string roleName, IBusinessIdentity identity)
{
this._roleName = roleName;
this._identity = identity;
}
public void OnAuthentication(AuthenticationContext filterContext)
{
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
if (!_identity.Roles.Contains(_roleName))
filterContext.Result = new HttpUnauthorizedResult();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Ninject.这是我的控制器.我正在尝试将注入的服务放入我的ActionFilter中,以便不依赖HttpContext于我,而是依赖于我的IBusinessIdentity.
IBusinessIdentity获取注入HttpContext.User.Identity`.它会执行一些数据库调用并获取userRoles.
public class HomeController : Controller
{
readonly IBusinessIdentity _identity;
public HomeController(IBusinessIdentity identity)
{
this._identity= identity;
}
[IsAdmin("Admin", _identity)]
public ActionResult Index()
{
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,当我尝试在编译时将"identity"放在actionfilter构造函数中时,我遇到编译器错误.
非静态字段,方法或属性需要对象引用 …
asp.net asp.net-mvc dependency-injection ninject inversion-of-control
我有一个像这样的自定义操作过滤器:
public class MySecurityTest : ActionFilterAttribut{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//Do some security tests
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
我将其添加到FilterConfig所有操作中。但我需要一些操作在没有它的情况下工作。现在我使用这样的东西:
public class MySecurityTest : ActionFilterAttribute
{
public bool CheckRules { get; set; }
public MySecurityTest(bool checkRules = true)
{
CheckRules = checkRules;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (CheckRules)
{
//Do some security tests
}
base.OnActionExecuting(filterContext);
}
}
Run Code Online (Sandbox Code Playgroud)
和用法:
[MySecurityTest(false)]
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
但如何构建类似[AllowAnonymous]属性的东西
此致
我完成了建议的步骤,将Ninject添加到我的MVC应用程序中.我向DbContext控制器的构造函数添加了一个参数.
控制器:
public class MyController : BaseController
{
public ArticlesController(MyDbContext context)
: base(context)
{ }
}
Run Code Online (Sandbox Code Playgroud)
基础控制器:
public class BaseController : Controller
{
protected DbContext MyDbContext;
public BaseController(MyDbContext context)
{
MyDbContext = context;
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎运作良好.但是请给我一些问题.
Ninject能否确保我DbContext的清理和及时处理?
我为所有应用程序的控制器创建了一个基类来处理任何常见的初始化等.基类DbContext在构造函数中接受我的参数的实例.但这需要我也将此参数添加到我的应用程序中的每个控制器.有没有办法不要求这个?
我不确定创建一个我的实例是多么昂贵DbContext.是否有任何方法可以进行优化,只有在请求实际要求我访问数据库时才会创建它.
我正在使用CastleWindsor作为我的依赖注入框架,当你在Controller中时它们都运行良好,因为我们可以使用controllerfactory构造函数注入.
但是有一些特定的情况,依赖注入(构造函数注入)将不起作用.例如:我希望能够在我的视图中的一些实用程序类或扩展方法(例如HtmlHelper)中使用IOC来解析依赖关系.我知道有些人不同意这一点,而是保持观点愚蠢,但让我们继续讨论.
所以这基本上让我有一个选项,那就是使用...服务定位器.所以我知道服务定位器被大多数人认为是反模式,我明白为什么..但是如果不能使用依赖注入,如何解决与IOC的依赖关系?对于我所知道的一切,使用IOC的服务定位器比没有任何东西更好.我想避免服务定位器模式,但我似乎不明白在某些特定情况下如何避免它.
接下来的问题是..所以即使你喜欢/不喜欢服务定位器.哪个是使用CastleWindsor实现此功能的最佳选择?
所以我猜选项是:
将容器公开为全局对象(或通过包装容器的其他全局对象),您可以从代码中的任何位置检索该容器.然后,您可以在容器上调用resolve和release方法.我不喜欢的一点是,我们必须明确地为瞬态生活方式对象调用release.如果没有经验的开发人员不这样做,你最终会发生内存泄漏.
我还发现:https://www.nuget.org/packages/CommonServiceLocator.WindsorAdapter,它有很多下载..(与选项1相同,但更通用,包装容器,以便您可以轻松交换DI框架)我查看了代码,发现适配器只有解析对象的方法.所以我有点想知道为什么没有发布方法..这是否意味着这个包有瞬态生活方式对象的内存泄漏问题?
希望有人能就这些问题给我一些建议!
asp.net-mvc dependency-injection castle-windsor castle service-locator