Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am new to servicestack and using servicestack version 4.5.0.
With reference to the ServiceStack 'session' missing?
Where can I find this base.SessionBag property ? with base keyword I am getting
these intellisense
please help me.
what does this line means:
Inside a Service the dynamic untyped Session Bag was renamed to base.SessionBag.
what does this line means:
Inside a Service the dynamic untyped Session Bag was renamed to base.SessionBag.
when I write base. I find following intellisense
base.Session //-- Property Method
base.SaveSession //--- Extension
base.SessionAs<>
base.SessionFactory //-- Property Method
public class EntryService : Service
{
public object Post(Entry request)
{
var date = request.Time.Date;
var trackedData = (TrackedData)Session= date.ToString();
if(trackedData == null)
trackedData = new TrackedData { Goal = 300 };
trackedData.Total += request.Amount;
Session[date.ToString()] = trackedData;
return new EntryResponse { Id = 1};
}
}
i want like this
Session[date.ToString()] = trackedData;
but an error occurs Cannot apply indexing with [] to an expression of type ServiceStack.CacheAccess.ISession
You have to inherit from ServiceStack's Service base class. You can then access the dynamic Session Bag with base.SessionBag.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
As part of a test, I wish to mock a Get() method using Moq such that it returns a popped value from a Stack every time it’s called.
Unfortunately, Moq’s Setup() methods are only run once and thus, each Get() within my test returns the same top value from the stack on every call.
My test kicks off a process in which multiple Get() methods are called. How would I mock this Get() method such that it pops a new value every time of off a orderedGetOutputs stack?
How about this:
public interface ISomeInterface
{
public string SomeValue { get; set; }
}
[Fact]
public void PropertyReturnsDifferentValueOnEachCall()
{
var stack = new Stack<string>();
stack.Push("World");
stack.Push("Hello");
var mock = new Mock<ISomeInterface>();
mock.SetupGet(s => s.SomeValue).Returns(() => stack.Pop());
// Because the method signature of stack.Pop() matches
// the expectation of Returns(), you could also write
// mock.SetupGet(s => s.SomeValue).Returns(stack.Pop);
var instance = mock.Object;
var resultOne = instance.SomeValue;
var resultTwo = instance.SomeValue;
Assert.NotEqual(resultOne, resultTwo);
}
When mocking a normal method you use .Setup(), but when you mock a property getter you have to use .SetupGet(). Regardless of what you mock, the .Returns() overload takes also a matching Func<> that can do whatever needed.
Moq has a specific function called “SetupSequence”. This will allow you to chain setup and return calls. Either that or you can pre populate the stack with information you’ll need for each call.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Is there any way to change a dynamic type property value ?
Code Example:
dynamic result = new { Status = "WARNING", Modified = false, Message = string.Empty };
result.Status = "OK";
Is that possible at all using C# ?
Thanks
Dynamic data type is added in c# 4.0. It is used to avoid the compile-time type checking. The compiler does not check the type of the dynamic type variable at compile time, instead of this, the compiler gets the type at the run time.
Though, in most cases, dynamic data type behaves like an object.
In the abovementioned example:
You've created an anonymous type variable which is like the class type but it can only include public read-only members so you can't change result.Status value in this case.
TO SOLVE THE PROBLEM :
I think you should use generic collection like dictionary
The code may look like this:
var result = new Dictionary<string, object>() { {"Status", "Warning"}, {"Modified", false}, {"Message", string.empty} };
result ["Status"] = "Ok";
No this is not possible. You will get a runtime error telling you that the property Status of the anonymous type is write protected or read-only.
For future use here is the online compiler proof:
The documentation says:
Anonymous types contain one or more public read-only properties.
You would need to recreate the object again using the values from the original one for the rest of the properties:
var result = new { Status = "WARNING", Modified = false, Message = string.Empty };
result = new { Status = "OK", Modified = result.Modified, Message = result.Message };
Console.WriteLine(result.Status);
Output:
OK
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to do a function such that when the user selects the class name and clicks the button, it create a new List<SelectedClass>. Currently I am using a switch statement like so:
switch(Name)
case"a" : List<a> X=new List<a>();
case"b" : List<b> X=new List<b>();
Is there another method to do this?
Try it with Activator.CreateInstance (so you won't need any switch statement):
Type type = Type.GetType(Name); // Example: Type.GetType("NamespaceName.ClassName")
Type listType = typeof(List<>);
var listWithType = listType.MakeGenericType(new [] { type });
var instanceOfList = Activator.CreateInstance(listWithType);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to update EF record, in method I have EF object and another new objct which I want to use to update data from. But I m not sure how to copy data from new object to existing one.
Help please.
Here is my code:
public int PostHomeLead(string _lead)
{
try
{
int result = 0;
Lead lead = new Lead();
lead = new JavaScriptSerializer().Deserialize<Lead>(_lead);
//check if lead exist with same session id, if so update it other wise add new.
Lead existingLead = new Lead();
existingLead = db2.HomeLoanCustRepo.GetByID(lead.Lead_id);
if (existingLead == null)
{
db2.HomeLoanCustRepo.Insert(lead);
db2.Save();
result = 1;
}
else
{
db2.HomeLoanCustRepo.Update(lead);
db2.Save();
result = 1;
}
return result;
}
catch(Exception ex)
{
throw ex;
}
}
Either map the properties manually:
existingLead.Foo = deserializedLead.Foo;
existingLead.Bar = deserializedLead.Bar;
existingLead.Baz = deserializedLead.Baz;
Or use a library that does this, like AutoMapper.
As for your comment, creating a deep copy is what you seem to be after. Note this allows for overposting or mass assignment when you don't verify which properties may be updated. You'll need to Attach() the cloned object when using cloning or mapping, as it will not be the same object as returned by GetByID(), so Entity Framework's change tracker won't recognize it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have an object named 'Account' and the following code:
Account objAcc = new Account();
objAcc.Name = "Test";
The above is working fine, as expected. I now have a list of values as follows:
string props = "Name,Address,Telephone";
Now, what I want to do is see if "Name" exists in that list. I only have the object to use though (hard coding a case statement etc isn't possible as the object is dynamic), so from objAcc.Name, I somehow need to get "Name" from that, and then see if it's in the list.
Thanks in advance, I hope it's clear enough,
Dave
You can use reflection, by doing that :
var properties = objAcc.GetType().GetProperties();
foreach(var property in properties)
{
if(props.Contains(property.Name))
{
//Do you stuff
}
}
string test = objAcc.GetType().GetProperty("Name") == null ? "" : objAcc.GetType().GetProperty("Name").Name;
bool result = "Name,Address,Telephone".Split(',').Contains(test);
You may use the following method if you like:
public bool PropertyExists<T>(string propertyName, IEnumerable<string> propertyList,T obj)
{
string test = obj.GetType().GetProperty(propertyName) == null ? "" : obj.GetType().GetProperty(propertyName).Name;
bool result = propertyList.Contains(test);
return result;
}
Giannis