Basic Ninject Information required please
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开发者_如何学Go 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.
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.
精彩评论