I'm using DataSet to connect my C# program with SQL database. I want one of the columns to be an enumeration and I want it to act as an enum in my code. How can I do this?
I don't mean to burst everyone's bubble, but you can easily map an integer to an Enum using a Strongly Typed DataSet. I do it all the time. Rather than type the whole thing out here I have created an entry on my Blog describing in detail how to accomplish this.
I don't think you can. SQL Server doesn't have a concept of enums.
I would suggest using an ApplicationService layer that sits atop your repository layer. Then in your ApplicationService class (think of appropriate name for this class) you can transform the data that is returned from the repository layer to the appropriate enum value in your POCO object.
You might try using a strongly typed dataset.
Technically you can't. Enum's are static typed, they are designed to be used when you know all of the values at compile time. While there are some work-arounds, I would highly recommend that you do not do this.
Look at a unmodifiable dataset, this will give most of the benefits of a Enum and can be created on the fly.
While data cannot be stored as an Enum, they can be used as a fancy filter as long as the data in the database column is of an integer type. That is a complete different question if that is what you are after. Go to the MSDN page and read up on Enum's.
Related
I'm writing a parser for Articy Draft, a writing tool which exports an entire project as an XML document. Most of it is fairly straightforward, but the way it handles entities is a little tricky.
Essentially, each entity has a collection of features, each of which is a collection of properties. Each property can be either an int, a string or an enumeration and has a name (string) and an ID (long).
I'm trying to figure out the best way of storing this data in each entity. I'd like it to be generic, rather than have predefined entities, as that is more future-proof. I guess I'm too inexperienced with C# to know of a correct way to store it. I only really need to index based on the name of the data field. Being able to index on the ID as well would be a bonus, but not at all necessary.
I could have three dictionaries, one each for int, enumeration and string. Index each one based on either the name or the index, whichever I prefer. It seems a bit messy having three different collections, but maybe that's the best way.
I could use DataColumns to create a DataTable in each entity with columns for ID, name, datatype and value, but then you'd need three value columns (or two, I guess since enumeration could be stored as an int) and that's a bit messy too.
It's a problem I can easily solve with my current knowledge, but it just feels like there's a more elegant solution than I'm aware of and there is the opportunity for me to expand my C# knowledge a little bit here.
I recommend doing some reading into the C# ExpandoObject which I believe will provide a good solution for you
C# ExpandoObject
I need to have a list(array, ect) of objects that have a static values. What is the best way (more usefull) to organize such data structure on c#?
Now, I am able to do it using two ways:
1) Enumarations with an additional parameters:
http://www.codeproject.com/KB/cs/stringenum.aspx
2) Create some class with needed fields and then create an array of different classes instances.
Please write what of methods is the best with comments why or provide other ideas how to do it
I think the data structure you adopt will be largely dependant on how you intend to use the static values.
If you are going to be doing a lot of comparisons, I would suggest an enumeration with custom attributes (as described in your link, but possibly taken further) to provide additional metadata. However, if the structure containing the static values is going to operate more like a series of choices (e.g. the Encoding class in System.Text) then a static class with fields marked as readonly would be a better option.
Tuples for objects with static values
Sounds like a classic example of a Dictionary, a simple Key-Value relationship
http://dotnetperls.com/dictionary-keys
http://en.wikipedia.org/wiki/Associative_array
i'd like to be able to query any number of databases with different table layouts, return a datatable then use that datatable to build a strongly typed object. Is there anything out there that does this or comes close without having to code for each different table layout?
Thank you in advance!
Zac
You could try subsonic.
It does exactly that, using code generation via T4 templates.
Get more information at http://subsonicproject.com/
Cheers,
André
May be you can use LINQ and return Anonymous Types http://msdn.microsoft.com/en-us/library/bb397696.aspx
eg:
var result = (from itm in list where itm.StateID==2 select new {Name = itm.Name, State=Itm.StateID});
I'm not sure this is exactly what you're looking for, but the datatable base type has a method called GetTypedTableSchema() that returns an XmlSchemaSet object. You may be able to use this as a roadmap to translate a typed datatable into a strongly typed object.
Assuming you're trying to get Intellisense benefits - go the code generation route against your tables.
No. This is not possible.
In case of typed-datasets, If you need typed-safety at run-time, the type needs to be defined at design-time or you are going to use plain datasets/datatables for your database operations.
Small design question here. I'm trying to develop a calculation app in C#. I have a class, let's call it InputRecord, which holds 100s of fields (multi dimensional arrays) This InputRecordclass will be used in a number of CalculationEngines. Each CalculcationEngine can make changes to a number of fields in the InputRecord. These changes are steps needed for it's calculation.
Now I don't want the local changes made to the InputRecord to be used in other CalculcationEngine's classes.
The first solution that comes to mind is using a struct: these are value types. However I'd like to use inheritance: each CalculationEngine needs a few fields only relevant to that engine: it's has it's own InputRecord, based on BaseInputRecord.
Can anyone point me to a design that will help me accomplish this?
If you really have a lot of data, using structs or common cloning techniques may not be very space-efficient (e.g. it would use much memory).
Sounds like a design where you need to have a "master store" and a "diff store", just analogous to a RDBMS you have data files and transactions.
Basically, you need to keep a list of the changes performed per calculation engine, and use the master values for items which aren't affected by any changes.
The elegant solution would be to not change the inputrecord. That would allow sharing (and parallel processing).
If that is not an option you will have to Clone the data. Give each derived class a constructor that takes the base Input as a parameter.
You can declare a Clone() method on your BaseInputRecord, then pass a copy to each CalculationEngine.
So I am refactoring a little application as an example to get more practice. The purpose of the application (let's say) is to collect the data from a "sign up new user" form, save it in the database. The only limitation I have is I have to use a special custom Data Access class which communicates directly with the database and returns the data (if applicable) in a DataTable object.
I have a question regarding a little details on a form and how do they fit in into the layer architecture. For example, my form has a drop down list that's fed from the database, but at the same time drop down list doesn't represent an object per SE (unlike a User that is a object, there is a class User that has multiple methods, data members etc). I don't want to have calls to the stored procedure right there in the code behind but I also do not wish to overdo on abstraction.
What would be an elegant way to take care of these little details w/o creating a class abstraction galore.
Hope I am being clear
Funny you should ask that. I went through that issue here.
These other Stack Overflow Questions that I've answered that show other parts (tangentially related):
Getting ListView Data Items from Objects
Working with ListViews
Concatenating Properties in a DropDownList
An option for getting non-object data to the UI is to create one or more lookup classes that are a bucket or "service" for getting odd bits of data for things like drop down lists etc...
Example:
myDDL.DataSource = Lookup.GetAllCountries(); // GetAllCountries is a static method
// set name/value fields etc...
myDDL.DataBind();
Using this methodology, you can still support tier separation. It's not object oriented or elegant, but it is very practical.
I don't know what's best practice, but what I do is I have a utility class that has a method that takes as arguments a DropDownList object and an enum, so I do
FillDropDown( ddlistPhoneType, DropDownTypes.PhoneTypes );
The utility class fills the dropdowns sometimes from the database, other times from XML, and occasionally some hardcoded values. But at least the GUI doesn't have to worry about that.