请提供基本的 Ninject 信息

发布于 2024-09-19 09:22:04 字数 1255 浏览 3 评论 0原文

我一直在慢慢地自学接口驱动编程的基础知识,并且正在努力理解一些有关 Ninject 控制反转的原则。

可以说我有一个具体的模型类,如下所示...

 public sealed class Host : EntityAuditable<Host, Guid>

我有一个基类,它定义了这样的模型方法...

public abstract class EntityAuditable<T, TKey> : 
    IEntity, 
    IDisposable, 
    IComparable<EntityAuditable<T, TKey>> 
    where T : EntityAuditable<T, TKey>, new()

IEntity 是一个非常简单的接口,它定义了对象属性“Id”。

现在,我的 Host 类的构造函数如下...

public Host(IService<Host> service)
{
    this.service = service;
    this.id = Guid.NewGuid();
}

其中 IService 实现提供了将对象持久保存到特定于服务的存储库的方法。

我想做的是在我的基类中提供一些静态方法,以便我可以编写类似的代码

Host.LoadAll(); 

并知道我的绑定将被正确注入。我在基类中的实现与此类似...

   public static IList<T> LoadAll()
    {
        T instance = new T();
        List<T> instances = new List<T>(instance.DataSelectAll());
        // Clean up the instance.
        instance.Dispose();
        return instances;
    }

这是可能的还是我在做一些根本错误的事情?

Ninject 的工作方式是否知道当我调用 new T() 时它应该调用我的参数化构造函数?

我在文档中找到的示例(遗憾的是已经过时了)使用内核来创建对象,但我不想让内核创建代码散落在我的模型中。

我将在 Global.ascx 文件中进行所有绑定。

I've been slowly teaching myself the fundamentals of interface driven programming and I'm struggling to get my head round a few principles regarding inversion of control specifically with Ninject.

Lets say I have a concrete model class as follows...

 public sealed class Host : EntityAuditable<Host, Guid>

I have a base class which defines methods for the model like this....

public abstract class EntityAuditable<T, TKey> : 
    IEntity, 
    IDisposable, 
    IComparable<EntityAuditable<T, TKey>> 
    where T : EntityAuditable<T, TKey>, new()

IEntity is a very simple interface that defines an object property 'Id'.

Now the constructor for my Host class would be as follows....

public Host(IService<Host> service)
{
    this.service = service;
    this.id = Guid.NewGuid();
}

Where the IService implementation provides methods to persist objects to the repository specific to the service.

What I would like to do is provide some static methods in my base class so that I can write code like

Host.LoadAll(); 

and know that my bindings will be injected correctly. My implementation in the base class would be something similar to this...

   public static IList<T> LoadAll()
    {
        T instance = new T();
        List<T> instances = new List<T>(instance.DataSelectAll());
        // Clean up the instance.
        instance.Dispose();
        return instances;
    }

Is this possible or am I doing something fundamentally wrong?

Does Ninject work in a way that it knows it should be calling my parameterized constructor when I call new T() ?

The examples I have found in the documentation (Which is sadly out of date) use the kernel to create the object but I don't want to have kernel creating code littered about in my models.

I'd be doing all my binding in my Global.ascx file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神回复 2024-09-26 09:22:04

在回答我自己的问题时,事实证明我的方法是错误的。

去掉静态方法并使用我的 mvc 控制器来处理业务逻辑可以实现更好的分离以及更干净和更可维护的代码。那时用 ninject 绑定所有东西要容易得多。

In answer to my own question it turns out that my approach was incorrect.

Stripping out the static methods and using my mvc controllers for the business logic led to far better separation and much cleaner and more maintainable code. Binding everything with ninject was far easier then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文