C# 'Object Pool' with too many generics












0












$begingroup$


I've implemented a generic object pool, to use it, you write a class that inherits from this class:



public abstract class WorkerBase<S, I, O> {
public WorkerBase() { }

public virtual bool Initialize() {
return true;
}

public abstract List<O> Process(S settings, List<I> input);
}


You then construct a single ObjectPool with your class, and call Process() to have the ObjectPool manage instantiation, construction, etc of your WorkerBase:



static ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output> myPool = new ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output>();

// Later...
var output = myPool.Process(settings, input);


ObjectPool looks like this:



public class ObjectPool<T, S, I, O> where T : WorkerBase<S, I, O>, new() {
/* Much implementation omitted for sake of clarity */

public List<O> Process(S settings, List<I> input) {
// Indirectly calls worker's Process function
}
}


What bothers me is the laundry list of generics that are needed to construct one of these things; it smells bad to me. I considered somehow merging the ObjectPool and WorkerBase classes, but it seems impractical, as the ObjectPool is sort of a 'container of' Workers. I could possibly eliminate S, I and O by using object types and casting when appropriate, but I think I'd prefer generics over the casting.



Why This Thing?



I have several unmanaged C++ DLLs that take a bit of time to initialize, so having a pool of them is useful. However, this is a .NET Core web service, so a traditional object pool will mean that when a pool member is called upon to process, it will be running on a thread different than the one where it was created (bad for these C++ DLLs). This issue is addressed in the object pool using something similar to a producer/consumer. In addition, the C++ code is a bit leaky and buggy, which is managed with a watchdog.










share|improve this question







New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Code Review. Your code is very generic and almost non-existent. However, here we need actual code from an actual project. If you're asking about best practice in a general context, try Software Engineering, but please check their on-topic rules first.
    $endgroup$
    – Zeta
    36 mins ago
















0












$begingroup$


I've implemented a generic object pool, to use it, you write a class that inherits from this class:



public abstract class WorkerBase<S, I, O> {
public WorkerBase() { }

public virtual bool Initialize() {
return true;
}

public abstract List<O> Process(S settings, List<I> input);
}


You then construct a single ObjectPool with your class, and call Process() to have the ObjectPool manage instantiation, construction, etc of your WorkerBase:



static ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output> myPool = new ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output>();

// Later...
var output = myPool.Process(settings, input);


ObjectPool looks like this:



public class ObjectPool<T, S, I, O> where T : WorkerBase<S, I, O>, new() {
/* Much implementation omitted for sake of clarity */

public List<O> Process(S settings, List<I> input) {
// Indirectly calls worker's Process function
}
}


What bothers me is the laundry list of generics that are needed to construct one of these things; it smells bad to me. I considered somehow merging the ObjectPool and WorkerBase classes, but it seems impractical, as the ObjectPool is sort of a 'container of' Workers. I could possibly eliminate S, I and O by using object types and casting when appropriate, but I think I'd prefer generics over the casting.



Why This Thing?



I have several unmanaged C++ DLLs that take a bit of time to initialize, so having a pool of them is useful. However, this is a .NET Core web service, so a traditional object pool will mean that when a pool member is called upon to process, it will be running on a thread different than the one where it was created (bad for these C++ DLLs). This issue is addressed in the object pool using something similar to a producer/consumer. In addition, the C++ code is a bit leaky and buggy, which is managed with a watchdog.










share|improve this question







New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$












  • $begingroup$
    Welcome to Code Review. Your code is very generic and almost non-existent. However, here we need actual code from an actual project. If you're asking about best practice in a general context, try Software Engineering, but please check their on-topic rules first.
    $endgroup$
    – Zeta
    36 mins ago














0












0








0





$begingroup$


I've implemented a generic object pool, to use it, you write a class that inherits from this class:



public abstract class WorkerBase<S, I, O> {
public WorkerBase() { }

public virtual bool Initialize() {
return true;
}

public abstract List<O> Process(S settings, List<I> input);
}


You then construct a single ObjectPool with your class, and call Process() to have the ObjectPool manage instantiation, construction, etc of your WorkerBase:



static ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output> myPool = new ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output>();

// Later...
var output = myPool.Process(settings, input);


ObjectPool looks like this:



