logo
down
shadow

Looking way to make generic out of this code


Looking way to make generic out of this code

Content Index :

Looking way to make generic out of this code
Tag : chash , By : mediafarm
Date : September 13 2020, 03:00 AM

like below fixes the issue
Could any one have any idea or any suggestions on how to refactor these things in more OO approach or generic way that would be very grateful to me.
var environmentCondition = new EnvironmentConditions
{
    ConditionType = spaceTypeInput.EnvironmentConditionsInput.ConditionType,
    CoolingSetPointOccupied = spaceTypeInput.EnvironmentConditionsInput.CoolingSetPointOccupied ?? 0,
    CoolingSetPointUnOccupied = spaceTypeInput.EnvironmentConditionsInput.CoolingSetPointUnOccupied ?? 0,
    HeatingSetPointOccupied = spaceTypeInput.EnvironmentConditionsInput.HeatingSetPointOccupied ?? 0,
    RelativeHumidityMax = spaceTypeInput.EnvironmentConditionsInput.RelativeHumidityMax ?? 0,
    RelativeHumidityMin = spaceTypeInput.EnvironmentConditionsInput.RelativeHumidityMin ?? 0
};
public class EnvironmentConditions
{
    ...
    public EnvironmentConditions(EnvironmentConditionsInput input)
    {
        ConditionType = input.ConditionType;
        CoolingSetPointOccupied = input.CoolingSetPointOccupied ?? 0;
        CoolingSetPointUnOccupied = input.CoolingSetPointUnOccupied ?? 0;
        HeatingSetPointOccupied = input.HeatingSetPointOccupied ?? 0;
        RelativeHumidityMax = input.RelativeHumidityMax ?? 0;
        RelativeHumidityMin = input.RelativeHumidityMin ?? 0;
        // some new property
    }
}
// new EnvironmentConditions(spaceTypeInput.EnvironmentConditionsInput);
public class SpaceTypeServices
{
    public Guid CreateRequest(...)
    {    
        // rest  
        var spaceType = new SpaceType(spaceTypeInput);
        dbContext.SpaceTypes.Add(spaceType);
        dbContext.SaveChanges();
        return id;
    }
}

Comments
No Comments Right Now !

Boards Message :
You Must Login Or Sign Up to Add Your Comments .

Share : facebook icon twitter icon

How can I make this code more generic


Tag : chash , By : AdrianB
Date : March 29 2020, 07:55 AM
it should still fix some issue Make the interface generic, then use a Func as a selector for the key. This assumes that you want the key for the dictionary to be extracted from the node. If this isn't a hard requirement, then you could specify the key itself using the generic type specifier in the signature.
public interface ITopology<T>
{ 
    Dictionary<T, INode> Nodes { get; set; }  
} 

public static class TopologyExtns 
{ 
    public static void AddNode<T>(this ITopology<T> topIf, INode node, Func<INode,T> keySelector ) 
    { 
        topIf.Nodes.Add( keySelector(node), node ); 
    } 
    public static INode FindNode<T>(this ITopology<T> topIf, T searchKey ) 
    { 
        return topIf.Nodes[searchKey]; 
    } 
} 

public class TopologyImp<T> : ITopology<T> 
{ 
    public Dictionary<T, INode> Nodes { get; set; } 

    public TopologyImp() 
    { 
        Nodes = new Dictionary<T, INode>(); 
    } 
}
public interface INode<T>
{
     T Key { get; }
     string Name { get; set; }
     int ID { get; set; }
}

public class StringNode : INode<string>
{
    public string Key { get { return this.Name; } }
    public string Name { get; set; }
    public int ID { get; set; }
}

public interface ITopology<T> 
{  
    Dictionary<T, INode<T>> Nodes { get; set; }   
}  

public static class TopologyExtns  
{  
    public static void AddNode<T>(this ITopology<T> topIf, INode<T> node )  
    {  
        topIf.Nodes.Add( node.Key, node );  
    }  
    public static INode<T> FindNode<T>(this ITopology<T> topIf, T searchKey )  
    {  
        return topIf.Nodes[searchKey];  
    }  
}  

public class TopologyImp<T> : ITopology<T>  
{  
    public Dictionary<T, INode<T>> Nodes { get; set; }  

    public TopologyImp()  
    {  
        Nodes = new Dictionary<T, INode<T>>();  
    }  
}
var topology = new TopologyImp<string>();
topology.AddNode( new StringNode { Name = "A", ID = 0 }  );
var node = topology.FindNode( "A" );

How to update Non-Generic code to make it Generic?


Tag : java , By : user116330
Date : March 29 2020, 07:55 AM
Hope this helps In Java Tutorials there are examples how to convert legacy code to use generics:
http://docs.oracle.com/javase/tutorial/extra/generics/convert.html

How to make this code generic


Tag : python , By : Liy
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have a use case telling that when a tag is link and it's attributes are rel=dns-prefetch and prefetch then just say that pre resolving dns is enabled. , If you want to do this with Bs4 You could use this:
from bs4 import BeautifulSoup
soup = BeautifulSoup(html)  # Or some xml-like structure
# This will select all link tags which have a rel attribute with value dns-prefetch.
if len(soup.select('link[rel=dns-prefetch]')) > 0:
    self.pre_resolve_dns_enabled = True
