Best approach to hold different classes as a property - c#

I probably don't know the correct terminology to look this up on google so I'm asking here.
Say I want to have a front end control to run different SQL queries in Winforms that allow entering the parameters which will be generated at runtime.
public class AnalysisQuery
{
public string QueryName { get; set; }
public string QueryDescription { get; set; }
public string QuerySQL { get; set; }
public T QueryParameters { get; set; } // <-- Not sure how to do this
}
and say I have 2 entirely different classes that hold the parameters for a particular query
public class EmployeeQueryParameters
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Occupation { get; set; }
public DateTime StartingDate { get; set; }
public bool IsMarried { get; set; }
}
public class CarQueryParameters
{
public string CarName { get; set; }
public string CarModel { get; set; }
public string CarMaker { get; set; }
public bool IsDiesel { get; set; }
}
How do I hold these different classes in the property QueryParameters?
What is the best way to do this?
Ultimately I need to use either EmployeeQueryParameters or CarQueryParameters for a datasource, eg
someControl.DataSource = new EmployeeQueryParameters()
{
FirstName = "",
LastName = "",
Occupation = "",
StartingDate = new DateTime(2018, 10, 18),
IsMarried = true
};
What I have tried so far....
1)I've looked into interfaces but this looks like it will only work if all properties are the same in each EmployeeQueryParameters and CarQueryParameters class.
2) This link shows an example to hold different types as list of parameters. It kind of works but ultimately the type must still be known at the end to retrieve the correct type, eg
public class AnalysisQuery
{
public string QueryName { get; set; }
public string QueryDescription { get; set; }
public string QuerySQL { get; set; }
public Parameter QueryParameters { get; set; }
public AnalysisQuery()
{
QueryName = "QueryName1";
QueryDescription = "QueryDescription1";
QuerySQL = "QuerySQL1";
QueryParameters = Parameter.Create<EmployeeQueryParameters>(
new EmployeeQueryParameters() { FirstName = "first name" });
}
}
still requires the type to be known to get the value so kind of defeats the object of using a generic parameter?
var analysisQuery = new AnalysisQuery();
EmployeeQueryParameters parameters =
analysisQuery.QueryParameters.Get<EmployeeQueryParameters>();

The answer depends a lot on your UI. Do you have a form that uses AnalysisQuery as a model in which you dynamically add new key value pairs to define parameters for the query? Then I recommend using a Dictionary for QueryParameters. Do you use only a limited number of predefined types of queries, then I recommend using AnalysisQuery as your model, which then you switch based on a dropdown or radiobutton list, etc. Even more, if your UI doesn't know (or shouldn't know) the list of predefined types, then you will have to construct the UI based on the actual type and values of the object in QueryParameters, in which case you can just declare it as an object and get the information using reflection.
Bottom line, your question is too vague for a clear answer.

Related

Send a limited/reduced class to frontend

