简体   繁体   English

从部分类c#调用Base实体构造函数

[英]Calling Base entity constructor from partial class c#

Consider the below scenario, 考虑以下情况,

I have entity created by DB First approach, 我有DB First方法创建的实体,

// auto-generated class
public partial DBEntity : DBContext
{
   public DBEntity ()
        : base("name=DBEntity ") // gets connection string from api.config
    {
    }
}

I have a situation where I need to build and pass the connection string at times, So I created this, 我有时需要构建并传递连接字符串,所以我创建了这个,

public partial class DBEntity 
{
    public DBEntity (String connectionString)
    {
        if (string.IsNullOrEmpty(connectionString))
            new DBEntity ();
        else
            base(connectionString); // this one fails
    }
}

What would be the best way to 什么是最好的方式

  1. call the parameterless entity constructor if connection string is empty? 如果连接字符串为空,则调用无参数实体构造函数?
  2. call the base constructor from the parametrized constructor if conn string is not empty and achieve what happens below, 如果conn string不为空,则从参数化构造函数中调用基础构造函数,并实现下面的操作,

public DBEntity (String connectionString) : base (connectionString)

I will always be doing a new DBEntity(string) 我将永远做一个new DBEntity(string)

You can not change the reference of the object being created in constructor. 您无法更改在构造函数中创建的对象的引用。 In another words when you call a constructor the object is initialized in memory and you are just setting it up. 换句话说,当你调用一个构造函数时,对象在内存中初始化,你只需要设置它。

Instead you should use a static method which allows you to control the return type. 相反,您应该使用静态方法,它允许您控制返回类型。 You can put it in another class like DbEntityFactory and call DbEntityFactory.Create or put it in your DbEntity class and call DbEntity.Create . 您可以将它放在DbEntityFactory等其他类中,并调用DbEntityFactory.Create或将其放在DbEntity类中并调用DbEntity.Create

public static DBEntity Create(String connectionString)
{
    if (string.IsNullOrWhiteSpace(connectionString)) //use this instead of IsNullOrEmpty
        return new DBEntity();
    else
        return new DBEntity(connectionString); 
}

You can't delay the call to the base constructor. 您不能延迟对基础构造函数的调用。 The base constructor always fires first. 基础构造函数始终首先触发。 This is to prevent you have a half-initialized class. 这是为了防止你有一个半初始化的类。

I think you should create two constructors if you want to deviate the call to the base class. 我想你应该创建两个构造函数,如果你想将调用偏离基类。 Or, as dotctor mentions: create a factory class. 或者,如dotctor所述 :创建一个工厂类。 If that will work may depend on the need to use generics and initialize instances from those generic types. 如果这将起作用可能取决于使用泛型和初始化这些泛型类型的实例的需要。

At the moment you are allowing a null or empty string as a value for connectionString , and I think that is wrong. 目前你允许null或空字符串作为connectionString的值,我认为这是错误的。

If you want to use the constructor that has an argument connectionString , then you have to supply a valid value. 如果要使用具有参数connectionString的构造函数,则必须提供有效值。 If you do it this way, then the whole problem goes away, since that constructor doesn't have the duality anymore. 如果你这样做,那么整个问题就会消失,因为那个构造函数不再具有二元性。

按照“构造函数中没有逻辑”这个简单的规则,我将提供两个构造函数 - 一个有一个,一个没有参数。

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

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