开发者

Best approach to call similar methods on c#

I have Business Objects classes that need to know what connection string have to use.

I call/create those BO from my code passing the connection String to a connString property of the BO, and can be called/created too from framework controls, and this doesn't allow me set connString property. (I have to call the method with one additional parameter for connectionstring)

I have something like this:

public class MyBOClass{
    public FillMethodX(int ID)
    {
        //Fill the BO with data...
    };
    public FillMethodX(int ID, string connString)
    {
        SetConnString(connString);
        FillMethodX(ID);
    };

    public FillMethodY(int ID)
    {
        //Fill the BO with data...
    };
    public FillMethodY(int ID, string connString)
    {
        SetConnString(connString);
        FillMethodY(ID);
    };
}

Can I avoid to replicate each method with a overload to pass a connString with a best approach? (optional parameter,generics, etc??)

Thanks.

edit: sorry I put "connection string" but开发者_JAVA技巧 really I passing a string that represents a database that my BOs use internally.


There are many ways you can do this, one of which you mentioned already - optional arguments.

Another way would be to expose a generic ConnectionString property that returns the current connection string, and then do away with the 1 parameter overload. Callers would be required to pass a connection string, and can just pass MyBOClass.ConnectionString. This may actually be a little clearer to readers of the code who wonder where the connection info comes from in the first place.


Remove the method without the parameter and just have:

 public FillMethodY(int ID, string connString)     
 {         SetConnString(connString);  //If connString is null, a default connection will be used       
           FillMethodY(ID);     
 }; 

For callers who want to have use a default, pass null for the connString and then in SetConnString handle the null case and return a default connection. Otherwise have it use what is passed if not null and set the connection to that.

Then you don't need the FillMethodY(int ID) and the method calls are reduced in half.


You can create a new constructor on your class accepting ConnectionString or create a public property ConnectionString, you can then retrieve that value inside your methods.

public class MyBOClass{

private string connectionString;
public MyBOClass(string connectioString)
{
    this.connectionString=connectionString;
}

public FillMethodX(int ID)
{
    //here use connectionString
    //Fill the BO with data...
};

hth

updated

using optional parameters:

public class MyBOClass{


public FillMethodX(int ID,[Optional, DefaultParameterValue(string.Empty)] string connString)
{
    //test if connString is null 
    SetConnString(connString);
    FillMethodX(ID);
};


Don't pass the connection string in the BO layer. Instead use an indentifier and then a factory in your DAL layer to go and get the connection string/DAL.

For example, DAL Dll config file:

<Connections>
    <connection name="test" connectionString="Data Source=####\SQL2008;Initial Catalog=test;User Id=####;Password=####;" providerName="System.Data.SqlClient">test</connection>
    <connection name="test2" connectionString="Data Source=####\SQL2008;Initial Catalog=test2;User Id=####;Password=####;" providerName="System.Data.SqlClient">test2</connection> 
</Connections>

When a client logs in to your application, give them a drop down of available connections (or hide it via a config file if you don't want users choosing the connection) and then use that to factory out a database connection.

The objects that are passed between layers would have this identifier. Use the identifer to create the DAL conneciton.

Sample data transfer object that could be passed between layers (with identifier):

    /// <summary>
    /// A data transfer object is sent between layers.  It has a few base properties
    /// as well as a list of payload objects.
    /// </summary>
    [DataContract(Namespace= SharedModelNamespace.Namespace.SharedModel)]
    public class DataTransferObject
    {
        /// <summary>
        /// As part of the data transfer object, it specifies what environment to connect
        /// to should it make a DAL call.
        /// </summary>
        [DataMember]
        public string DataEnvironment {get; set };

        /// <summary>
        /// These are the list of objects (the payload) that will be transferred
        /// back and forth from the server to the client.
        /// </summary>
        [DataMember]
        public List<BasePOCO> DataTransferObjects { get; set; }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