I have done get data by Id (primary key) with success. But if I call Get by another field, why is it always Id that is used?
Here is my code:
ITempatAppService.cs
public interface ITempatAppService:IApplicationService
{
GetTempatOutput GetTempatById(GetTempatInput input);
GetTempatOutput GetTempatByIdKategori(GetTempatKategori input);
}
GetTempatInput.cs
public class GetTempatInput
{
public int Id { get; set; }
}
GetTempatOutput.cs
public class GetTempatKategori
{
public int IdKategori { get; set; }
}
TempatAppService.cs
public class TempatAppService:ApplicationService,ITempatAppService
{
private readonly ITempatManager _tempatManager;
public TempatAppService(ITempatManager tempatManager)
{
_tempatManager = tempatManager;
}
public GetTempatOutput GetTempatById(GetTempatInput input)
{
var getTempat = _tempatManager.GetTempatById(input.Id);
GetTempatOutput output = Mapper.Map<MasterTempat, GetTempatOutput>(getTempat);
return output;
}
public GetTempatOutput GetTempatByIdKategori(GetTempatKategori input)
{
var getTempat = _tempatManager.GetTempatByIdKategori(input.IdKategori);
GetTempatOutput output = Mapper.Map<MasterTempat, GetTempatOutput>(getTempat);
return output;
}
}
Here is my TempatManager.cs:
public class TempatManager : DomainService, ITempatManager
{
private readonly IRepository<MasterTempat> _repositoryTempat;
public TempatManager(IRepository<MasterTempat> repositoryTempat)
{
_repositoryTempat = repositoryTempat;
}
public MasterTempat GetTempatById(int Id)
{
return _repositoryTempat.Get(Id);
}
public MasterTempat GetTempatByIdKategori(int IdKategori)
{
return _repositoryTempat.Get(IdKategori);
}
}
Naming the parameter IdKategori doesn't make it search by that column. Do this:
public MasterTempat GetTempatByIdKategori(int IdKategori)
{
return _repositoryTempat.GetAll().First(t => t.IdKategori == IdKategori);
}
To take the list of the selected kategori.
public List<MasterTempat> GetTempatByIdKategori(int IdKategori)
{
return _repositoryTempat.GetAll().Where(t => t.IdKategori == IdKategori).ToList();
}
Related
I have 3 interfaces.
public interface IItem
{
string Name { get; set; }
}
public interface IEquipable : IItem
{
void Equip();
}
public interface IConsumable : IItem
{
void Use();
}
IEquipable is implemented by the classes Helmet and Bow, and IConsumable is implemented by classes Potion and Food.
Then, I have a class with a property which contains a List of IItem, and proceed to add a few items of both IEquipable and IConsumable after instantiating it.
public class Character
{
public List<IItem> Items { get; private set; }
public Character()
{
this.Items = new List<IItem>();
}
public void AddItem(IItem item)
{
this.Items.Add(item);
}
}
Program.cs
...
Character char = new Character();
char.AddItem(new Potion());
char.AddItem(new Food());
char.AddItem(new Helmet());
char.AddItem(new Bow());
...
Is there a way I can get a List of all IEquipable members from the List of IItems, each AS IEquipable?
I want to do something like
...
List<IEquipable> equipmentList = //do something to char.Items and get all items of type IEquipable.
IEquipment equipment = equipmentList.First(...)
equipment.Equip();
...
I've tried using List<IEquipable> equipmentList = char.Items.OfType<IEquipable>().ToList() but the resulting list ends up empty.
I implemented (and fixed minor typos in) your code like this:
void Main()
{
Character character = new Character();
character.AddItem(new Potion());
character.AddItem(new Food());
character.AddItem(new Helmet());
character.AddItem(new Bow());
List<IEquipable> equipmentList = character.Items.OfType<IEquipable>().ToList();
}
public class Potion : IConsumable
{
public string Name { get; set; }
public void Use()
{
throw new NotImplementedException();
}
}
public class Food : IConsumable
{
public string Name { get; set; }
public void Use()
{
throw new NotImplementedException();
}
}
public class Helmet : IEquipable
{
public string Name { get; set; }
public void Equip()
{
throw new NotImplementedException();
}
}
public class Bow : IEquipable
{
public string Name { get; set; }
public void Equip()
{
throw new NotImplementedException();
}
}
public interface IItem
{
string Name { get; set; }
}
public interface IEquipable : IItem
{
void Equip();
}
public interface IConsumable : IItem
{
void Use();
}
public class Character
{
public List<IItem> Items { get; private set; }
public Character()
{
this.Items = new List<IItem>();
}
public void AddItem(IItem item)
{
this.Items.Add(item);
}
}
Your exact code (albeit char renamed to character) works perfectly fine. The equipmentList ends up with two elements. The issue you're seeing, i.e. "the resulting list ends up empty", is not reproducible with the code you've posted.
You can use the OfType method
Filters the elements of an IEnumerable based on a specified type.
Signature
public static IEnumerable<TResult> OfType<TResult> (this IEnumerable source)
Usage
var equipable = Character.Items.OfType<IEquipable>();
Or encapsulate it as a method in the instance or an extension method if you like
So it does work like I wanted. My actual code just had another issue and I'm a dummy for not actually posting that. So here it is, for future reference.
using System.Collections.Generic;
using RolePlayGame.Library.Items.Backstage;
using System.Linq;
using System.Text;
using System;
namespace RolePlayGame.Library.Characters.Backstage
{
public class Inventory
{
public List<IItem> StoredItems { get; private set; }
public List<EquippedItem> Gear { get; private set; }
public Inventory()
{
this.StoredItems = new List<IItem>();
this.Gear = new List<EquippedItem>();
}
public bool HasItem(string name)
{
return this.StoredItems.Exists(item => item.Name == name);
}
public bool HasItem(IItem item)
{
return this.StoredItems.Contains(item);
}
public void RemoveItem(string name)
{
int firstIndex = this.StoredItems.FindIndex(item => item.Name == name);
if (firstIndex != -1)
{
this.StoredItems.RemoveAt(firstIndex);
}
}
public void RemoveItem(IItem item)
{
int firstIndex = this.StoredItems.IndexOf(item);
if (firstIndex != -1)
{
this.StoredItems.RemoveAt(firstIndex);
}
}
public void AddItem(IItem item, int quantity)
{
for (int i = 0; i < quantity; i++)
{
this.StoredItems.Add(item);
}
}
public void AddItem(IItem item)
{
this.StoredItems.Add(item);
}
public bool CheckEquipmentSlot(EquipmentSlot slot)
{
return this.Gear.Exists(item => item.UsedSlots.Contains(slot));
}
public bool HasEquipment(IEquipment equipment)
{
return this.Gear.Exists(item => item.Item == equipment);
}
public void AddEquipment(IEquipment equipment)
{
IEquipment alreadyEquipped;
foreach (EquipmentSlot slot in equipment.SlotsUsed)
{
if (this.Gear.Exists(item => item.UsedSlots.Contains(slot)))
{
alreadyEquipped = this.Gear.Find(item => item.UsedSlots.Contains(slot)).Item;
this.RemoveEquipment(slot);
this.StoredItems.Add(alreadyEquipped);
}
}
EquippedItem newEquipment = new EquippedItem(equipment);
this.Gear.Add(newEquipment);
}
public void RemoveEquipment(EquipmentSlot slot)
{
this.Gear.RemoveAll(equipment => equipment.UsedSlots.Contains(slot));
}
public int GetAttributeBonusTotal(AttributeType attribute)
{
int bonusTotal = 0;
foreach (IEquipment item in this.StoredItems.OfType<IEquipment>().ToList())
{
bonusTotal += item.GetAttributeBonus(attribute);
}
return bonusTotal;
}
public int GetCarryWeight()
{
int totalWeight = 0;
foreach (IItem item in StoredItems)
{
totalWeight += item.Weight;
}
return totalWeight;
}
public string GearToString()
{
StringBuilder builder = new StringBuilder();
builder.Append(" Equipped Gear:");
foreach (EquippedItem equipment in this.Gear)
{
builder.Append($"\n {equipment.Item.Name}");
}
return builder.ToString();
}
public string ItemsToString()
{
StringBuilder builder = new StringBuilder();
builder.Append(" Inventory:");
foreach (IItem item in this.StoredItems.Distinct())
{
builder.Append($"\n {item.Name} x {this.StoredItems.FindAll(value => value == item).Count()}");
}
return builder.ToString();
}
public int GetDefenseRateAgainstTypeTotal(DamageType againstType)
{
int rate = 0;
List<IOutfit> outfits = this.Gear.Select(value => value.Item).OfType<IOutfit>().ToList();
foreach (IOutfit item in outfits)
{
rate += item.GetDefenseRateAgainstType(againstType);
}
return rate;
}
}
}
One of the last lines has the problem (now fixed). List<IOutfit> outfits = this.Gear.Select(value => value.Item).OfType<IOutfit>().ToList(); used to be List<IOutfit> outfits = this.Gear.OfType<IOutfit>().ToList();. But Gear is of type List<EquippedItem>, and EquippedItem is not an implementation of IItem.
Here is EquippedItem.cs
using RolePlayGame.Library.Items.Backstage;
using System.Collections.Generic;
namespace RolePlayGame.Library
{
public class EquippedItem
{
public List<EquipmentSlot> UsedSlots { get; set; }
public IEquipment Item { get; set; }
public EquippedItem(IEquipment equipment)
{
this.Item = equipment;
this.UsedSlots = equipment.SlotsUsed;
}
}
}
I needed to select the Item property from the items inside Gear as another list before doing the type filtering with .OfType<IOutfit>(). That's where .Select(value => value.Item) enters the stage.
So that's that. I'll learn to post actual code for future questions.
I have the following code for supporting a list of different types :
public enum eType
{
tInt,
tString,
tDateTime
}
public interface ICustomType<out T>
{
T Value { get; }
}
public abstract class DifferentType
{
protected DifferentType(eType type, string mnemonic)
{
Type = type;
Mnemonic = mnemonic;
}
public string Mnemonic { get; private set; }
public eType Type { get; private set; }
}
public class DateTimeType : DifferentType, ICustomType<DateTime>
{
public DateTimeType(DateTime value, string mnemonic)
: base(eType.tDateTime, mnemonic)
{
Value = value;
}
public DateTime Value { get; private set; }
}
public class IntType : DifferentType, ICustomType<int>
{
public IntType(int value, string mnemonic)
: base(eType.tInt, mnemonic)
{
Value = value;
}
public int Value { get; private set; }
}
public class StringType : DifferentType, ICustomType<string>
{
public StringType(string value, string mnemonic)
: base(eType.tString, mnemonic)
{
Value = value;
}
public string Value { get; private set; }
}
public static class UtilValue
{
public static T GetValue<T>(DifferentType customType)
{
return ((ICustomType<T>)customType).Value;
}
}
public class testTypes2
{
public testTypes2()
{
var values = new List<DifferentType> { GetInt(), GetString(), GetDate() };
foreach (var i in values)
{
switch (i.Type)
{
case eType.tInt:
int resInt = UtilValue.GetValue<int>(i);
break;
case eType.tString:
string resString = UtilValue.GetValue<string>(i);
break;
case eType.tDateTime:
DateTime resDateTime = UtilValue.GetValue<DateTime>(i);
break;
}
}
}
private DateTimeType GetDate()
{
return new DateTimeType(new DateTime(2000, 1, 1), "MnemonicDate");
}
private IntType GetInt()
{
return new IntType(5, "MnemonicInt");
}
private StringType GetString()
{
return new StringType("ok", "MnemonicString");
}
}
and would like to avoid the cast at line return ((ICustomType<T>)customType).Value; in the UtilValue class, any idea how I can get rid of that while still keeping the design?
I am not even sure if this cast is expensive to do? My guess is most certainly.
Visitor-pattern example:
interface IDifferentTypeVisitor
{
void Visit(DateTimeType dt);
void Visit(StringType st);
}
class DifferentType
{
public abstract void Accept(IDifferentTypeVisitor visitor);
}
class DateTimeType : DifferentType
{
public void Accept(IDifferentTypeVisitor visitor)
{
visitor.Visit(this);
}
}
class StringType : DifferentType
{
public void Accept(IDifferentTypeVisitor visitor)
{
visitor.Visit(this);
}
}
class SomeVisitor : IDifferentTypeVisitor
{
public void Visit(DateTimeType dt)
{
//DateTime resDateTime = dt.Value; Or similar
}
public void Visit(StringType st)
{
//string resString = st.Value; Or similar
}
}
public class testTypes2
{
public testTypes2()
{
var values = new List<DifferentType> { /* Content */ };
var visitor = new SomeVisitor();
foreach (var i in values)
{
i.Accept(visitor);
}
}
}
In C# 4 with dynamic it's possible to save some code by adding this to DifferentType:
public void Accept(IDifferentTypeVisitor visitor)
{
visitor.Visit((dynamic)this);
}
and then delete all other Accept methods. It hurts performance but it looks better ;-)
I have a test method...
[TestMethod]
public void MainViewModel_PropertiesReflectDataEntityProperties()
{
// Arrange
var facilityDataEntity = MockRepository.GenerateStub<FacilityDataEntity>();
var shopOrderDataEntity = MockRepository.GenerateStub<ShopOrderDataEntity>();
// Act
MainViewModel mainViewModel = new MainViewModel(facilityDataEntity, shopOrderDataEntity);
// Assert
Assert.AreSame(facilityDataEntity.Value, mainViewModel.FacilityValue);
}
... and the test passes. However, I have not implemented the mapping of the DataEntity's properties to the MainViewModel's properties yet! How can this be? I thought AreSame checks whether two references point to the same instance.
public class MainViewModel
{
private readonly FacilityDataEntity facilityDataEntity;
private readonly ShopOrderDataEntity shopOrderDataEntity;
public MainViewModel(FacilityDataEntity facilityDataEntity)
{
this.facilityDataEntity = facilityDataEntity;
}
public MainViewModel(FacilityDataEntity facilityDataEntity, ShopOrderDataEntity shopOrderDataEntity)
{
this.facilityDataEntity = facilityDataEntity;
this.shopOrderDataEntity = shopOrderDataEntity;
}
public ShopOrderDataEntity ShopOrderDataEntity
{
get { return shopOrderDataEntity; }
}
public FacilityDataEntity FacilityDataEntity
{
get { return facilityDataEntity; }
}
public int ShopOrder { get; set; }
public decimal RequiredQuantity { get; set; }
public string ItemCode { get; set; }
public string ItemDescription { get; set; }
public string FacilityValue { get; set; }
public string FacilityLabel { get; set; }
public static IEnumerable<MainViewModel> TranslateDataEntityList(IEnumerable<FacilityDataEntity> facilityDataEntityList)
{
foreach (FacilityDataEntity facilityDataEntity in facilityDataEntityList)
{
yield return new MainViewModel(facilityDataEntity);
}
}
public static IEnumerable<MainViewModel> TranslateDataEntityList(FacilityDataEntity facilityDataEntity, IEnumerable<ShopOrderDataEntity> shopOrderDataEntityList)
{
foreach (ShopOrderDataEntity shopOrderDataEntity in shopOrderDataEntityList)
{
yield return new MainViewModel(facilityDataEntity, shopOrderDataEntity);
}
}
}
Underneath it all, these tests are just using Object.ReferenceEquals:
true if objA is the same instance as objB or if both are null; otherwise, false.
I guess this is happening because they are both null.
in this case, I'd say its comparing null with null, which are the same.
I have implemented Single Pattern. Here is my code i am getting the an error when i call the Test.BuildData() function. Please help
public class WordDataItem
{
public string Word { get; set; }
public string Definition { get; set; }
public int WordGroupKey { get; set; }
}
public class WordDataGroup
{
public List<WordDataItem> listItem = new List<WordDataItem>();
public int GroupKey { get; set; }
}
public sealed class WordDataSource
{
private static WordDataSource _dataSoruce;
private List<WordDataGroup> listGroup = new List<WordDataGroup>();
public List<WordDataGroup> ListGroup
{
get { return listGroup; }
set { listGroup = value; }
}
private WordDataSource() { }
public static WordDataSource Instance
{
get
{
if (Instance == null)
{
_dataSoruce = new WordDataSource();
}
return _dataSoruce;
}
}
}
public static class Test
{
public static void BuildData()
{
WordDataSource.Instance.ListGroup.Add(new WordDataGroup() { GroupKey = 8, listItem = new List<WordDataItem>() { new WordDataItem() {Word = "Hello", Definition="Greetings", WordGroupKey = 8}} });
}
}
I get an error of stack over flow when i call the Test.BuildData() function.
Your Instance property is recursively calling into itself when you check if it is null.
Try this:
public static WordDataSource Instance
{
get
{
if (_dataSoruce == null)
{
_dataSoruce = new WordDataSource();
}
return _dataSoruce;
}
}
I'm trying to sort a custom BindingList. But i came across the problem that my Comparer does not recognize the properties of my class.
The "x.Code_PK_OriginalValue" is not recognized. The weird thing is that intellisense marks "Begrenzingen" in Comparer class different as "Begrenzingen" in the first code block beneath.
BindingListX<Begrenzingen> lst = new BindingListX<Begr.....;
lst.OrderBy(t => t, new CustomComparer<Begrenzingen>());
.
public class CustomComparer<Begrenzingen> : IComparer<Begrenzingen>
{
private readonly Comparison<Begrenzingen> _comparison;
public CustomComparer()
{
_comparison = new Comparison<Begrenzingen>(
(Begrenzingen x, Begrenzingen y) =>
{
return x.Code_PK_OriginalValue.CompareTo(y.Code_PK_OriginalValue);
}
);
}
public int Compare(Begrenzingen x, Begrenzingen y)
{
return _comparison(x, y);
}
}
.
public class BindingListX<T> : BindingList<T>
{
public void OrderBy(Func<T,T> keySelector, IComparer<T> comparer)
{
this.Items.OrderBy(keySelector, comparer);
}
}
.
public class Begrenzingen : DefaultTable, IComparable<Begrenzingen>
{
public Begrenzingen()
{ //New -> Insert DB
Code_PK_OriginalValue = -1;
isDeleted = false;
}
public decimal Code_PK_OriginalValue { get; set; }
public decimal Code_PK { get; set; }
public string Naam { get; set; }
public decimal? SeqLayer { get; set; }
public Boolean isDeleted { get; set; }
public string SeqLayerDisplayValue {
get {
if (SeqLayer == null) return string.Empty;
return (from sdo in MainWindow.Main.SdoLayers where sdo.SeqLayer == this.SeqLayer select sdo.DisplayValue).First();
}
}
public override string ToString()
{
return String.Format("{0};{1};{2};{3}", Code_PK_OriginalValue, Code_PK, Naam, SeqLayer);
}
public int CompareTo(Begrenzingen o)
{
return Code_PK.CompareTo(o.Code_PK);
}
}
How about just lst.OrderBy(t => t.Code_PK_OriginalValue);