if len(soup.select('link[rel=prefetch]')) > 0:
    self.prefetch_enabled = True

Is there a way to make the following code more generic?


Tag : java , By : Milander
Date : March 29 2020, 07:55 AM
To fix the issue you can do You could pass a function into the sum method to get the value from each line that you want to sum:
public int sumLines(Function<Line, Integer> extractor){
    Line[] lines = ...
    return Arrays.stream(lines).map(extractor)
        .filter(Objects::nonNull).mapToInt(i -> i).sum();
}
....
int time = sumLines(Line::getTime);

How to make this JSX code more generic to avoid code reuse in React?


Tag : reactjs , By : Mariocki
Date : March 29 2020, 07:55 AM
will be helpful for those in need The code can be extracted to a component. In case some parameters are common in some cases, it can be higher-order component that accepts common parameters:
const boxHOC = (count, avg_length) => props => (
    <div className="box box-default">
        <div className="box-header with-border">
        <h3 className="box-title">Strings Info</h3>
        <div className="key-details">
            <dl className="dl-horizontal">
                <dt>Count</dt>
                <dd>{count}</dd>
                <dt>Average Length</dt>
                <dd>{avg_length}</dd>
            </dl>
        </div>
        </div>
        <div className="box-header with-border">
            <h3 className="box-title">Strings</h3>
            <div>
                <pre>
                    {props.raw_strings}
                </pre>
            </div>
        </div>
    </div>
);

const OneTwoBox = boxHOC(1, 2);
const ThreeFourBox = boxHOC(3, 4);
Related Posts Related QUESTIONS :
  • How to return to previous search page without being asked to Confirm Form Re-submission and keeping the results on ASP.N
  • How set a identity scaffolding item/page how initial page in asp.net MVC core?
  • LINQ isn't calling Dispose on my IEnumerator when using Union and Select, expected behavior or bug?
  • What is "ByteArray.uncompress()" in AS3 equivalent to in C#?
  • Getting a specific letter from a string variable for my simple guessing game for clues
  • Send an email with Outlook without a subject --- dialog box issue
  • passing List<MyModel> from my controller in the "WebInterfaceProject" to the processor method in "D
  • How to convert Word document created from template by OpenXML into MemoryStream?
  • How can I make a single slider that changes the color of an object?
  • Remap JSON parameter in c#
  • What is the difference between "this ref" and "ref this" when talking about C# 7.2 ref extension met
  • Convert OpenSSL encryption into native C#
  • Accessing Properties in Razor Pages
  • How to get SOAP element value
  • Projection after Group
  • C# error cannot convert sytem.text.regularexpressions.match to string
  • Issues with Save/Load System in a Text Based Adventure game made with ScriptableObjects in Unity
  • VS2019 MSBuild.exe - ASP .Net MVC project fails to publish when using PublishProfile, but works when using OutDir parame
  • Does <pages validateRequest="false" /> in Web.config still matter?
  • How to send new request to redirect URL with new access token
  • Attempt to invoke virtual method on a null object reference Xamarin LockScreen
  • "The attribute names could not be inferred from bind attribute 'bind-value'" exception in Blazor
  • How to fix ''System.ArgumentException" in c#?
  • C#. Ref returning delegate for ref extension method
  • Swashbuckle Swagger generate an actual guid
  • Trying to make thousands of database calls simultaneously - they stack instead of running concurently
  • run mstest from cmd using testcontainer
  • How to make M:N (many-to-many) relationship where both M and N are the same entities?
  • Consume COM DLL from multiple clients
  • Convert json string response representing UCHAR array to Byte array
  • Why does this LinqPad program produce different results on the second run?
  • ServiceStack how to use MaxLoginAttempts feature
  • What does "late-bound access to the destination object" mean?
  • How to load appsettings.json inside a Static class based on deploy environment so that I can use in other class librarie
  • dynamically add images to view
  • Are static extension methods on User object safe?
  • Process thousands of database calls simultaneously
  • Trying to decrypt a string with public key(not private) using bouncycastle in .net
  • How to retrieve the name of an usb device
  • Query many-to-many related data using Entity Framework Core
  • ASP.NET MVC : how to find the view from a controller?
  • Inconsistent behaviour using Entity Framework for ASP.NET MVC app vs windows service
  • AutoFixture Customize vs Build
  • How to change textbox location at the click of a mouse in live mode?
  • How can I deserialize a XML file without defining the parent node?
  • asp.net core how decrease class coupling
  • ASP.NET Core Razor Pages Routing With parameters
  • String Escaping With $ Interpolation and @ Encoding
  • ConfigurationManager in ASP.NET WebAPI tested with HttpServer
  • Top down shooting in Unity 2D don't work as I want it to
  • Use own redirectUri when the Authentication expires
  • Using "foreach" in string array c#
  • "Object reference not set to an instance of an object" for create multiply models on one view
  • Get data passed trought pushAsync
  • Selector expression dynamic on IQueryable
  • Logut with SignInManager not calling method
  • Exit event handler with MessageBox OK button
  • Can not load view from Custom Renderer Xamarin Forms
  • Looking for a .Net ORM
  • Cost of SortedDictionary.Count
  • shadow
    Privacy Policy - Terms - Contact Us © scrbit.com