Bit (sorry) confused about how to use bitwise operators with Entity Framework.
A recent requirement has necessetated that we alter an enum from a series of sequential integers into a bitwise enum. So:
[Flags]
public enum AdminLevel
{
None = 0,
Basic = 1,
Partial = 2,
Full = 4
}
We originally stored this as an int column in the database. It's still an int, of course, but I'm having trouble seeing how I can perform selections based on more than one possible enum value. For example this code:
public string GetAdminEmails(AdminLevel adminlevel)
{
using (IRepository r = Rep.Renew())
{
string[] to = r.GetUsers().Where(u => u.AdminLevel >= (int)adminlevel).Select(u => u.Email).ToArray();
return string.Join(",", to);
}
}
Would return all the admin levels including and above the one supplied. If I want more than one admin level, I now have to call it like this:
GetAdminEmails(AdminLevel.Partial | AdminLevel.Full);
But obviously I can't convert that to an int and use greater than any more. Is there a neater way of handling this change than a series of flow control statements?
You can use the HasFlag method:
AdminLevel myFlags = AdminLevel.Partial | AdminLevel.Full;
string s = GetAdminEmails(myFlags);
public string GetAdminEmails(AdminLevel myFlags)
{
using (IRepository r = Rep.Renew())
{
string[] to = r.GetUsers().Where(u => u.AdminLevel.HasFlag(myFlags))
.Select(u => u.Email).ToArray();
return string.Join(",", to);
}
}
You can define an int column in your database as an enumeration in your model:
public enum MyEnum : int { First = 0, Second = 1}
public partial class MyModel
{
public MyEnum Type {get; set;}
}
Throughout your application you can just use MyEnum and EF will work with int on the background.
I'm trying to map an entity to a enum. As I was searching for a source I found this:
using Should;
public enum OrderStatus : short
{
InProgress = 0,
Complete = 1
}
public enum OrderStatusDto
{
InProgress = 0,
Complete = 1
}
[Test]
public void Example()
{
Mapper.Map<OrderStatus, OrderStatusDto>(OrderStatus.InProgress)
.ShouldEqual(OrderStatusDto.InProgress);
Mapper.Map<OrderStatus, short>(OrderStatus.Complete).ShouldEqual((short)1);
Mapper.Map<OrderStatus, string>(OrderStatus.Complete).ShouldEqual("Complete");
Mapper.Map<short, OrderStatus>(1).ShouldEqual(OrderStatus.Complete);
Mapper.Map<string, OrderStatus>("Complete").ShouldEqual(OrderStatus.Complete);
}
but I think this works for only enum-to-enum mapping. because when I try to use .ShouldEqual, intellisense can't find it. In that codeblock, there is a reference that's called Should but I couldn't find its reference anywhere.
Any ideas about how to use automapper to map between enum and entity/class?
Any ideas about using Should?
#I updated the question because without seeing the actual code, it's harder to consider a solution. Here is the code snippet that might be needed:
public class ParameterEnum
{
/// <summary>
/// Enum Sayisi: 2650, Son Guncelleme Tarihi: 21.2.2013 09:40:37
/// </summary>
public enum Parameters : int
{
...
IsEmriTuruIsTalebi = 138,
<summary>
Adi: Kalite Öneri; ID: 2218; Seviyesi: 3; Aciklamasi: ; Aktif Mi: True
</summary>
...}}
and this is where normal mapping done:
isEmriEntity.IsEmriTuruId = (int)ParameterEnum.Parameters.IsEmriTuruIsTalebi;
You should look into ITypeConverter. Something like this should do the job:
Mapper.CreateMap<OrderStatus, OrderStatusDto>().ConvertUsing(new OrderStatusConverter());
and your converter would look like so:
public class OrderStatusConverter: ITypeConverter<OrderStatus, OrderStatusDto>
{
public OrderStatusDto Convert(OrderStatus source)
{
return (OrderStatusDto)source;
}
}
That should be enough to apply the same approach to any other cross-type mappings in your DTOs.
EDIT:
On your enum conversion error, using this as an example for clarity (an enum is not a DTO):
public enum ExampleEnum : short
{
SomeValue,
SomeOtherValue,
BigValue = 100,
}
public enum AnotherEnum
{
Foo,
Bar,
}
This should make the enum conversion clearer (don't cast to int at all).
private void Test()
{
// Casting to int only works when the value is 0
// This works (SomeValue = 0)
AnotherEnum example = (int) ExampleEnum.SomeValue;
// This won't even compile (SomeOtherValue = 1)
AnotherEnum example2 = (int) ExampleEnum.SomeOtherValue;
// Casting to another enum works fine
AnotherEnum example2 = (AnotherEnum) ExampleEnum.SomeOtherValue;
// Just be careful of values that don't exist in the target enum
// This will compile even though it won't work at run-time (BigValue = 100)
AnotherEnum example2 = (AnotherEnum) ExampleEnum.BigValue;
}
Is it possible to do somethink like
public class TestClass
{
public List<double> preTvoltage
{
get
{
return preTvoltage;
}
set
{
preTvoltage.Add(this); //how to add to the List??
}
}
}
The reason I want to do this (I do not know if this is a best method, just as far as my knowledge allows) because I have to get data from xml files that do not have always same number of data in them.
Later I want to fill a ListView rows and using list I can count how many items are and how many columns will be needed.
Here is a schematic of xml file:
and there are also Trigger and PostTrigger nodes in xml file with same data sorting.
and here is the listview I want to achive:
Link to full size image
So, there are some pin groups and each pingroup has lots of data, the above code I gave, was just to hold 1 of the voltage nodes in xml file.
I am pretty much listening for your ideas!
Thanks.
No, and it defies usage of properties - you should implement it as an Add (or similarly aptly named) method.
You can't add this, because this is a TestClass, not a double; and you can't add value, as otherwise suggested, because that is a List<double>, and Add requires a double.
It's not clear how you would use this, but it looks like a very bad idea to me. Setting a collection as a property is slightly unusual already, but it's even odder for that set operation to mutate the list. It's additionally weird that you're not using the value variable within the setter... why not?
You should consider what the calling code would look like, and whether that's really the clearest way of expressing the semantics you want.
set { preTvoltage.AddRange(value); }
As Jon Skeet is saying, this is not what you should do. Instead, do
TestClass t = new TestClass();
t.PreTvoltage.Add(...);
declaring the property as
public List<double> PreTvoltage
{
get { return preTvoltage; }
}
The type of a getter and setter must match.
You could have:
public List<double> preTvoltage
{
get
{
return preTvoltage;
}
set
{
preTvoltage.AddRange(value); //add all items in list assigned.
}
}
However, this seems like a bad idea as it would be confusing to users why the value got did not match the value just set. I would have the two operations as separate members, and the setter either not exist or else overwrite the existing preTvoltage entirely.
You can not implement it like this, the preferable way is to make collection controls like:
private IList<double> _preTvoltage = new List<double>();
public IEnumerable<double> preTvoltage
{
get
{
return preTvoltage.AsEnumerable();
}
}
public void AddTvoltage(double item)
{
_preTvoltage.Add(item);
}
Well I managed to solve my problem this way:
public class ITestData
{
public string pinName { get; set; } //Name of the pin
public double stressLevel { get; set; } //Stress level for latchup
public int psuCount { get; set;} //Number of PSU's
public List<double[]> preTrigger = new List<double[]>();
public List<double[]> inTrigger = new List<double[]>();
public List<double[]> postTrigger = new List<double[]>();
public void AddPreTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
preTrigger.Add(data);
}
public void AddInTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
inTrigger.Add(data);
}
public void AddPostTrigger(double volt, double curr)
{
double[] data = new double[2];
data[0] = volt;
data[1] = curr;
postTrigger.Add(data);
}
}
Ok, so here's an enum, right?
public enum BrandSafe : short
{
Yes = 1,
No = -1,
Unknown = 0
}
Underlying datatype of short, OK, so far so good.
Here is a table:
Now, here is a DTO class:
public class VurlRow
{
public long VurlRMXID { get; set; }
public string VurlString { get; set; }
public Enums.BrandSafe BrandSafe { get; set; }
}
Finally, here is a linq method:
List<VurlRow> vurls = (from vurl in m_Context.Vurls
select new VurlRow()
{
BrandSafe = (Enums.BrandSafe)vurl.BrandSafe,
VurlRMXID = vurl.VurlRMXID,
VurlString = vurl.VurlString
}).ToList();
I've also tried (Enums.BrandSafe)Enum.ToObject(typeof(Enums.BrandSafe), vurl.BrandSafe) to produce the Enum. When I remove the line BrandSafe = (Enums.BrandSafe)vurl.BrandSafe, the call works, but with the line I get a InvalidCast exception.
"Specified cast is not valid."
Seems like it should be totally valid to me, but what do I know, not enough about enums and linq apparently, can anyone here help?
BrandSafe is tinyint in the database; tinyint maps to byte, not short. That's the problem. Make it:
public enum BrandSafe : byte
{
Yes = 1,
No = -1, // <====== see below
Unknown = 0
}
(short would map to smallint)
However!!!! Note that -1 is not really a legal value for either of byte/tinyint - it will be 255 or an OverflowException in .NET (depending on whether it is a checked or unchecked context), and an arithmetic-overflow (error 220) at the database.
I do, however, wonder whether bool? (in C#) and bit null (TSQL) would be a better match.
EDIT 1: Forgot to add the nested property curve ball.
UPDATE: I have chosen #mtazva's answer as that was the preferred solution for my specific case. In retrospect, I asked a general question with a very specific example and I believe that ended up confusing everyone (or maybe just me) as to what the question was exactly. I do believe the general question has been answered as well (see the Strategy pattern answers and links). Thanks everyone!
Large switch statements obviously smell and I have seen some links on how you could do this with a dictionary that maps to functions. But I'm wondering if there is a better (or smarter way) to do this? In a way, this is a question I've always sort of had rolling around in the back of my head but never really had a good solution to.
This question stemmed from another question I asked earlier: How to select all the values of an object's property on a list of typed objects in .Net with C#
Here is an example class I'm working with (from an external source):
public class NestedGameInfoObject
{
public string NestedName { get; set; }
public int NestedIntValue { get; set; }
public decimal NestedDecimalValue { get; set; }
}
public class GameInfo
{
public int UserId { get; set; }
public int MatchesWon { get; set; }
public long BulletsFired { get; set; }
public string LastLevelVisited { get; set; }
public NestedGameInfoObject SuperCoolNestedGameInfo { get; set; }
// thousands more of these
}
Unfortunately, this is coming from an external source... imagine a HUGE data dump from Grand Theft Auto or something.
And I want to get just a small cross section of a list of these objects. Imagine we want to be able to compare you with a bunch of your friends' game info objects. An individual result for one user would look like this:
public class MyResult
{
public int UserId { get; set; } // user id from above object
public string ResultValue { get; set; } // one of the value fields from above with .ToString() executed on it
}
And an example of what I want to replace with something more manageable (believe me, I DON'T want to be maintaining this monster switch statement):
const int MATCHES_WON = 1;
const int BULLETS_FIRED = 2;
const int NESTED_INT = 3;
public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
var output = new List<MyResult>();
switch(input)
{
case MATCHES_WON:
output = gameInfos.Select(x => new MyResult()
{
UserId = x.UserId,
ResultValue = x.MatchesWon.ToString()
}).ToList<MyResult>();
break;
case BULLETS_FIRED:
output = gameInfos.Select(x => new MyResult()
{
UserId = x.UserId,
ResultValue = x.BulletsFired.ToString()
}).ToList<MyResult>();
break;
case NESTED_INT:
output = gameInfos.Select(x => new MyResult()
{
UserId = x.UserId,
ResultValue = x.SuperCoolNestedGameInfo.NestedIntValue.ToString()
}).ToList<MyResult>();
break;
// ad nauseum
}
return output;
}
So the question is are there any reasonable ways to manage this beast? What I'd really like is a dynamic way to get this info in case that initial object changes (more game info properties are added, for instance). Is there a better way to architect this so it's less clumsy?
I think your first sentence eluded to what is probably the most reasonable solution: some form of dictionary mapping values to methods.
For example, you could define a static Dictionary<int, func<GameInfo, string>>, where each value such as MATCHES_WON would be added with a corresponding lambda that extracts the appropriate value (assuming your constants, etc are defined as shown in your example):
private static Dictionary<int, Func<GameInfo, string>> valueExtractors =
new Dictionary<int, Func<GameInfo, string>>() {
{MATCHES_WON, gi => gi.MatchesWon.ToString()},
{BULLETS_FIRED, gi => gi.BulletsFired.ToString()},
//.... etc for all value extractions
};
You can then use this dictionary to extract the value in your sample method:
public static List<MyResult> GetMyResult(GameInfo[] gameInfos, int input)
{
return gameInfo.Select(gi => new MyResult()
{
UserId = gi.UserId,
ResultValue = valueExtractors[input](gi)
}).ToList<MyResult>();
}
Outside of this option, you could potentially have some sort of file/database/stored lookup with the number and the property name, then use reflection to extract the value, but that would obviously not perform as well.
I think this code is getting out of hand a bit. You're effectively using constants to index properties - and this is creating fragile code that you're looking to use some technique - such as - reflection, dictionaries, etc - to control the increased complexity.
Effectively the approach that you're using now will end up with code like this:
var results = GetMyResult(gameInfos, BULLETS_FIRED);
The alternative is to define an extension method that lets you do this:
var results = gameInfos.ToMyResults(gi => gi.BulletsFired);
This is strongly-typed, it doesn't require constants, switch statements, reflection, or anything arcane.
Just write these extension methods and you're done:
public static class GameInfoEx
{
public static IEnumerable<MyResult> ToMyResults(
this IEnumerable<GameInfo> gameInfos,
Func<GameInfo, object> selector)
{
return gameInfos.Select(gi => gi.ToMyResult(selector));
}
public static MyResult ToMyResult(
this GameInfo gameInfo,
Func<GameInfo, object> selector)
{
return new MyResult()
{
UserId = gameInfo.UserId,
ResultValue = selector(gameInfo).ToString()
};
}
}
Does that work for you?
You can use reflection for theses purposes. You can implement custom attributes, mark your properties, etc. Also, it is dynamic way to get info about your class if it changes.
If you want to manage switch code I would point you at Design Patterns book (GoF) and suggest possibly looking at patterns like Strategy and possibly Factory (thats when we talk about general case use, your case isn't very suited for Factory) and implementing them.
While switch statement still has to be left somewhere after refactoring to pattern is complete (for example, in a place where you select strategy by id), code will be much more maintanable and clear.
That said about general switch maintenance, if they become beast like, I am not sure its best solution given how similar your case statements look.
I am 100% sure you can create some method (possibly an extension method) that will be accepting desired property accessor lambda, that should be used when results are generated.
If you want your code to be more generic, I agree with the suggestion of a dictionary or some kind of lookup pattern.
You could store functions in the dictionary, but they seemly all perform the same operation - getting the value from a property. This is ripe for reflection.
I'd store all your properties in a dictionary with an enum (prefer an enum to a const) as the key, and a PropertyInfo - or, less preferred, a string which describes the name of the property - as the value. You then call the GetValue() method on the PropertyInfo object to retrieve the value from the object / class.
Here's an example where I'm mapping enum values to their 'same named' properties in a class, and then using reflection to retrieve the values out of a class.
public enum Properties
{
A,
B
}
public class Test
{
public string A { get; set; }
public int B { get; set; }
}
static void Main()
{
var test = new Test() { A = "A value", B = 100 };
var lookup = new Dictionary<Properties, System.Reflection.PropertyInfo>();
var properties = typeof(Test).GetProperties().ToList();
foreach (var property in properties)
{
Properties propertyKey;
if (Enum.TryParse(property.Name, out propertyKey))
{
lookup.Add(propertyKey, property);
}
}
Console.WriteLine("A is " + lookup[Properties.A].GetValue(test, null));
Console.WriteLine("B is " + lookup[Properties.B].GetValue(test, null));
}
You can map your const values to the names of the properties, PropertyInfo objects which relate to those properties, functions which will retrieve the property values... whatever you think suits your needs.
Of course you will need some mapping - somewhere along the way you will be depending on your input value (the const) mapping to a specific property. The method by which you can get this data might determine the best mapping structure and pattern for you.
I think the way to go is indeed some kind of mapping from one value (int) to something that is somehow a function that knows how to extract a value.
If you really want to keep it extensible, so that you can easily add some without touching the code, and possibly accessing more complex properties (ie. nested properties, do some basic computation), you may want to keep that in a separate source.
I think one way to do this is to rely on the Scripting Services, for instance evaluating a simple IronPython expression to extract a value...
For instance in a file you could store something like :
<GameStats>
<GameStat name="MatchesWon" id="1">
<Expression>
currentGameInfo.BulletsFired.ToString()
</Expression>
</GameStat>
<GameStat name="FancyStat" id="2">
<Expression>
currentGameInfo.SuperCoolNestedGameInfo.NestedIntValue.ToString()
</Expression>
</GameStat>
</GameStats>
and then, depending on the requested stat, you always end up retrieving the general GameInfos. You can them have some kind of foreach loop with :
foreach( var gameInfo in gameInfos){
var currentGameInfo = gameInfo
//evaluate the expression for this currentGameInfo
return yield resultOfEvaluation
}
See http://www.voidspace.org.uk/ironpython/dlr_hosting.shtml for examples on how to embed IronPython Scripting in a .NET application.
NOTE: when working with this kind of stuff, there are several things you must really be careful about:
this potentially allows someone to inject code in your application ...
you should measure the performance impact of Dynamic evaluation in here
I don't have a solution to your switch problem off the top of my head, but you could certainly reduce the code by using a class that can automatically map all the fields you need. Check out http://automapper.org/.
I would not have written the GetMyResult method in the first place. All it is doing is transforming GameInfo sequence into MyResult sequence. Doing it with Linq would be easier and more expressive.
Instead of calling
var myResultSequence = GetMyResult(gameInfo, MatchesWon);
I would simply call
var myResultSequence = gameInfo.Select(x => new MyResult() {
UserId = x.UserId,
ResultValue = x.MatchesWon.ToString()
});
To make it more succinct you can pass the UserId and ResultValue in constructor
var myResultSequence =
gameInfo.Select(x => new MyResult(x.UserId, x.MatchesWon.ToString()));
Refactor only if you see the selects getting duplicated too much.
This is one possible way without using reflection:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
public class GameInfo
{
public int UserId { get; set; }
public int MatchesWon { get; set; }
public long BulletsFired { get; set; }
public string LastLevelVisited { get; set; }
// thousands more of these
}
public class MyResult
{
public int UserId { get; set; } // user id from above object
public string ResultValue { get; set; } // one of the value fields from above with .ToString() executed on it
}
public enum DataType
{
MatchesWon = 1,
BulletsFired = 2,
// add more as needed
}
class Program
{
private static Dictionary<DataType, Func<GameInfo, object>> getDataFuncs
= new Dictionary<DataType, Func<GameInfo, object>>
{
{ DataType.MatchesWon, info => info.MatchesWon },
{ DataType.BulletsFired, info => info.BulletsFired },
// add more as needed
};
public static IEnumerable<MyResult> GetMyResult(GameInfo[] gameInfos, DataType input)
{
var getDataFunc = getDataFuncs[input];
return gameInfos.Select(info => new MyResult()
{
UserId = info.UserId,
ResultValue = getDataFunc(info).ToString()
});
}
static void Main(string[] args)
{
var testData = new GameInfo[] {
new GameInfo { UserId="a", BulletsFired = 99, MatchesWon = 2 },
new GameInfo { UserId="b", BulletsFired = 0, MatchesWon = 0 },
};
// you can now easily select whatever data you need, in a type-safe manner
var dataToGet = DataType.MatchesWon;
var results = GetMyResult(testData, dataToGet);
}
}
}
Purely on the question of large switch statements, it is notable that there are 2 variants of the Cyclomatic Complexity metric in common use. The "original" counts each case statement as a branch and so it increments the complexity metric by 1 - which results in a very high value caused by many switches. The "variant" counts the switch statement as a single branch - this is effectively considering it as a sequence of non-branching statements, which is more in keeping with the "understandability" goal of controlling complexity.