仓储层当前有接口 IRepository<T> 抽象类 BaseRepository<T> 业务逻辑层有抽象类 BaseBusiness<M, E> 接口 IBusiness<M, E> 请用C# 给出一个案例,支持不同表对应不同的业务逻辑层,然后不同仓储实例。

以下是一个简单的C#示例,展示了如何实现不同表对应不同的业务逻辑层和不同的仓储实例:

// 仓储层
public interface IRepository<T>
{
    void Add(T entity);
    void Update(T entity);
    void Delete(T entity);
    T GetById(int id);
    // 其他仓储操作方法...
}

public abstract class BaseRepository<T> : IRepository<T>
{
    // 实现 IRepository 接口的通用方法
}

// 业务逻辑层
public interface IBusiness<M, E>
{
    void Process(M model);
    // 其他业务逻辑方法...
}

public abstract class BaseBusiness<M, E> : IBusiness<M, E>
{
    // 实现 IBusiness 接口的通用方法
}

// 具体业务逻辑类
public class ProductBusiness : BaseBusiness<ProductModel, ProductEntity>
{
    private readonly IRepository<ProductEntity> _productRepository;

    public ProductBusiness(IRepository<ProductEntity> productRepository)
    {
        _productRepository = productRepository;
    }

    public override void Process(ProductModel model)
    {
        // 实现特定的业务逻辑
        // 可以使用 _productRepository 执行数据操作
    }
}

public class CustomerBusiness : BaseBusiness<CustomerModel, CustomerEntity>
{
    private readonly IRepository<CustomerEntity> _customerRepository;

    public CustomerBusiness(IRepository<CustomerEntity> customerRepository)
    {
        _customerRepository = customerRepository;
    }

    public override void Process(CustomerModel model)
    {
        // 实现特定的业务逻辑
        // 可以使用 _customerRepository 执行数据操作
    }
}

// 模型和实体类
public class ProductModel
{
    // 产品模型的属性...
}

public class ProductEntity
{
    // 产品实体的属性...
}

public class CustomerModel
{
    // 客户模型的属性...
}

public class CustomerEntity
{
    // 客户实体的属性...
}

// 使用示例
class Program
{
    static void Main(string[] args)
    {
        // 注入不同的仓储实例到不同的业务逻辑类中
        IRepository<ProductEntity> productRepository = new ProductRepository();
        IBusiness<ProductModel, ProductEntity> productBusiness = new ProductBusiness(productRepository);

        IRepository<CustomerEntity> customerRepository = new CustomerRepository();
        IBusiness<CustomerModel, CustomerEntity> customerBusiness = new CustomerBusiness(customerRepository);

        // 使用业务逻辑类进行操作
        var productModel = new ProductModel();
        productBusiness.Process(productModel);

        var customerModel = new CustomerModel();
        customerBusiness.Process(customerModel);
    }
}

 

posted @ 2024-05-05 09:47  摩诘  阅读(8)  评论(0编辑  收藏  举报