简体   繁体   English

Autofac 通用存储库服务多个 DBSet

[英]Autofac Generic Repository Service Multiple DBSet

I'm new to Autofac and repository.我是 Autofac 和存储库的新手。 I downloaded an MVC sample project and I'm working on it.我下载了一个 MVC 示例项目,我正在研究它。 I have a database and several interrelated tables.我有一个数据库和几个相互关联的表。 There is a Customer repo service in the MVC Controller . MVC Controller中有一个Customer回购服务。

I have several property classes in the customer class.我在客户 class 中有几个属性类。

I want to add a new record.我想添加一条新记录。 But I also need to add records to the nested classes.但我还需要向嵌套类添加记录。 One Customer service is added to the parameter of the constructive method.在构造方法的参数中添加了一项客户服务。

How can I access other tables in controller.如何访问 controller 中的其他表。

What is the best way for that.什么是最好的方法。

 private readonly IService<SubCustomer> _serviceSubCust;
    private readonly IService<Customer> _serviceCust;
    private readonly IService<Document> _serviceDoc;
    private readonly int _pageSize;

    public DocumentTransactionController(
        IService<SubCustomer> serviceSubCust,
        IService<Customer> serviceCust,
        IService<Document> serviceDoc)
    {
        _serviceCust = serviceCust;
        _serviceSubCust = serviceSubCust;
        _serviceDoc = serviceDoc;
        _pageSize = 10;
    }

Assuming all the services are registered inside your startup.cs, start with data access and simple data writing.假设所有服务都在你的 startup.cs 中注册,从数据访问和简单的数据写入开始。 You didn't post your service methods so I'm going to take a black box stab at what is in there.您没有发布您的服务方法,所以我要对其中的内容进行黑匣子测试。

Perhaps you have a method inside your _serviceCust that exposes your Customer list, or it has various methods such as GetCustomerList() , or CreateCustomer(Customer cust) .也许您的_serviceCust中有一个公开您的客户列表的方法,或者它有各种方法,例如GetCustomerList()CreateCustomer(Customer cust) The magic of services is that you can store reusable logic and access it from your controller.服务的神奇之处在于您可以存储可重用逻辑并从 controller 访问它。 In this case, the best use of a service is a method that can update nested structures for you in the database.在这种情况下,服务的最佳用途是可以为您更新数据库中的嵌套结构的方法。

If you want to create a Customer, start in your controller如果你想创建一个客户,从你的 controller 开始

var bobCustomer= new Customer()
    {
         Name = "Bob",
         Profession = "Builder",
         Document = docObject, //you need a prepared document object here   
    };

Then you would call your service using _serviceCust.CreateCustomer(bobCustomer)然后您将使用_serviceCust.CreateCustomer(bobCustomer)调用您的服务

If you want to learn more advanced methods of updating database tables, look into transaction contexts .如果您想了解更新数据库表的更高级方法,请查看事务上下文 They will allow you to piece-by-piece manipulate your database and still maintain ACIDity.它们将允许您逐个操作您的数据库并仍然保持 ACIDity。

If you had to have logic in your controller, however, you could do the following pseudocode但是,如果您的 controller 中必须有逻辑,则可以执行以下伪代码

using(var ctx = new ContextTransaction()){
    var customer = new Customer();//...create customer object...
    _serviceCust.Update(customer);
    ctx.SaveChanges();
    var sub = new SubCustomer();
    sub.ParentCustomer = customer.Id; //use the customer id as a foreign key for your subcustomer
    _serviceSubCust.Update(sub);
    ctx.SaveChanges();
    //use the subcustomer id as a foreign key for your other nested classes, so on so forth...
    ctx.Commit();
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM