StructureMap3 How to configure constructor string injection for all types?

荒凉一梦 提交于 2019-12-12 03:19:44

问题


I have registered my types using

Scan(
    scan => {
        scan.TheCallingAssembly();
        scan.WithDefaultConventions();
        scan.With(new ControllerConvention());
    });

But how do I specify for constructor injection with out having to specify the concrete type like this?

string connStr = "...";
For<IRepository().Use<MyRepository>().Ctor<string>("connectionString").Is(connStr);

回答1:


You can create dedicated convention for registration of repositories.

public class RepositoryConvention : IRegistrationConvention
{
    private const string ConnectionString = "your connection string";

    public void Process(Type type, Registry registry)
    {
        if (type.IsConcrete() && type.GetInterfaces().Contains(typeof(IRepository)))
        {
            registry.For(typeof(IRepository))
                .Use(type)
                .CtorDependency<string>("connectionString")
                .Is(ConnectionString);
        }
    }
}

or create dedicated type to provide with connection string. I bet you are getting it from web/app.config so adding abstraction for accessing it would be helpful anyway.

public interface IConfigurationSettingsReader
{
    string ReadConnectionString(string name);
    T ReadSetting<T>(string settingName);
}

Then you just add it as a dependency for your MyRepository and you don't need to add it explicitly in registration or use custom convention.

public class MyRepository : IRepository
{
    private readonly string connectionString;

    public MyRepository(IConfigurationSettingsReader settingsReader)
    {
        this.connectionString = settingsReader.ReadConnectionString("ConnStrName");
    }
}

You can consider creating an abstract base repository class to be inherited by each repository to get rid of setup bolerplate.

Hope this helps!



来源:https://stackoverflow.com/questions/28077293/structuremap3-how-to-configure-constructor-string-injection-for-all-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!