|
Oh no... That Result there is making a sync method out of the async method. And Result shouldn't be used like that... You are asking for deadlocks that way.
|
|
|
|
|
Hi,
I am working on Simple injector DI framework. I need good ideas on how to register interface which is consumed in more than one provider (component).
Below is my requirement.
It is WebAPI project where I have a BaggageController class which will get a request for Add Baggage operation, Based on a provider from the request I have to route the request to concerned class. A Provider can be an Airline A or Airline B.
Request for AddBaggage(BaggageRequest, Provider) from client
--> BaggageController --> Business.Baggage.Implementation (Decision) --> Call Provider A
--> Call Provider B
Below is the code I am registering with Simple injector.
container.Register<ibaggageservice, baggageservice="">(Lifestyle.Scoped);
container.Register<iflightservice, providera="">(Lifestyle.Scoped);
container.Register<iflightservice, providerb="">(Lifestyle.Scoped); --> This line throws an error as IFlightService is already registered.
Since my decision would be taken at runtime, I can not use the RegisterConditional method.
Is there any other way to register dependency at runtime using simple injector?
Thank you
|
|
|
|
|
How would the application know which of the two to create, based on only knowing IFlightService? Register a ProviderStrategy that returns either ProviderA or ProviderB.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
In order to do this, Could you please share some example? I have been searching it from quite a long time.
|
|
|
|
|
Nope, just a description. Instead of registering two different classes for a single interface,
container.Register<IFlightService, ProviderA>(Lifestyle.Scoped); You want one class to register. That one class should invoke the required stuff for either ProviderA or ProviderB.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
I have a set of calculation methods (10) that return a list of decimals, what is the best (simplest) way to invoke the methods and get the return list of decimals?
public void CalcKeyRef()
{
foreach (KeyRefDB oKR in lAllKeyRef)
{
string sMethod = string.Format("DoKR{0}", oKR.KeyRefID);
List<decimal> lResults = ????
}
}
private List<decimal> DoKR1(List<decimal> lResults)
{
lResults = new List<decimal>();
return lResults;
}
Never underestimate the power of human stupidity
RAH
modified 28-Nov-17 20:28pm.
|
|
|
|
|
All you have are the names of the methods?
Where do they come from?
How often do you need to do this?
|
|
|
|
|
You could use reflection: https://www.dotnetperls.com/getmethod
or perhaps an array of delegates:
public delegate List<decimal> DoKR(List<decimal> lResults);
public DoKR[] DoKRs;
public void SomeInit()
{
DoKRs = new DoKR[] { DoKR1, DoKR2, DoKR3, ... };
}
public void CalcKeyRef()
{
List<decimal> param = new List<decimal>();
foreach (KeyRefDB oKR in lAllKeyRef)
{
List<decimal> lResults = DoKRs[oKR.KeyRefID](param);
}
}
|
|
|
|
|
Works for me - only catch was the identity field is not a zero index value.
Thank you.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
You're welcome. My pleasure.
You could add a dummy (or use -1 with the Id)
DoKRs = new DoKR[] { DoKR1, DoKR1, DoKR2, DoKr3, ... };
|
|
|
|
|
If you have the methods available: I'd be using a Dictionary:
public Dictionary<KeyRefID, Func<List<decimal>> KeyRefToFunc;
public List<decimal> GetListById(KeyRefDB oKR)
{
if(KeyRefToFunc == null) throw new ... whatever
if(KeyRefToFunc.ContainsKey(oKR.KeyRefID))
{
return KeyRefToFunc[oKR.KeyRefID]();
}
throw new ... whatever
}
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
This reads as more obscure than Peters solution, possibly because I rarely use dictionary . I will always go for the clearest solution as I will not have to puzzle out the working in the future.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi, MyCroft, When I was first discovering (2006) [^] sticking executable code into Dictionaries, it felt very weird. Now, it's even easier since we have Action, Func, and Lambdas, to get us over the Delegate "syntax hump" that confounds many newcomers to C#. Of course, a Delegate lets you have a params list, and Action, and Func, do not.
Consider:
delegate int Calc(params int[] args);
Dictionary<string, Calc> CalcDict = new Dictionary<string, Calc>
{
{"+", (iary) => iary.Sum()},
{"-", (iary) =>
{
for (var i = 1; i < iary.Length; i++)
{
iary[0] -= iary[i];
}
return iary[0];
}
}
};
From my perspective, this "saves you" writing a 'switch statement, and handles variable length input parameters.
But, trust me ... I'm not trying to "sell" you anything, just adding this for the sake of the discussion.
«While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)
|
|
|
|
|
using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
byte[] buffer = BitConverter.GetBytes(tif);
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int bits = 32;
while (bits > 0)
{
for (int i = 1; i < 33; i++)
{
if (tif == true)
{
tif = false;
goto A;
}
if (tif == false) tif = true;
A:;
output.Write(buffer, 0, bits);
}
}
}
}
}
}
modified 28-Nov-17 20:21pm.
|
|
|
|
|
|
Oh dear
- The smallest unit of "writable data" is a byte.
- Drop that goto (and please don't use it ever again, for anything). Instead: Toggling a boolean variable is as easy as this:
tif = !tif;
- Doesn't help a lot here though because the change will not reflect in buffer . After you create buffer it keeps its value forever.
- You're using the third parameter of the Stream.Write -method wrongly - at least judging from your described intent. It's supposed to be the amount of bytes from buffer , starting at position 0, that should be written into the file stream. But buffer is only 1 byte long and bits says there should be 32 bytes. That's where you are "outside of the array bounds".
- Your while -loop will run forever because bits will be > 0 forever.
You should start using the debugger. Place a breakpoint at the start of your method (first line or opening brace) by placing the cursor there and pressing F9. Then start your program in debug mode by pressing F5. Then step over the execution line-by-line by pressing F10* while you inspect the values of your variables by hovering with the mouse cursor over it. Compare the values you expect with their actual values and when there's a difference try to find out, why.
* : Take a look at further ways of stepping through your code while debugging: In the Debug-Menu and/or the Debugging-Toolbar (and the Debugger-documentation).
If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson
|
|
|
|
|
Reminds me of when I got my first 8088.
Now I use MemoryStream and BinaryWriter.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Sorry, may I ask, why you try to do so?
Do you try to solve HDD performance issue or save flash memory read-write cycles?
Thanks!
|
|
|
|
|
This is my first ever attempt at coding against AD, so bear with me...
Here's what I have:
public static void GetAllUsers()
{
PrincipalContext AD = new PrincipalContext(ContextType.Domain, serverName);
UserPrincipal u = new UserPrincipal(AD);
PrincipalSearcher search = new PrincipalSearcher(u);
foreach (UserPrincipal result in search.FindAll().OrderBy(x => x.DisplayName))
{
if (!string.IsNullOrEmpty(result.DisplayName))
{
Console.WriteLine(result.DisplayName);
}
}
Console.ReadLine();
}
When I run this I get back a list of objects. But what I get back is only a partial list. Some users are there, some are not.
Anyone see what I'm doing wrong?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Are you only getting back the first 256 results?
c# - Can I get more than 1000 records from a PrincipalSearcher? - Stack Overflow[^]
PrincipalSearcher search = new PrincipalSearcher(u);
DirectorySearcher ds = search.GetUnderlyingSearcher() as DirectorySearcher;
if (ds != null) ds.PageSize = 0;
Also, you should really be wrapping the disposable objects in using blocks.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I'm trying to use only Alphanumeric characters an must use 4. No more or less.
The Code I'm using is below. If I remove the For Loop and can type Alphanumeric values in the textbox but I can seem to type only 4 characters. maxLenght says: cannot convert type in to bool. (Must be 4 characters). I appreciate your help.
public void AlphaNumericString()
{
string s = BacBox.Text;
int maxLenght = 4;
Regex r = new Regex("^[a-zA-Z0-9]*$");
if (r.IsMatch(s))
{
for (int i = 0; i = maxLenght; i++)
{
BacBox.Text = s;
}
}
else
{
MessageBox.Show("Enter 4 alphanumeric characters only");
}
|
|
|
|
|
The * in that RegEx means "any number of, including 0". It doesn't mean "exactly 4".
To get an exact 4 characters, you would have to use a RegEx pattern of "^[a-zA-Z0-9]{4}$".
You don't need that for loop at all.
As for the rest, I have no idea what this code is supposed to be doing. It's not named properly and doesn't return a value soooooo....
I would probably rename the method
public bool IsValidId(string text)
and have it return a true/false value that indicates if the passed in value is a valid Id number, according to your new RegEx pattern.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
Don't repost the same question over and over again. It just duplicates work and doesn't allow for a collaborated answer.
System.ItDidntWorkException: Something didn't work as expected.
C# - How to debug code[ ^].
Seriously, go read these articles.
Dave Kreskowiak
|
|
|
|
|
A "TextBox" usually has a property to limit input length; and "may" have a mask for alphanumeric.
Depends on Windows Forms, WPF, 3rd party.
Easier than doing it yourself.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I posted this and got no replies, so I thought I'd bump it and see if anyone can contribute
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|