Generic programming is a style of computer programming in which algorithms are written in terms of to-be-specified-later types that are then instantiated when needed for specific types provided as parameters.
7
votes
1answer
90 views
Model simulation using Java annotations
A couple of days ago I posted my code that models systems made up of moving parts. I got several great tips and ideas and here is my latest version. My actions are now methods annotated with @Action ...
7
votes
2answers
125 views
Game passive skill system
I'm trying to create some easily accessible database of different skills. General idea is that every skill should do something different and be able to act on different things (ex. one skill which ...
2
votes
1answer
41 views
Delegate that selects a writable property on a generic
In the following contrived example, I have a collection of PropertyManagers that each contains selector and assigner delegates to read from and write to a property on the generic type. It feels clunky ...
5
votes
1answer
41 views
Monadic Immutable Linked List in the Least Functional Language Evar
I've written a List monad as an example for a related question. This is a rather frustrating experience as I wanted to use Java, which (as of Java 7) still lacks lambda expressions and lacks ...
6
votes
2answers
89 views
A monad in Java (FriendSpace and People)
After asking a similar question on Stack Overflow, I'm picking up on the answer there to improve my monad. I'm not trying to solve the general case, just come up with one to see how it works. If I'm ...
11
votes
2answers
325 views
Is this C# 'hack' a bad idea?
As C# doesn't have generic specialisation like C++ template specialisation and I wanted a way to be able to do it, I came up with a little hack that goes a bit like this:
public sealed class ...
6
votes
3answers
124 views
Factory for classes unknown at compile time
I have a class called Machine. It contains a list of parts and a list of actions. The list of parts will contain instances of the Part class subclasses and the list of actions will contain instances ...
6
votes
2answers
67 views
Abstract Factory Experiment
While I should probably be using Dependency Injection via Ninject or a similar project, I have been attempting to implement an abstract factory design that would provide me with the following easy to ...
7
votes
1answer
79 views
Cache wrapper - Generics vs Dynamic
I've implemented a common wrapper pattern I've seen for the .NET cache class using generics as follows:
private static T CacheGet<T>(Func<T> refreashFunction, [CallerMemberName]string ...
1
vote
1answer
64 views
A genric extension method to filter Linq-EF queries
I have various types of EF entities, all of them have a navigation property called "Employee". When generating reports the user will have the option to filter the report according to the different ...
4
votes
1answer
60 views
Mapping from multiple sources to destination using JMapper
I am using JMapper to map from multiple sources to a destination class. Could you review it and let me know if the code looks OK to you? Please suggest any changes that I can make to improve it.
...
2
votes
3answers
57 views
Forcing type-safe IDs for use with Collections and Maps
Introduction
I have a hierarchically nested tree class (like SampleFolder, s.b.) that I want to serialize.
The problem arises with the collection of sub items like this one:
...
6
votes
1answer
101 views
Genetic C++ programming with Brainfuck
This was a project I worked on for fun that basically uses a genetic algorithm to produce simple Brainfuck programs that output whatever the user specifies.
I was just was hoping for criticism of the ...
6
votes
1answer
86 views
PriorityQueue<T> Implementation — Flaws, Thoughts, General Feedback
I have been looking desperately for some more experienced .NET (C#) software developers to take a look at my code, and provide me with some good ol' honest feedback and/or criticism about it. It only ...
2
votes
1answer
71 views
VARIANT structure deserialization code organization
In my project i work with COM object via System.Reflection. COM object returns pointer on structure VARIANT, that i cast as object and then i cast one as byte array. There are many structures that i ...
4
votes
1answer
79 views
Generic 'temporary instance' class
I've been reading about WeakPointer and WeakPointer<T> today, and it looks very useful. Rather than just using it as-is though, I decided to write a wrapper around it that covers a common usage ...
1
vote
1answer
69 views
Java reflection: Inject data generically and safely
I have become interested in reflection and I wrote a class to wrap an Object so that I can access its private instance fields. The code works great. I do not need to wory about the exceptions in the ...
2
votes
2answers
93 views
Add item to an Array 2D using LINQ
I've wrote a generic function to add an item to an Array 2D
This is what I have:
Private Sub Add_Item_Array_2D(ByRef Array_2D As String(,), _
ByVal Items As String())
...
1
vote
2answers
91 views
Covariance in generic collections
I have a some classes and interface
public interface IGeoObject
{
GeoCoordinate GetGeocoordinate();
}
public class User : IGeoObject
{
public GeoCoordinate ...
1
vote
1answer
121 views
Opening a list of URLs and splitting the queries into different files
I recently made a program in Python to open a list of URLs and split the queries into different files. I want to make this code more generic and simple. I am open to suggestions.
def parse_file():
...
3
votes
1answer
52 views
How can I improve my Java MethodPointer?
This was just an experiment to see if I could replicate something like C++ function pointers in Java. Basically, the idea was to have an object which represented a method call, and when you call ...
2
votes
1answer
334 views
Using generic methods for basic crud operations
Regarding re-usability, is this OK? What might go wrong? What would be a better design? Performance-related issues and any other comments are welcome.
package sample.library.dao.util;
import ...
0
votes
0answers
172 views
Binding and iterating a database table to a List<T> in C#
I would like to iterate through the records of a database table using a C# List of my custom class. Is this the most performant way to handle this? (Simplified for readability)
static void ...
3
votes
1answer
43 views
Refactoring my windowsFormApp code
I have around 150 lines of code that I managed to optimize.
But, because my two methods and the six if-statements almost are identical, could there be room for improvement? Maybe it could be done ...
2
votes
1answer
131 views
How to remove duplicated code? AOP and Generics are Ok
I have figures - TriangleItem and CircleItem:
public abstract class BaseItem
{
protected int _id;
protected string _description;
}
public sealed class TriangleItem : BaseItem
{
public int ...
2
votes
3answers
229 views
Better way to write generic method to convert List<List<T>> to T[][]
I was working through getting myself past the creation of a generic method, that converts a List<T> or List<List<T>>, to T[] or T[][] respectively. Well, let's just consider a ...
2
votes
0answers
798 views
Review my Generic ASP.net MVC controller that generates many pages?
I recently set out to create an open-source ASP.net MVC web development framework. Specifically, I wanted to automate some of the tasks associated with the creation of data-driven applications. I've ...
3
votes
2answers
435 views
Tips on multiple key Map-wrapper
I'm creating a simple generic Map-wrapper with multiple keyed values.
I'm intending to use it with storing edges in a graph, where an edge goes from one vertex to another. Thus, I can fetch any of ...
2
votes
2answers
82 views
Generically typed interface
I came across the following code in our code base:
public interface ICloneable<T>
{
T Clone();
}
public class MyObject : ICloneable<MyObject>
{
public Stuff SomeStuff { get; set; ...
5
votes
2answers
392 views
Generic Task Blocking Queue
A generic blocking queue has the following properties:
It is thread-safe.
It allows queuing and de-queuing of items of a certain type (T).
If a de-queue operation is performed and the ...
1
vote
1answer
407 views
Java; Generic Observer/Observable - is this as messy as I think?
I have recently had a whole load of help trying to roll my own loosely-coupled Observable/Observer paradigm, here: Loosely coupled observer pattern
To keep things simple and to aid in my ...
6
votes
2answers
370 views
Improvement requested for: Generic Calculator and Generic Number
.NET does not support generic numbers. It is not possible to enforce a generic method with generic argument T that T is a number. The following code will simply not compile:
public T ...
2
votes
2answers
436 views
Sequential Execution: Orchestrator Pattern
I built a code piece that makes it easy to execute a set of sequential operations, that might depend on the same parameter value (passed across the execution) or might need a particular parameter.
...
6
votes
2answers
238 views
Generic wrapper for equality and hash implementation: Is there a way I can make it more efficient?
I was writing an answer in another thread and came across this challenge. I'm trying to have a generic class so that I can delegate the routine (and tiring) Equals and GetHashCode implementation to it ...
0
votes
2answers
85 views
I would just like to find out if the code below is the correct way of creating a generic list and populating it?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
public class InnerJoinTables
{
public ...
4
votes
2answers
208 views
Move object by one up or down algorithm in a custom order
Basically, I did an object (using hibernate) with a field called sorting_order. This field needs to be unique and I wish to swap two object by one. So one element has to be after or before the current ...
3
votes
2answers
1k views
Is there a better way of defining generic entity classes?
I have some entities in use in my project, and to make things easier, I would like to have the type of the key for that entity defined via a generic. E.g.:
public abstract class Entity<T>
{
...
0
votes
1answer
123 views
Improving my code to be more generic
This is one of a classes (that sends email base on object type to AM (Role)) that is executed by a engine. I have also commands like SendEmailToAOCommand that send email to role AO base on object ...
3
votes
2answers
99 views
How would you improve this object model design to get around Covariance Issues with ObjectModel.Collection<T>?
Consider the following simple relationship.
Code
[DataContract(Name = "Wheel")]
public class Wheel { }
[DataContract(Name = "OffRoadWheel")]
public class OffRoadWheel : Wheel { }
...
7
votes
2answers
243 views
Synced/Atomic access
Forward
I would love any comments you have, any ideas, any flaws you can find, and any suggestions you might have regarding the code below.
If this is similar to other implementations, I would love ...
1
vote
3answers
135 views
Can I somehow tidy up this (overuse?) of generics?
I'm building a generic flat file reader which looks something like this.
public class GenericReader<TComposite, THeader, TData, TTrailer>
where TComposite : GenericComposite<THeader, ...
0
votes
1answer
133 views
Refactoring code into a generic method [closed]
I have a reoccuring code block in my EntityFramework backed repository which I would like to genericise somehow and call as a method, so reuse the code rather than repeat it.
The current code block ...
2
votes
2answers
142 views
how to write generic extensions for standard collection types in java?
I am a Java beginner and I am looking for an idiomatic way of writing a function that involves generics. I wrote this helper class (below) that pushes items into a sorted generic collection and I ...
5
votes
2answers
533 views
Generic Wrapper for a Task-based API
I've written a piece of an ugly code.
The idea was to provide a clear-looking, type-inferring framework to test a task-based API (it's for integration testing, and test methods should be as clear as ...
8
votes
1answer
391 views
using <T> in generic delegates?
Here is my problem. Do I need to give a new identifier to each delegate I write of the following delegate type? like so: or could i use one delegate that accounts for any Datatype I need to use so i ...
2
votes
1answer
1k views
Review of a generic request handler class (RestSharp)
I wrote a class that uses RestSharp to access Rest and HTTP API web services. I tested it and it works however I was wondering if any changes could be made to it to make it better.
public class ...
10
votes
3answers
198 views
Cast inside the method or let the client code cast, which one is better?
I have two choices of implementing a method, the first one is a generic type where the client code does not have to cast
public T GetControl<T>(string controlName) where T : Control
{
...
3
votes
3answers
670 views
ReadOnlyCollection - my alternative to .NET one
I wanted something more flexible than System.Collections.ObjectModel.ReadOnlyCollection.
The goal was: consuming classes should only see what I want to show them, not whole underlying collection.
For ...
4
votes
2answers
236 views
Online-Offline Class Manager
Router is a generic class that manage multiple contracts that. It is able to find out wheter it's an online or offline situation, on the very moment when an operation is being made.
There's a really ...
4
votes
1answer
146 views
OOP: Need tips of my approach in .Net
I am looking for some different approaches to this following OOP scenario.
Dim pb As Object = Nothing
If radCars.Checked = True Then
Dim productBase As ProductsBase(Of CarColumns)
...