小编use*_*218的帖子

C#服务层设计模式

我们正在研究创建一个新项目,并希望探索使用Repository和Service层模式,目的是创建松散耦合的代码,使用模拟存储库完全可测试.

请参阅下面的基本架构构思.我们将使用接口来描述存储库,并将这些接口注入服务层以删除任何依赖项.然后使用autofac我们将在运行时连接服务.

public interface IOrderRepository
{
    IQueryable<Order> GetAll();
}

public class OrderRepository : IOrderRepository
{
    public IQueryable<Order> GetAll()
    {
        return new List<Order>().AsQueryable();
    }
}

public class OrderService
{
    private readonly IOrderRepository _orderRepository;

    public OrderService(IOrderRepository orderRepository)
    {
        _orderRepository = orderRepository;
    }

    public IQueryable<Order> GetAll()
    {
        return _orderRepository.GetAll();
    }
}

public class EmailService
{
    public void SendEmails()
    {
        // How do I call the GetAll method from the order serivce
        // I need to inject into the orderService the repository to use …
Run Code Online (Sandbox Code Playgroud)

c# tdd service design-patterns repository

11
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

design-patterns ×1

repository ×1

service ×1

tdd ×1