4

我专门将 C# 与 Ninject 一起使用,但问题不仅限于 Ninject。我的问题是我有几个类都具有不同的构造函数参数以及注入的参数。我知道我可以kernel.Get<MyObject>(constructor args here)用来实例化对象。这对我来说感觉不对,因为我到处都有内核。我会尽力在下面列出示例。

我现在拥有的:

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; }
}

我知道在 Ninject 中我可以执行以下操作:

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

但这对我来说感觉不对。我想做的是将其更改为具有 Initialize 方法,但我不确定这是否是最佳实践或是否有更好的方法。

可能的新东西:

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);
    }
}

问题: “可能的新东西”是一种常见的做法、坏主意、好主意但执行不力,还是有更好的方法来做到这一点?

4

1 回答 1

6

您不应该将 IStore 注入您的 DTO。它们应该是普通的对象。而是注入IStore<IThing>当前调用的类UpdateCommitToDatabase从那里调用。

例如

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);
    }
}

此外,不应使用 IoC 容器创建像 Person 这样的 DTO。从持久层获取它们,使用 AutoMapper 创建它们或使用new. 但不要为它们使用 IoC 容器。他们不应该有任何依赖。

于 2011-11-16T17:13:52.380 回答