如何向数据库连接类添加基础构造函数?

huangapple go评论67阅读模式
英文:

How can i add a base constructor to the database connection class?

问题

I use EfFeatureDal class as a database connection class and i use constructor for this connection. But when i need to call this class from FeatureList i cant add these connection constructor's parameters thats why i need base constructor on EfFeatureDal but i cant add it?

我使用EfFeatureDal类作为数据库连接类,并使用构造函数来建立连接。但是,当我需要从FeatureList中调用这个类时,我无法添加这些连接构造函数的参数,所以我需要在EfFeatureDal中添加基础构造函数,但我无法添加它。

I got this error on base constructor:

在基础构造函数上出现了以下错误:

Error CS7036 There is no argument given that corresponds to the required parameter '_db' of 'GenericRepository.GenericRepository(Context1)'

错误CS7036:没有提供与“GenericRepository.GenericRepository(Context1)”的必需参数“_db”对应的参数。

I got this error when i delete base constructor on new FeatureManager(new EfFeatureDal()):

当我在new FeatureManager(new EfFeatureDal())上删除基础构造函数时,出现了以下错误:

Error CS7036 There is no argument given that corresponds to the required parameter '_db' of 'EfFeatureDal.EfFeatureDal(Context1)'

错误CS7036:没有提供与“EfFeatureDal.EfFeatureDal(Context1)”的必需参数“_db”对应的参数。

英文:

I use EfFeatureDal class as a database connection class and i use constructor for this connection. But when i need to call this class from FeatureList i cant add these connection constructor's parameters thats why i need base constructor on EfFeatureDal but i cant add it?

public class FeatureList:ViewComponent
{

    FeatureManager featureManager = new FeatureManager(new EfFeatureDal());

    public IViewComponentResult Invoke() 
    {
        return View();
    }
}

public class EfFeatureDal : GenericRepository<Feature>, IFeatureDal
{
    public EfFeatureDal(Context1 _db) : base(_db)
    {
    }

    public EfFeatureDal() { }

}

I got this error on base constructor:

> Error CS7036 There is no argument given that corresponds to the
> required parameter '_db' of
> 'GenericRepository<Feature>.GenericRepository(Context1)'

public class FeatureList:ViewComponent
{

    FeatureManager featureManager = new FeatureManager(new EfFeatureDal());

    public IViewComponentResult Invoke() 
    {
        return View();
    }
}

I got this error when i delete base constructor on new FeatureManager(new EfFeatureDal()):

> Error CS7036 There is no argument given that corresponds to the
> required parameter '_db' of 'EfFeatureDal.EfFeatureDal(Context1)'

答案1

得分: 0

你所犯的错误是手动实例化EfFeatureDal对象,而你卡在了陷阱里,实际上你需要了解依赖注入(DI)概念,并使用IoC容器在派生对象中实例化和注入EfFeatureDal对象。你可以在这里看到一个示例。

英文:

The mistake that you made is you are trying to instantiate EfFeatureDal manually and you got stuck in the trap, actually you need to know about DI(Dependency Injection) Concept and use a IoC container to instantiate and inject EfFeatureDal object in your derived objects.
you can see a sample here

答案2

得分: 0

当我需要从FeatureList调用这个类时,我无法添加这些连接构造函数的参数,所以我需要在EfFeatureDal上添加基础构造函数,但我无法添加它?

好吧,如果你能分享FeatureManager的详细信息,那就太完美了。然而,如果你的EfFeatureDal类在其中隐式定义了一个构造函数,如下所示:

public EfFeatureDal(ApplicationDbContext context) : base(context) { }

由于它具有构造函数依赖性,如果你想在某个地方创建它的实例,你必须传递构造函数。但这不是我们每天都做的常见做法。

你可以这样做,如果你需要访问EfFeatureDal的方法或你在那里定义了其他内容,你可以通过在派生类构造函数中注入其接口来调用它。请看以下示例:

public class FeatureList : ViewComponent
{
    private readonly IEfFeatureDal _efFeatureDal;

    public FeatureList(IEfFeatureDal featureDal)
    {
        _efFeatureDal = featureDal;
    }

    public IViewComponentResult Invoke()
    {
        _efFeatureDal... // 从你的存储库中调用任何你想要的内容
        return View();
    }
}

另外一种选择:

如果你已经定义了FeatureManager如下:

public class FeatureManager
{
    public FeatureManager(EfFeatureDal featureDal) { }
}

在这种情况下,如果你想在FeatureList派生类中调用EfFeatureDal,你必须将DbContext作为其构造函数参数传递。你可以这样做:

public class FeatureList : ViewComponent
{
    private readonly ApplicationDbContext _context;

    public FeatureList(ApplicationDbContext dbContext)
    {
        _context = dbContext;
    }

    public IViewComponentResult Invoke()
    {
        FeatureManager feature = new FeatureManager(new EfFeatureDal(_context));
        return View();
    }
}

注意: 我们将_context传递给EfFeatureDal,因为它具有构造函数依赖性,即public EfFeatureDal(ApplicationDbContext context) : base(context) { }。因此,请始终记住在创建派生类的实例时应调用基类构造函数。我建议你查看我们的官方文档以获取更多详细信息

英文:

> When i need to call this class from FeatureList i cant add these
> connection constructor's parameters thats why i need base constructor
> on EfFeatureDal but i cant add it?

Well, It would be perfect it if you would have been shared FeatureManager details. However, If your EfFeatureDal class has implicitly defined a constructor within it as following:

 public EfFeatureDal(ApplicationDbContext context) : base(context) { }

As it has constructor dependency, you must have to pass the constructor if you would like to create its instance somewhere. But this is not the common practice we do everyday.

What you can do is, if you need to access EfFeatureDal methods or anything you have defined there, you can call it in derived class by injecting its interface within derived class constructor. Have a look in following example:

public class FeatureList : ViewComponent
    {

        private readonly IEfFeatureDal _efFeatureDal;

        public FeatureList(IEfFeatureDal featureDal)
        {

            _efFeatureDal = featureDal;
        }

        public IViewComponentResult Invoke()
        {
           _efFeatureDal... // Anything you want to call from your repository
            return View();
        }
    }

Output:

如何向数据库连接类添加基础构造函数?

Alternatively:

If you have defined FeatureManager as following:

public class FeatureManager
    {
        public FeatureManager(EfFeatureDal featureDal) { }
    }

In that scenario, if you would like to call EfFeatureDal in your FeatureList derived class you have to pass DbContext as its constructor parameter. You can do that in following manner:

public class FeatureList : ViewComponent
    {
        private readonly ApplicationDbContext _context;
        public FeatureList(ApplicationDbContext dbContext)
        {

           
            _context = dbContext;
        }

        public IViewComponentResult Invoke()
        {

            FeatureManager feature = new FeatureManager(new EfFeatureDal(_context));
            return View();
        }
    }

Note: We are passing _context to EfFeatureDal because it has constructor dependency that is public EfFeatureDal(ApplicationDbContext context) : base(context) { }. Thus, Always keep in mind base-class constructor should be called when creating instances of the derived class. I would recommend you to check our official document for more details.

huangapple
  • 本文由 发表于 2023年2月14日 01:14:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定