Skip to main content

Could this be a good case when to use 'params'

In urma cu cateva saptamani am discutat putin despre keyword-ul params si cautam locuri unde acesta ar putea sa fie folosit cu un scop clar, nu doar de dragul de a il folosi. Zilele acestea am gasit un caz unde acesta ar putea sa isi gaseasca locul.
Sa presupunem ca scriem o metoda care genereaza semnatura unei metode pe baza. Numarul de parametrii a unei metode poate sa fie variat de la 1 la n si chiar 0. Din aceasta cauza pentru a putea sa acoperim cele doua cazuri ar fi nevoie sa avem ca si parametru o colectie de parametri care sa accepte sa fie si null.
public string GetMethodSignature(string methodName, List<object> parameters = null)
Acuma apar usoare probleme pentru cel care ne foloseste metoda. Pentru fiecare apel cand are unul sau mai multi parametri o sa fie nevoit sa creeze o lista de elemente.
myObj.GetMethodSignature( "FooAction" , new [] { param1 });
In acest caz, o implementare folosind params ne simplifica putin atat apelul cat si modul in care procesam datele:
public string GetMethodSignature(string methodName, params object[] parameters)
...
myObj.GetMethodSignature( "FooAction" , param1 );
myObj.GetMethodSignature( "FooActionNoParam");
Pentru cazul cand nu avem nici un parametru este de ajuns sa apelam metoda ca si mai sus, iar colectia noastra nu o sa aibe nici un element.
Cum vi se pare acest caz? Credeti ca in acest caz este folositor "params"?

Comments

  1. Depinde ce se intelege prin "metoda care genereaza semnatura unei metode pe baza" - daca e vorba de metode overloaded, acestea pot diferi nu doar prin numarul si numele parametrilor, ci si prin tipul lor - deci pot exista doua metode:
    FooAction(int a);
    FooAction(DateTime a);

    Altfel, varianta cu params e doar un mic sintactic sugar care usureaza munca programatorului - mai important mi se pare ca
    List parameters
    e destul de vag si nu sugereaza ce ar trebui pasat acolo fara a citi ceva documentatie/comments..

    In plus, daca in viitor cineva va vrea intr-o clasa derivata sa adauge un parametru intr-un overload al metodei respective, de genul:
    public string GetMethodSignature(string methodName, int newParam, params obj ect[] parameters)
    poate introduce un breaking change in codul existent.
    Deci as evita params intr-un framework/API public, dar no problems in interiorul unei aplicatii..

    ReplyDelete
    Replies
    1. Pe partea de breaking change in codul existent e un risc destul de mare daca expui o metoda in API ce foloseste params.
      Params in sine e un sintactic sugar, eu incercam sa vad poate sa fie util cu adevarat.
      parameters ar putea sa fie redenumit parametersMethodValue, poate asa ar fi mai usor de inteles ce reprezinta.

      Delete

Post a Comment

Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

Continuous DeploymentTestingIsolationSecurity at container levelVersioning ControlEnvironment Standardization
Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you…