标签: constructor-injection

具有多个存储库的依赖注入

我有一个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

6
推荐指数
2
解决办法
5580
查看次数

具有过滤的dbContext的多租户Web应用程序

我是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# asp.net-mvc multi-tenant constructor-injection dbcontext

6
推荐指数
1
解决办法
3673
查看次数

Windows 窗体中可以避免构造函数过度注入吗?

我有一个 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)

这既明智又可能吗?我对其他方法持开放态度,不需要假设任何依赖注入容器(我宁愿不使用任何容器)。

.net c# dependency-injection winforms constructor-injection

6
推荐指数
1
解决办法
239
查看次数

构造函数注入 - 在哪里调用?

我对构造函数注入模式和规则有点困惑不要调用容器; 它会打电话给你.

有人可以解释我(也许还有其他人)真正的应用程序如何使用构造函数注入获得所有DI优势?我给出了一些简单的,我认为常见的例子:

DomainObject
RepositoryObject
DaoObject
Run Code Online (Sandbox Code Playgroud)

关系很明显(我认为) - RepositoryObject需要DaoObject,DomainObject需要Repository.

使用构造函数注入我假设我可以忘记(在大多数情况下)关于NEW关键字,但是何时,何地以及如何创建新对象(主要是域)?我必须为所有班级写工厂吗?我应该在那家工厂参考DI Container吗?

最好的将是当有人向我展示一些真实的应用程序示例(请不要Asp.Net MVC :))或草绘一些项目结构.

.net dependency-injection constructor-injection

5
推荐指数
1
解决办法
466
查看次数

StructureMap:选择嵌套依赖项的具体类型

计算器:

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)

c# structuremap dependency-injection constructor-injection

5
推荐指数
1
解决办法
1729
查看次数

CDI 构造函数注入不适用于瞬态不可序列化依赖项

我非常喜欢 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 …

java serializable transient constructor-injection cdi

5
推荐指数
1
解决办法
2411
查看次数

Spring重载构造函数注入

这是代码:

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 将调用哪个构造函数?

java spring constructor-injection

5
推荐指数
2
解决办法
3975
查看次数

Spring 3由构造函数自动装配 - 为什么这段代码有效?

春季版是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 constructor autowired constructor-injection spring-3

5
推荐指数
1
解决办法
395
查看次数

Spring 配置类中是否可以注入构造函数?

如果我有一个 Spring 配置类(即用 @Configuration 注释的类),我可以使用构造函数注入吗?

如果我添加一个,我会得到一个没有默认构造函数的消息,如果我添加一个默认构造函数,它会使用它而不是重载的构造函数,这并没有真正的帮助。

configuration spring constructor constructor-injection

5
推荐指数
1
解决办法
4677
查看次数

将Spring字段注入转换为构造函数注入(IntelliJ IDEA)?

我坚信将我的Spring Java字段注入[@Autowired]转换为构造函数注入(除其他原因外,以方便模拟单元测试)...

有没有可以用来自动完成Spring字段到构造函数注入转换的实用程序?

例如,IntelliJ IDEA有很多东西的生成快捷方式(即:为字段生成设置器和获取器);我希望有类似的东西...当手动进行繁琐的转换时,这特别有用,因为要转换的类已经有许多字段注入的字段。

java spring dependency-injection intellij-idea constructor-injection

5
推荐指数
2
解决办法
2571
查看次数