在听完清洁代码会谈之后,我开始明白我们应该使用工厂来组合对象.因此,例如,如果a House有a Door和a Door有a DoorKnob,则在HouseFactory我们创建new DoorKnob并将其传递给构造函数Door,然后将该新Door对象传递给构造函数House.
但是使用的类House (比如类名ABC)呢?这将取决于HouseFactory,对吗?那么我们传递HouseFactory构造函数ABC吗?我们不是必须以这种方式在构造函数中传递大量工厂吗?
我创建了2个简单的类.一个类的构造函数注释为@Autowired.它接受另一个类的对象.但是这段代码失败了.
类: - 1)SimpleBean.java
@Configuration
public class SimpleBean {
InnerBean prop1;
public InnerBean getProp1() {
return prop1;
}
public void setProp1( InnerBean prop1) {
System.out.println("inside setProp1 input inner's property is "
+ prop1.getSimpleProp1());
this.prop1 = prop1;
}
@Autowired(required=true)
public SimpleBean(InnerBean prop1) {
super();
System.out.println("inside SimpleBean constructor inner's property is "
+ prop1.getSimpleProp1());
this.prop1 = prop1;
}
}
Run Code Online (Sandbox Code Playgroud)
2)Inner.java
@Configuration
public class InnerBean {
String simpleProp1;
public String getSimpleProp1() {
return simpleProp1;
}
public void setSimpleProp1(String simpleProp1) {
this.simpleProp1 = simpleProp1; …Run Code Online (Sandbox Code Playgroud) spring annotations default-constructor constructor-injection
假设我的Foo班级有以下内容:
readonly IService service;
public Foo(IService service)
{
if (service == null)
throw new ArgumentNullException("service");
this.service = service;
}
public void Start()
{
service.DoStuff();
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,我有一个单元测试的构造函数,我传入null来验证是否ArgumentNullException抛出了.我是否需要为我的构造函数进行第二次单元测试,在这里我传入一个有效的IService并验证是否已this.service设置(需要公共访问器)?
或者我应该依靠单元测试Start来测试此代码路径的方法?
c# constructor unit-testing dependency-injection constructor-injection
我在这个板上看了类似的问题,但没有一个回答我的问题.这听起来很奇怪,但是可以模拟出你正在嘲笑的对象的构造函数调用.
例:
class RealGuy {
....
public void someMethod(Customer customer) {
Customer customer = new Customer(145);
}
}
class MyUnitTest() {
public Customer customerMock = createMock(Customer.class)
public void test1() {
//i can inject the mock object, but it's still calling the constuctor
realGuyobj.someMethod(customerMock);
//the constructor call for constructor makes database connections, and such.
}
}
Run Code Online (Sandbox Code Playgroud)
我怎么能期待一个构造函数调用?我可以更改Customer构造函数调用以使用newInstance,但我不确定这是否有帮助.我无法控制new Customer(145)构造函数的主体做什么.
这可能吗?
我正在寻找有关为IoC设计对象的最佳方法的建议
假设我有一个对象(Service)依赖于向Ioc注册的DataContext.
但它还需要一个名称属性,我可以像这样设计对象:
class Service
{
public Service(IDataContext dataContext,
string name)
{
this._dataContext = dataContext;
this._name = name
}
public string Name
{
get
{
return _name;
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是使用Ioc容器变得非常复杂,因为字符串对象如名称不容易注册,并且Ioc容器的使用变得复杂:因此解决方案变得混乱:
var service = Ioc.Resolve<Service>( ?? )
Run Code Online (Sandbox Code Playgroud)
另一种方法是将其设计如下:
class Service
{
public Service(IDataContext dataContext)
{
this._dataContext = dataContext;
}
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
分辨率现在更容易:
var service = Ioc.Resolve<Service>();
service.Name = "Some name";
Run Code Online (Sandbox Code Playgroud)
唯一的缺点是不再需要指定名称.我想听听DI或IoC专家如何设计这个并且仍然对混凝土Ioc容器技术保持相当不可知.
我知道很多取决于你想如何使用它,如果name真的是可选的,选项2将是完美的.但是在需要名称的情况下,你可以在代码的另一个点添加验证步骤,而是去设计使Ioc更简单.
思考?
c# dependency-injection inversion-of-control property-injection constructor-injection
我有一个基类Class Base具有dependecy Dep和default和Injection Constructor-
Class Base : IBase
{
public IDep Dep { get; set; }
public Base()
{
Console.WriteLine("Default Constructor Base ");
}
[InjectionConstructor]
public Base(IDep dep)
{
Console.WriteLine("Injection Constructor Base ");
Dep = dep;
}
}
Run Code Online (Sandbox Code Playgroud)
我认为在解析派生类时,应该自动注入Dependency dep(通过Constructor Injection).
但是,当我从它派生一个类并解析该类时,这似乎不起作用,而是调用Base的默认构造函数.
当我从Derived Class中显式调用构造函数时,我只能使它工作.
class Derived : Base
{
public Derived ()
{
Console.WriteLine("Default Constructor Derived ");
}
public Derived (IDep dep) : base(dep1)
{
Console.WriteLine("Injection Constructor Derived ");
}
}
Run Code Online (Sandbox Code Playgroud)
unity是否提供了任何直接的方法来隐式调用基类的注入构造函数(而不是通过显式的Construtor调用)?如果没有,是否有任何理由为什么统一容器本身不做?
c# dependency-injection unity-container constructor-injection
创建视图模型时,您可以将选项(例如,在下拉列表中使用)填充到视图模型的setter属性中.问题是,当该视图模型稍后作为参数(通过框架!)传递到操作方法时,这些属性值尚未自动重新填充,因此如果由于验证错误需要重新显示表单,则需要再次重新填充这些选项.
我在这个问题中特别要求的一个可能的解决方案是如何使MVC框架使用构造函数注入实例化视图模型,这将为视图模型构造函数提供某种数据访问对象的实现(例如存储库) )当视图请求选项时,它们可用于检索选项(例如在辅助方法"DropDownListFor"中)?
我认为该解决方案可能与IModelBinderProvider或IModelBinder的实现有关,但是在网络上的示例代码片段中对这些事情进行了实验后,我仍在寻找一个完全可行的示例,可下载的可执行代码没有任何遗漏将所有事物放在一起的方法.
如果您正在寻找关于如何填充一个选择列表,例如用"Dependecy查找"而不是"Dependecy注入"你可能想看看下面的讨论中,一些另类的讨论:最好的方式来填充的SelectList的视图模型上的GET/POST 最佳在GET/POST上为ViewModel填充SelectList的方法
几天前,我在该帖子中写了关于"Dependecy Injection"的以下后续问题,我正在寻找这个帖子:https://stackoverflow.com/a/8674525/310457 (它提供了一个代码示例问题我正在寻找解决方案)
但是,我没有希望有人会找到那个标题较少的旧帖子,而是用一个更具体的主题来创建这个新问题,关于我在寻找什么.对于想要跟进我正在寻找的这个特定解决方案的任何人,我还将提供从该线程到这个新问题的链接.
dependency-injection viewmodel constructor-injection asp.net-mvc-3
我有一个带有 lombok @RequiredArgsConstructor 的课程:
@RequiredArgsConstructor
@Service
public class Test{
private final String str;
private String str5;
// more code
}
Run Code Online (Sandbox Code Playgroud)
在非 Spring Boot 中,我们在 xml 中提供如下:
<bean id="Test" class="com.abc.Test">
<constructor-arg index="0" value="${xyz}"/>
</bean>
Run Code Online (Sandbox Code Playgroud)
如何从 Spring Boot 实现相同的目标可能是通过 application.properties 但如何注入
我是ASP.Net MVC和多租户Web应用程序的新手.我做了很多阅读,但作为初学者,我只是按照我的理解.所以我设法构建了一个示例场景Web应用程序,需要解决它的结尾部分.希望这种情况对其他一些初学者也有用,但欢迎任何其他方法.提前致谢
1)SQLServer 2008中的数据库.

2)数据层:名为MyApplication.Data的C#类库项目
public class AppUser
{
[Key]
public virtual int AppUserID { get; set; }
[Required]
public virtual int TenantID { get; set; }
[Required]
public virtual int EmployeeID { get; set; }
[Required]
public virtual string Login { get; set; }
[Required]
public virtual string Password { get; set; }
}
public class Employee
{
[Key]
public virtual int EmployeeID { get; set; }
[Required]
public virtual int TenantID { get; set; }
[Required]
public virtual …Run Code Online (Sandbox Code Playgroud) 控制反转是一种价值证明技术,用于模块化系统并使组件彼此分离.
低耦合始终是一个优势:它简化了组件的自动测试,使代码更符合单一责任原则.
在申报依赖于另一个类(方式服务定位,物业注入调用的公共方法/设置一个公共属性...),构造注入似乎是最好的办法.
虽然它可能是最难实现的(至少来自列出的三个),但它具有显着的优势:
C++提供的许多选择通过构造函数执行注入的优缺点是什么?
c++ dependency-injection inversion-of-control constructor-injection
c# ×4
java ×2
annotations ×1
asp.net-mvc ×1
c++ ×1
constructor ×1
dbcontext ×1
easymock ×1
lombok ×1
mocking ×1
multi-tenant ×1
spring ×1
spring-boot ×1
unit-testing ×1
viewmodel ×1