How do I correctly use Unity to pass a ConnectionString to my repository classes?

后端 未结 4 1110
抹茶落季
抹茶落季 2020-12-08 07:49

I\'ve literally just started using the Unity Application Blocks Dependency Injection library from Microsoft, and I\'ve come unstuck.

This is my IoC class that\'ll ha

4条回答
  •  爱一瞬间的悲伤
    2020-12-08 08:35

    I ll add another way :)

    You can use Injection parameters that get passed into the constructor when you register a type. This way you can still use constructor injection.

    class Repository : IRepository {
      readonly string m_ConnectionString;
      public Repository(string connectionString) {
        ConnectionString = connectionString;
      }
    }
    
    //When registering
    container.RegisterType(new InjectionConstructor("connectionstring"));
    

提交回复
热议问题