开发者

Injecting a instance of a class to a constructor

开发者 https://www.devze.com 2023-04-05 20:19 出处:网络
Is this the bes开发者_Python百科t practice for injecting a class dependency into a repository?Bear in mind that other repositories will need this PetaPoco.Database instance as I want each repository t

Is this the bes开发者_Python百科t practice for injecting a class dependency into a repository? Bear in mind that other repositories will need this PetaPoco.Database instance as I want each repository to use a shared database connection object.

public class ConfigRepository : IConfigRepository
{
        private Database DB;

        public ConfigRepository(PetaPoco.Database db)
        {
            DB = db;
        }
}

//Here is how structuremap is configured

    ObjectFactory.Initialize(x =>
    {
        x.Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.WithDefaultConventions();
            scan.AddAllTypesOf<IController>();


        });

        x.Register<PetaPoco.Database>(new PetaPoco.Database("DBConnection"));

        x.For<IConfigRepository>().Use<ConfigRepository>();

    });
    return ObjectFactory.Container;


I'm told this is what you need:

x.For<PetaPoco.Database>().Singleton().Use(()=>new PetaPoco.Database("connectionString"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号