我有一个wcf服务,在客户端我有:
var service = new ServiceReference1.CACSServiceClient()
Run Code Online (Sandbox Code Playgroud)
实际的服务代码是:
public CACSService() : this(new UserRepository(), new BusinessRepository()) { }
public CACSService(IUserRepository Repository, IBusinessRepository businessRepository)
{
_IRepository = Repository;
_IBusinessRepository = businessRepository;
}
Run Code Online (Sandbox Code Playgroud)
所以,所有这一切都很好,但我不喜欢我如何同时新建所有的存储库,因为客户端代码可能不需要新的UserRepository,只有兴趣新的BusinessRepository.那么,有没有办法将一些东西传递给这段代码:
var service = new ServiceReference1.CACSServiceClient()
根据调用服务的代码或者在为我的实体框架设计存储库时需要进行的任何其他建议,告诉它哪个存储库是新的.Thankx
c# wcf dependency-injection repository constructor-injection
我是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) 我有一个 Windows 窗体。它包含许多控制器:网格、按钮,甚至TreeView. 其中大多数都定义了事件。例如,有多个独立事件以独立方式查询或命令我的 SQL 服务器。对于几乎所有情况,接口隔离原则迫使我不要合并这些接口。
因为我正在进行依赖项注入,所以我的 Windows 窗体的构造函数被大量过度注入。我在表单的构造函数中有四个参数,仅用于单个网格。示例包括:网格的初始填充、查询其中一个单元格中组合框的值列表、当另一种类型更改时重新填充某些类型的单元格等。我认为用不了多久我的构造函数就会有一些东西荒谬的是,像 20 个参数,所有查询服务器的接口。
这在 Windows 窗体中可以避免吗?据猜测,我认为如果我能够以某种方式构建每个组件,然后将它们输入到我的构造函数中,而不是让我的表单了解每个组件的依赖关系,我会更好。也就是说,我想我宁愿替换
MyForm(IQueryGridTimes TimeQueryRepo, ICommandGridTimeCells TimeCommandRepo, IQueryTheWholeGrid GridInitialPopulator, ..., IQueryForTheTreeView TreeViewPopulator)
Run Code Online (Sandbox Code Playgroud)
和
var grid = new WhateverGrid(IQueryGridTimes TimeQueryRepo, ICommandGridTimeCells TimeCommandRepo, IQueryTheWholeGrid GridInitialPopulator)
var tress = new WhateverTreeview(QueryForTheTreeView TreeViewPopulator)
MyForm(grid, ..., trees,)
Run Code Online (Sandbox Code Playgroud)
这既明智又可能吗?我对其他方法持开放态度,不需要假设任何依赖注入容器(我宁愿不使用任何容器)。
我对构造函数注入模式和规则有点困惑不要调用容器; 它会打电话给你.
有人可以解释我(也许还有其他人)真正的应用程序如何使用构造函数注入获得所有DI优势?我给出了一些简单的,我认为常见的例子:
DomainObject
RepositoryObject
DaoObject
Run Code Online (Sandbox Code Playgroud)
关系很明显(我认为) - RepositoryObject需要DaoObject,DomainObject需要Repository.
使用构造函数注入我假设我可以忘记(在大多数情况下)关于NEW关键字,但是何时,何地以及如何创建新对象(主要是域)?我必须为所有班级写工厂吗?我应该在那家工厂参考DI Container吗?
最好的将是当有人向我展示一些真实的应用程序示例(请不要Asp.Net MVC :))或草绘一些项目结构.
计算器:
public interface ICalculator
{
int Calculate(int a, int b);
}
public class Calculator : ICalculator
{
private readonly ICalculatorStrategy _calculatorStrategy;
public Calculator(ICalculatorStrategy calculatorStrategy)
{
_calculatorStrategy = calculatorStrategy;
}
public int Calculate(int a, int b)
{
return _calculatorStrategy.Calculate(a, b);
}
}
Run Code Online (Sandbox Code Playgroud)
计算器stragies:
public interface ICalculatorStrategy
{
int Calculate(int a, int b);
}
public class AdditionCalculator : ICalculatorStrategy
{
public int Calculate(int a, int b)
{
return a + b;
}
}
public class MultiplyCalculator : ICalculatorStrategy
{
public int Calculate(int …Run Code Online (Sandbox Code Playgroud) 我非常喜欢 CDI 的构造函数注入,但现在我发现了一个用例,其中构造函数注入显然无法按预期工作。
在我的示例中,我有两个类。类“BeanA”没有明确定义的范围,也没有实现可序列化。类“BeanB”使用@SessionScoped 进行注释并且确实实现了可序列化。
public class BeanA{
}
@SessionScoped
public class BeanB implements Serializable{
@Inject
private BeanA beanA;
}
Run Code Online (Sandbox Code Playgroud)
When I try to inject an instance of BeanA into BeanB of cource I get an UnserializableDependencyException from Weld because BeanA isn't serializable. This is the expected behaviour.
When I mark the field "beanA" with "transient" the injection works without problems:
@Inject
private transient BeanA beanA;
Run Code Online (Sandbox Code Playgroud)
Now Weld doesn't throw any exceptions.
This is perfectly fine for me but my …
这是代码:
public class Triangle {
private String color;
private int height;
public Triangle(String color,int height){
this.color = color;
this.height = height;
}
public Triangle(int height ,String color){
this.color = color;
this.height = height;
}
public void draw() {
System.out.println("Triangle is drawn , +
"color:"+color+" ,height:"+height);
}
}
Run Code Online (Sandbox Code Playgroud)
Spring配置文件是:
<bean id="triangle" class="org.tester.Triangle">
<constructor-arg value="20" />
<constructor-arg value="10" />
</bean>
Run Code Online (Sandbox Code Playgroud)
是否有任何特定规则来确定 Spring 将调用哪个构造函数?
春季版是3.2
在Spring in Action,第三版,3.1.1.陈述了四种自动装配,即
构造函数自动装配与byType共享相同的限制.当Spring找到多个与构造函数的参数匹配的bean时,它不会尝试猜测哪个bean要自动装配.此外,如果一个类有多个构造函数,其中任何一个都可以通过自动装配来满足,Spring将不会尝试猜测要使用哪个构造函数.
这最后一句让我感到困惑.我有以下bean定义:
<bean id="independentBean" class="autowiring.IndependentBean" scope="prototype" />
<bean id="weirdBean" class="autowiring.WeirdBean" scope="prototype" />
<bean id="dependentBeanAutowiredByName" class="autowiring.DependentBean" autowire="byName" />
<bean id="dependentBeanAutowiredByType" class="autowiring.DependentBean" autowire="byType" />
<bean id="dependentBeanAutowiredByConstructor" class="autowiring.DependentBean" autowire="constructor" />
Run Code Online (Sandbox Code Playgroud)
IndependentBean和WeirdBean是空类.
DepentendBean定义如下:
package autowiring;
public class DependentBean {
private IndependentBean independentBean;
private IndependentBean anotherBean;
private WeirdBean weirdBean;
public DependentBean() {
}
public DependentBean(IndependentBean independentBean) {
super();
this.independentBean = independentBean;
}
public DependentBean(IndependentBean independentBean, IndependentBean anotherBean) {
super();
this.independentBean = independentBean;
this.anotherBean = anotherBean;
}
public DependentBean(IndependentBean independentBean, …Run Code Online (Sandbox Code Playgroud) 如果我有一个 Spring 配置类(即用 @Configuration 注释的类),我可以使用构造函数注入吗?
如果我添加一个,我会得到一个没有默认构造函数的消息,如果我添加一个默认构造函数,它会使用它而不是重载的构造函数,这并没有真正的帮助。
我坚信将我的Spring Java字段注入[@Autowired]转换为构造函数注入(除其他原因外,以方便模拟单元测试)...
有没有可以用来自动完成Spring字段到构造函数注入转换的实用程序?
例如,IntelliJ IDEA有很多东西的生成快捷方式(即:为字段生成设置器和获取器);我希望有类似的东西...当手动进行繁琐的转换时,这特别有用,因为要转换的类已经有许多字段注入的字段。
java spring dependency-injection intellij-idea constructor-injection
c# ×4
spring ×4
java ×3
.net ×2
constructor ×2
asp.net-mvc ×1
autowired ×1
cdi ×1
dbcontext ×1
multi-tenant ×1
repository ×1
serializable ×1
spring-3 ×1
structuremap ×1
transient ×1
wcf ×1
winforms ×1