What I want
I want to send a limited/reduced class/object to frontend (as JSON). I use .NET Core 5.
What I have
I have a model class like this:
namespace Tasks.Models
{
public class Resources
{
public Guid Id { get; set; }
public string Type { get; set; }
public string Url { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime? Createdon { get; set; }
public Guid Userid { get; set; }
public Guid Taskid { get; set; }
public int Clicked { get; set; }
public byte Active { get; set; }
+++ many more properties
}
}
Now depending on the which controller that calls this model I want to have different "kind" of models. So if the resource is file I maybe want the properties Id,Type,Name. But if the resource is URL I want Id, Url, Name.
I tried setting up a method that "initialized the fields I wanted, but that also returned all properties
public static Responses FileResponse()
{
var response = new Responses()
{
Id = new Guid(),
Name = "",
Type = "File",
};
return response;
}
Now, when I call the Resources class or this method I get all properties, and returning it to the view presents all properties, but mostly as null, because I only set the three fields in the method.
What is the recommended way of solving this?
If you want to remove the field if it's null instead of showing in json with null value.
public class Resources
{
public Guid Id { get; set; }
public string Type { get; set; }
// if null, dont show it in JSON output
[JsonIgnoreAttribute(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Url { get; set; }
// if null, dont show it in JSON output
[JsonIgnoreAttribute(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Name { get; set; }
public string Description { get; set; }
public DateTime? Createdon { get; set; }
public Guid Userid { get; set; }
public Guid Taskid { get; set; }
public int Clicked { get; set; }
public byte Active { get; set; }
}
PS: Fiddle https://dotnetfiddle.net/hiMAci
It is just limiting the Resource class I am not able to do
Yep, side effect of C# being strongly typed, with object X definitely having properties Y and Z. You need differently shaped objects - either full on classes or records - that name the reduced set of properties because the serializer is going to look a tthe object and ser every property it can find.
You could make a new class for every variation - quick and easy with records, and easy to pass around inside your C#:
public record FileThing(string Id, string Type, string Name);
//make a new one and return it
new FileThing(someResources.Id, someResources.Type, someResources.Name);
Or can consider using an anonymous type if you're literally looking to put a few properties into some json, down a socket to a consuming front end (I can't quite decide what you mean by "view" - it doesn't seem to be an MVC View) that only cares about a few props out of many
So if the resource is file I maybe want the properties Id,Type,Name. But if the resource is URL I want Id, Url, Name.
public ActionResult SomeControllerMethod(){
if(isFile)
return Ok(new { someResources.Id, someResources.Type, someResources.Name });
else if(isUrl)
return Ok(new { someResources.Id, someResources.Url, someResources.Name });
}
Anonymous types are a bit harder to work with because the compiler writes the class for you, so it's tricky to do things like declare return types from methods if the method is returning an AT.. But if you're using it as some fill-in all within one method, such as a "make this and serialize it", they work well..
I think your approach is not the right one here. I tend to follow more general OO guidelines in this situation (note, some consider these a bit dated, and other solutions exist. But they are still commonly used)
You write against an interface. So let's see what you want... A guid, type and name. All other deatils aren't important.
public interface IResourceDetails
{
public Guid Id { get; }
public string Name { get; }
public string Type { get; }
}
And you can have multiple of these interfaces.
You could then implement the interfaces per type. But I would probably combine them in a base class
public abstract class ResourceBase : IResourceDetails
{
public Guid Id { get; } = new ();
public string Name { get; init; }
public string Type { get; }
public ResourceBase(string type)
{
Type = type;
}
}
Each resource type would have it's own implementation
public class FileResource : ResourceBase
{
public FileResource() : base("File") { }
// File-specific properties.
public string Description { get; init; }
public DateTime? Createdon { get; init; }
}
The response method then could be made generic and look like this
public static IActionResult Response(IResourceDetails resource)
{
return Ok(new
{
resource.Id,
resource.Name,
resource.Type,
});
}

How to Map anonymous object in Auto Mapper

Am trying to Map an Anonymous object in auto mapper but am not getting how to do that. Please find my requirement below
Am getting some data from joining 2 tables with only one common column(Id). Am getting Anonymous type data from this query.
var query = (from _vdata in Table1
join entityFind in Table2 on _vdata.id equals entityFind.id
select new { entityFind.FamilyName, entityFind.LastLogin, entityFind.GivenName,
entityFind.Email, entityFind.EmailVerified, entityFind.Uuid, _vdata.Role,
_vdata.Payers, _vdata.Accounts, _vdata.ModifiedOn }).ToList();
Am getting Anonymous data from above query. I have some more list of data in another variable i need to add those data to this list with limited columns having 4-5 columns.
How to do mapping in this situation in AutoMapper or in any other technique
thanks
Since all anonymous types derived from System.Object, I found a solution (workaround) to add mapping from object to your destination type
//Allow to map anonymous types to concrete type
cfg.CreateMap(typeof(object), typeof(ExternalCandle),
MemberList.None);
But please note that for most scenarios this is not the correct solution
For example, if you want to map ORM types - go with this way: Queryable Extensions
I guess that Jimmy bogard won't recommend this solution because of the same reason that CreateMissingTypeMaps was removed from AutoMappers's API -https://github.com/AutoMapper/AutoMapper/issues/3063
So maybe in a future version of AutoMapper this code won't work (I am using AutoMapper 10.1.1 and it worked for me)
You cannot map anonymous type. To achieve the above functionality you can create a Model like below:
public class ResultantData
{
public string FamilyName { get; set; }
public string LastLogin { get; set; }
public string GivenName { get; set; }
public string Email { get; set; }
public string EmailVerified { get; set; }
public string Uuid { get; set; }
public string Role { get; set; }
public string Payers { get; set; }
public string Accounts { get; set; }
public string ModifiedOn { get; set; }
}
Then you can write the above query as below and return the IQueryable of the result:
var query = (from _vdata in Table1
join entityFind in Table2 on _vdata.id equals entityFind.id
select new ResultantData
{
entityFind.FamilyName,
entityFind.LastLogin,
entityFind.GivenName,
entityFind.Email,
entityFind.EmailVerified,
entityFind.Uuid,
_vdata.Role,
_vdata.Payers,
_vdata.Accounts,
_vdata.ModifiedOn
});
When you want to map this result to actual model then you can use ProjectTo method of Automapper as below:
var result = query.ProjectTo<ResultantDataModel>().ToList();
I have used below class as result model:
public class ResultantDataModel
{
public string FamilyName { get; set; }
public string LastLogin { get; set; }
public string GivenName { get; set; }
public string Email { get; set; }
public string EmailVerified { get; set; }
public string Uuid { get; set; }
public string Role { get; set; }
public string Payers { get; set; }
public string Accounts { get; set; }
public string ModifiedOn { get; set; }
}

passing object as a ref parameter to generic method

I have created a generic method, and I want to pass an object by a reference to this method to populate few properties. It compiles, and runs without problems, but the object is not being populated.
my generic method
public static void SplitAddress<T>(ref T ob, string addressToSplit) where T : Address
{
//ptr : Postcode, Town, Region
var ptr = addressToSplit.Split(new char[] { '-' }, 2, StringSplitOptions.RemoveEmptyEntries).ToList();
var pt = ptr[0].Split(new char[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries).ToList();
if (ptr.Count == 2)
{
ob.Region = ptr[1];
}
for (int x = 0; x < pt.Count; x++)
{
switch (x)
{
case 0:
{
ob.PostCode = pt[x];
break;
}
case 1:
{
ob.Town = pt[x];
break;
}
}
}
}
Object i want to pass
class Merchant : Address
{
public int MeId { get; set; }
public int HoId { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Address { get; set; }
public string PostCode { get; set; }
public string Town { get; set; }
public string Region { get; set; }
public string VatNr { get; set; }
public string TRSshopId { get; set; }
}
Address class
abstract class Address
{
public string PostCode;
public string Town { get; set; }
public string Region { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string Adrress { get; set; }
}
method invocation
Methods.SplitAddress<Merchant>(ref me, row.Cells[i].Text);
I could create two overloaded methods, for two different object types, but they will repeat the same code, which I want to avoid.
It look very odd, but for example "Postcode" is being populated, but when I hover the mouse on "ob", the property is still empty.
EDIT
As #Lee astutely noticed, you are hiding the properties of Address in Member. Since your generic method is constrained to members of type Address, your code is actually changing the properties of the Address class that are hidden, not the properties of the Merchant class, so you are not seeing those changes if you have a variable of type Merchant. You would see the values if you cast the Member to an Address. Just remove those properties from Merchant and you should be fine.
p.s. Member inheriting form Address seems wrong - a member has an address, it is not an address. a better design would be:
class Merchant
{
public int MeId { get; set; }
public int HoId { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public string VatNr { get; set; }
public string TRSshopId { get; set; }
}
Original Answer
I want to pass an object by a reference to this method to populate few properties
Since Address is a class, you don't need to use ref. A parameter of a reference types will contain a reference to the same object as the variable that's passed in, so you can change the values of the properties of that object and the calling method will see the changes. The main thing ref lets you do is change the reference to a different object, which you aren't doing, so using ref won't change what you are trying to do.
I suggest you run it in the debugger to make sure your if blocks are getting executed the way you expect them to. (e.g. is ptr.Count == 2 true? could it be greater then 2?)
Also your entire for block can be reduced to:
if(pt.Count > 0) ob.PostCode = pt[0];
if(pt.Count > 1) ob.Town = pt[1];

How to store and sort two types of classes in List<>?

I have two classes:
public class SavedQuote
{
public int ID { get; set; }
public string Text { get; set; }
public string Context { get; set; }
public string URL { get; set; }
public string Comment { get; set; }
public string WhereToSearch { get; set; }
public DateTime DateOfAdding { get; set; }
public string OwnerName { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
public class NoteOnSite
{
public int ID { get; set; }
public string URL { get; set; }
public DateTime DateOfAdding { get; set; }
public string OwnerName { get; set; }
public string Comment { get; set; }
public virtual ICollection<Tag> Tags { get; set; }
}
I have also two lists: one that represents some "SavedQuotes" and one that has some "NoteOnSites". I need to sort data from those Lists by DateOfAdding and display them in one table on my webiste.
The problem is: I (probably) can't save objects with two different classes in one List<> (I need to do this to sort those objects). What do you advise me to do? How would you solve this problem?
I (probably) can't save objects with two different classes in one List<>
You can, as long as object have a common base class. In C#, all objects have a common base class System.Object, which is enough to store objects of entirely different types in a single list.
A heavyweight approach would be to put a common interface on the objects that you wish to sort:
public interface IWithDate {
public DateTime DateOfAdding { get; set; }
}
public class SavedQuote : IWithDate {
...
}
public class NoteOnSite : IWithDate {
...
}
...
var mixedList = new List<IWithDate>();
However, this may introduce more structure than you wish: making the classes related to each other through a common interface is too much, if all you need is to sort objects of these classes together.
If you wish to sort the objects on a commonly named property without adding any static structure around your classes, you can make a list of dynamic objects, and use DateOfAdding directly:
var mixedList = new List<dynamic>();
mixedList.AddRange(quotes);
mixedList.AddRange(notes);
mixedList.Sort((a, b)=>a.DateOfAdding.CompareTo(b.DateOfAdding));
Try a little Linq using JOIN
List<SavedQuote> savedQuotes = new List<SavedQuote>();
List<NoteOnSite> noteOnSites = new List<NoteOnSite>();
var results = from savedQuote in savedQuotes.OrderBy(x => x.DateOfAdding)
join noteOnSite in noteOnSites.OrderBy(x => x.DateOfAdding)
on savedQuote.ID equals noteOnSite.ID
select new { saved = savedQuotes, note = noteOnSites };​

Request for Creational Design/Pattern Suggestion

I have a number of classes that are all related conceptually, but some more-so at the details level than others. For example, these three classes have nearly identical properties (although member functions will vary):
public class RelatedA : IRelatedType
{
public string Name { get; set; }
public string Value { get; set; }
public DateTime Stamp { get; set; }
}
public class RelatedB : IRelatedType
{
public string Name { get; set; }
public string Value { get; set; }
public DateTime Stamp { get; set; }
}
public class RelatedC : IRelatedType
{
public string Name { get; set; }
public string Value { get; set; }
public DateTime Stamp { get; set; }
public int Special { get; set; }
}
There are a couple of other classes that are conceptually related to the above 3, but can be a bit different implementation-wise:
public class RelatedD : IRelatedType
{
public string Name { get; set; }
public string Statement { get; set; }
}
public class RelatedE : IRelatedType
{
public string Name { get; set; }
public string Statement { get; set; }
public bool IsNew { get; set; }
}
Instances of these can be created by a factory based on some sort of "type" enumerated value. The problem is that later on when these objects are being used (in a business layer, for example), there could be a lot of code like this:
IRelatedType theObject = TheFactory.CreateObject(SomeEnum.SomeValue);
if (theObject is RelatedC)
{
RelatedC cObject = theObject as RelatedC;
specialVal = cObject.Special;
}
else if (theObject is RelatedD)
{
RelatedD dObject = theObject as RelatedD;
statementVal = dObject.Statement;
}
else if (theObject is RelatedE)
{
RelatedE eObject = theObject as RelatedE;
statementVal = eObject.Statement;
isNewVal = eObject.IsNew;
}
This could be repeated in many places. Is there a better approach to the design that I should be using (there's got to be)?
You could try and factor the differences into seperate classes that are then provided so for example:
IRelatedType theObject = TheFactory.CreateObject(SomeEnum.SomeValue);
RelatedTypeHelper theHelper=TheFactory.CreateHelper(theObject);
theHelper.DoSpecialThing(theObject);
Now you won't have to have all the if else blocks, and if you add a new type which requires new handling, you just whip up a new helper implement the required pieces and you should be good to go. The helper should help document this process.
I would also question why a single method would have such a different implementation for specialVal and StatementVal could be your sample, but It makes me curious what your really doing here. can you simplify things back taking a step back and questioning the point of these being included in this specific hierarchy.

Categories