public class ObjectPool<T, S, I, O> where T : WorkerBase<S, I, O>, new() {
/* Much implementation omitted for sake of clarity */

public List<O> Process(S settings, List<I> input) {
// Indirectly calls worker's Process function
}
}


What bothers me is the laundry list of generics that are needed to construct one of these things; it smells bad to me. I considered somehow merging the ObjectPool and WorkerBase classes, but it seems impractical, as the ObjectPool is sort of a 'container of' Workers. I could possibly eliminate S, I and O by using object types and casting when appropriate, but I think I'd prefer generics over the casting.



Why This Thing?



I have several unmanaged C++ DLLs that take a bit of time to initialize, so having a pool of them is useful. However, this is a .NET Core web service, so a traditional object pool will mean that when a pool member is called upon to process, it will be running on a thread different than the one where it was created (bad for these C++ DLLs). This issue is addressed in the object pool using something similar to a producer/consumer. In addition, the C++ code is a bit leaky and buggy, which is managed with a watchdog.










share|improve this question







New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$




I've implemented a generic object pool, to use it, you write a class that inherits from this class:



public abstract class WorkerBase<S, I, O> {
public WorkerBase() { }

public virtual bool Initialize() {
return true;
}

public abstract List<O> Process(S settings, List<I> input);
}


You then construct a single ObjectPool with your class, and call Process() to have the ObjectPool manage instantiation, construction, etc of your WorkerBase:



static ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output> myPool = new ObjectPool<MyPool, MyPool.Settings, MyPool.Input, MyPool.Output>();

// Later...
var output = myPool.Process(settings, input);


ObjectPool looks like this:



public class ObjectPool<T, S, I, O> where T : WorkerBase<S, I, O>, new() {
/* Much implementation omitted for sake of clarity */

public List<O> Process(S settings, List<I> input) {
// Indirectly calls worker's Process function
}
}


What bothers me is the laundry list of generics that are needed to construct one of these things; it smells bad to me. I considered somehow merging the ObjectPool and WorkerBase classes, but it seems impractical, as the ObjectPool is sort of a 'container of' Workers. I could possibly eliminate S, I and O by using object types and casting when appropriate, but I think I'd prefer generics over the casting.



Why This Thing?



I have several unmanaged C++ DLLs that take a bit of time to initialize, so having a pool of them is useful. However, this is a .NET Core web service, so a traditional object pool will mean that when a pool member is called upon to process, it will be running on a thread different than the one where it was created (bad for these C++ DLLs). This issue is addressed in the object pool using something similar to a producer/consumer. In addition, the C++ code is a bit leaky and buggy, which is managed with a watchdog.







c#






share|improve this question







New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Marc BernierMarc Bernier

101




101




New contributor




Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Marc Bernier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • $begingroup$
    Welcome to Code Review. Your code is very generic and almost non-existent. However, here we need actual code from an actual project. If you're asking about best practice in a general context, try Software Engineering, but please check their on-topic rules first.
    $endgroup$
    – Zeta
    36 mins ago


















  • $begingroup$
    Welcome to Code Review. Your code is very generic and almost non-existent. However, here we need actual code from an actual project. If you're asking about best practice in a general context, try Software Engineering, but please check their on-topic rules first.
    $endgroup$
    – Zeta
    36 mins ago
















$begingroup$
Welcome to Code Review. Your code is very generic and almost non-existent. However, here we need actual code from an actual project. If you're asking about best practice in a general context, try Software Engineering, but please check their on-topic rules first.
$endgroup$
– Zeta
36 mins ago




$begingroup$
Welcome to Code Review. Your code is very generic and almost non-existent. However, here we need actual code from an actual project. If you're asking about best practice in a general context, try Software Engineering, but please check their on-topic rules first.
$endgroup$
– Zeta
36 mins ago










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");

StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






Marc Bernier is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215845%2fc-object-pool-with-too-many-generics%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes








Marc Bernier is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Marc Bernier is a new contributor. Be nice, and check out our Code of Conduct.













Marc Bernier is a new contributor. Be nice, and check out our Code of Conduct.












Marc Bernier is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215845%2fc-object-pool-with-too-many-generics%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to make a Squid Proxy server?

Is this a new Fibonacci Identity?

19世紀