Pregunta

I'm specifically using C# with Ninject but the problem stretches beyond just Ninject. My problem is that I have a few classes all with different constructor arguments plus the injected ones. I know I can use kernel.Get<MyObject>(constructor args here) to instantiate the objects. This doesn't feel right to me as I'd have the kernel all over the place. I'll do my best to list examples below.

What I have now:

public interface IStore<T>
{
    void CommitToDatabase(T item);
}

public abstract class Thing
{
    private IStore<Thing> _store;

    protected Thing(object key, IStore<Thing> store)
    {
        Key = key;
        _store = store;
    }

    public object Key { get; private set; }

    public virtual void Update()
    {
        _store.CommitToDatabase(this);
    }
}

public class Person :Thing
{
    public Person(object key, string name, int age, IStore<Thing> store)
        : base(key, store)
    {
        Name = name;
        Age = age;
    }

    public string Name { get; private set; }
    public int Age { get; private set; }
}

public class Car :Thing
{
    public Car(object key, int year, string make, string model, IStore<Thing> store)
        : base(key, store)
    {
        Year = year;
        Make = make;
        Model = model;
    }

    public int Year { get; private set; }    
    public string Make { get; private set; }
    public string Model { get; private set; }
}

I know in Ninject I can do the following:

kernel.Get<Car>(new ConstructorArgument("key", 1), new ConstructorArgument("year", 2010), new ConstructorArgument("make", "Astin Martin"), new ConstructorArgument("model", "Vanquish"));

but that doesn't feel right to me. What I was thinking of doing was to change it to have an Initialize method, but I'm not sure if this is best-practice or if there's a better way.

Possible new stuff:

public interface IStore<T>
{
    void CommitToDatabase(T item);
}

public abstract class Thing
{
    private IStore<Thing> _store;

    protected bool _isInitialised;

    protected Thing(IStore<Thing> store)
    {
        Key = null;
        _store = store;
        _isInitialised = false;
    }

    public object Key { get; private set; }

    public virtual void Initialize(object key)
    {
        if (!_isInitialised) {
            Key = key;
            _isInitialised = true;
        }
    }

    public virtual void Update()
    {
        _store.CommitToDatabase(this);
    }

    protected bool IsInitialised()
    {
        return _isInitialised;
    }
}

public class Person :Thing
{
    public Person(IStore<Thing> store)
        : base(store)
    {
        Name = string.Empty;
        Age = int.MinValue;
    }

    public string Name { get; private set; }
    public int Age { get; private set; }

    public void Initialize(object key, string name, int age)
    {
        if (!base.IsInitialised()) {
            Name = name;
            Age = age;
        }

        base.Initialize(key);
    }
}

public class Car :Thing
{
    public Car(IStore<Thing> store)
        : base(store)
    {
        Year = 0;
        Make = "Ford";
        Model = "Model T";
    }

    public int Year { get; private set; }
    public string Make { get; private set; }
    public string Model { get; private set; }

    public void Initialize(object key, int year, string make, string model)
    {
        if (!base.IsInitialised()) {
            Year = year;
            Make = make;
            Model = model;
        }

        base.Initialize(key);
    }
}

Question: Is the 'Possible new stuff' a common practice, bad idea, good idea with poor implementation, or is there a better way to do it altogether?

¿Fue útil?

Solución

You shouldn't inject IStore into your DTO's. They should be plain objects. Instead inject IStore<IThing> into the classes that currently call Update and call CommitToDatabase from there.

e.g.

public class PersonService
{
    private readonly IStore<Person> store;
    public PersonService(IStore<Person> store)
    {
       this.store = store;
    }

    public void CreatePerson(string name, int age)
    {
       var person = new Person(name, age);
       this.store.CommitToDatabase(person);
    }
}

Also DTOs like Person shouldn't be created using an IoC container. Get them from your persistence layer, create them using AutoMapper or create them using new. But do not use an IoC container for them. They shouldn't have any dependencies.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top