Nest Classes using Abstract class? (As well as protect/hide a class)? - c#

When I was working on nest classes and ran into issues. That's when somebody told me I need to use interface. Along the way on implementing the interface, there I learned that interface doesn't allow constructor or passing of the argument parameters. So, I'm thinking of abstract but I'm uncertain on implementing them and keeping it simple.
In the source code below. Environment Setting is populated in Console project and is passed on to the Database class in DLL project.
Question #1 - Is it possible to abstract Environment Setting for simplier script?
Question #2 - Using the nested class or not, How do I protected the DealerRepository class (Make it invisible to Console project? (I'm guessing abstract).
Question #3 - Does DealerRepository class need to be abstracted or what?
What I want here is to protect/hide the DealerRepository class from Console project and not having to pass on EnvironmentSetting class/model repeatly through nest classes.
Thanks...
//EnvironmentSetting.cs
namespace zTestcase1.Model {
public class EnvironmentSetting : IDisposable
{
public EnvironmentSetting() { } //Constructor...
public void Dispose() { } /Dispose...
//Member variables...
private string _emulation = "";
private string _application = "";
private string _database = "";
//Get/Set properties...
public string Emulation { get {return _emulation;} set {_emulation = value;} }
public string Application { get {return _application;} set {_application = value;} }
public string Database { get {return _database;} set {_database = value;} }
}
}
//DealerRepository.cs
namespace zTestcase1.Data.Dealer
{
public class DealerRepository : IDisposable
{
public DealerRepository(EnvironmentSetting parmEnvironmentSetting) {
_environmentSettingA = parmEnvironmentSetting;
} //Constructor...
public void Dispose() { } //Dispose...
//Member variables...
private EnvironmentSetting _environmentSettingA = "";
//Get/Set properties...
//N/A...
//Functions...
public string EnvironmentResponse()
{
return "Emulation - " + _environmentSettingA.Emulation + ", Application - " + _environmentSettingA.Application + ", Database - " + _environmentSettingA.Database + "";
}
public string DealerProifle_Lookup(string parmName)
{
return "(DealerProfile-Lookup) - " + parmName;
}
public string DealerProfile_Save(string parmName)
{
return "(DealerProfile-Save) - " + parmName;
}
}
}
namespace zTestcase1.Library
{
/*public class Database : IDisposable
{
//Class...
public class DataDealer : IDisposable
{
//Constructor...
public DataDealer(EnvironmentSetting parmEnvironmentSetting)
{
_environmentSettingA = parmEnvironmentSetting;
}
//Dispose...
public void Dispose() { }
//Member variables...
private EnvironmentSetting _environmentSettingA = null;
private DealerRepository _dataDealer = null;
//Get/Set properties...
public DealerRepository Dealer { get { if (_dataDealer == null) { _dataDealer = new DealerRepository(_environmentSettingA); } return _dataDealer; } }
//Functions...
//N/A...
}*/
//Constructor...
public Database(EnvironmentSetting parmEnvironmentSetting)
{
_environmentSettingB = parmEnvironmentSetting;
}
public void Dispose() { } //Dispose...
//Member variables...
private EnvironmentSetting _environmentSettingB = null;
//private DataDealer _dataDealerB = null;
private DealerRepository _dataDealerC = null;
//Get/Set properties...
//public DataDealer Dealer { get { if (_dataDealerB == null) { _dataDealerB = new DataDealer(_environmentSettingB); } return _dataDealerB; } }
public DealerRepository Dealer { get { if (_dataDealerC == null) { _dataDealerC = new DealerRepository(_environmentSettingB); } return _dataDealerC; } }
//Functions...
//N/A...
}
}
namespace zTestcase1.Sample1.Console
{
class Program
{
static void Main(string[] args)
{
EnvironmentSetting environmentSettting = new EnvironmentSetting();
environmentSettting.Application = "DOS Console";
environmentSettting.Database = "Not yet hooked up";
environmentSettting.Emulation = "Development";
Database _libraryDatabase = new Database(environmentSettting);
//var test1 = _libraryDatabase.Dealer.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
var test2 = _libraryDatabase.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
System.Console.WriteLine(_libraryDatabase.Dealer.EnvironmentResponse() + " [=] " + test2);
System.Console.ReadLine(); //To pause the console...
}
}
}

Answer #1: Instead of ...
//Member variables...
private string _emulation = "";
private string _application = "";
private string _database = "";
//Get/Set properties...
public string Emulation { get {return _emulation;} set {_emulation = value;} }
public string Application { get {return _application;} set {_application = value;} }
public string Database { get {return _database;} set {_database = value;} }
Your properties can be expressed this way instead (it is equivalent, except for the initialization to "", which can be done if needed in a constructor instead):
public string Emulation { get; set; }
public string Application { get; set; }
public string Database { get; set; }
Answer 2/3: Abstractness and inner classes are not used to limit the visibility of classes to outside callers. Use internal instead: this will give other classes within the same library access, while prohibiting access from outside the current library. Though, in this case, it looks as though you are working with the DealerRepository class from the Console application through the _libraryDatabase.Dealer property. I'm not exactly sure what you're trying to achieve here.

If I've understood you correctly, you need dependency injection.
This will require DI-container. E.g., with MEF the code will look like this:
1) assembly "A.dll" (contracts):
public interface IEnvironmentSetting
{
string Emulation { get; }
string Application { get; }
string Database { get; }
}
public interface IDealerRepository
{
string EnvironmentResponse();
string DealerProifle_Lookup(string parmName);
string DealerProfile_Save(string parmName)
}
public interface IDatabase
{
IDealerRepository Dealer { get; }
}
2) Assembly "B.dll" (implementations):
[Export(typeof(IDatabase))]
public class Database : IDatabase, IDisposable
{
[Import]
public IDealerRepository Dealer { get; set; }
// other code here...
}
[Export(typeof(IDealerRepository))]
public class DealerRepository : IDealerRepository, IDisposable
{
[Import]
private IEnvironmentSetting EnvironmentSetting { get; set; }
// other code here...
}
[Export(typeof(IEnvironmentSetting))]
public class EnvironmentSetting : IEnvironmentSetting, IDisposable
{
// other code here...
}
3) Assembly "C.exe" (console application). You need to refer assembly "A.dll" only from "C.exe":
static void Main(string[] args)
{
using (var catalog = // configure composable part catalog here, read MEF docs to find best way for you)
using (var container = new CompositionContainer(catalog))
{
var database = container.GetExportedValue<IDatabase>();
// you're working with IDatabase and IDealerRepository here,
// there's no direct access to the particular implementation classes
database.Dealer.DealerProifle_Lookup("Best Dealer Cars Buy");
// ...
}
}

Related

Assign string[] array field to another array field of same type c#

I have two classes defined in my solution
public class Registration {
[...]
public list<Account> Accounts {get; set;}
}
public class Account {
[...]
public string Code { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
In the web service that I am consuming, the following class definitions are available
public partial class VendReg {
[...]
private Payment_Details[] requestDetailsField;
[System.Xml.Serialization.XmlArrayItemAttribute(IsNullable=false)]
public Payment_Details[] RequestDetails {
get {
return this.requestDetailsField;
}
set {
this.requestDetailsField = value;
}
}
}
public partial class Payment_Details {
private string bk_CodeField;
private string bk_NameField;
private string bk_AddressField;
public string Bk_Code {
get {
return this.bk_CodeField;
}
set {
this.bk_CodeField = value;
}
}
public string Bk_Name {
get {
return this.bk_NameField;
}
set {
this.bk_NameField = value;
}
}
public string Bk_Address {
get {
return this.bk_AddressField;
}
set {
this.bk_AddressField = value;
}
}
}
I want to assign Account to Request Details which is an array of Payment_Details. I tried this code below
vendReg.RequestDetails = registration.Accounts.Cast<Payment_Details>().ToArray();
I got invalid cast exception: Unable to cast object of type 'Account' to type 'Payment_Details'
Please guide on what I am not doing right
You need to convert this yourself (or you can look into things like Automapper)
vendReg.RequestDetails = registration.Accounts.Select(acc =>
new Payment_Details {
Bk_Code = acc.Code,
Bk_Name = acc.Name,
Bk_Address = acc.Address
}).ToArray();

C# Generic way to store unknown properties in List

I'd like to store LicenseInformations for multiple domains in my application.
The structure looks the following way:
public class LicenseData
{
// properties...
public List<LicenseDomain> Domains { get; set; }
// other properties...
}
public class LicenseDomain
{
// properties...
public object LicenseConfig { get; set; }
}
We have multiple domains with total different properties, but the license may contain multiple configurations..
For example:
{
"MaxValidUsers": 5
}
{
"Property": "xy"
"SubProperty": { "Foo" : "Bar"
}
}
The generation is no problem in any way..
But if I restore the informations from my signed json file I deserialize to object..
Which pattern / possiblity I have to work with Interfaces / Abstracts / that I can (RE)store generic informations here..
Right now I hack with:
JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(domain.LicenseConfig))
But I can't agree with myself.
So, based on the pieces of context I can grab, I would actually recommend having your LicenseConfig stored as a JSON string, which would give you the ability to do something like this:
public class LicenseDomain
{
// properties...
// Depending on how this is loaded,
// this property (or at least its setter) could be made private/protected/internal
public string LicenseConfigJson { get; set; }
public T LicenseConfig<T>() where T : BaseLicenseConfig
{
if (string.IsNullOrWhiteSpace(LicenseConfigJson))
{
return null;
}
return JsonConvert.DeserializeObject<T>(LicenseConfigJson);
}
public void SaveLicenseConfig<T>(T config) where T : BaseLicenseConfig
{
if (config == null)
{
LicenseConfigJson = null;
}
else
{
LicenseConfigJson = JsonConvert.SerializeObject(config);
}
}
}
Or if each LicenseDomain can only have one type of LicenseConfig, you could make it a generic parameter to the class:
public class LicenseData
{
// properties...
public List<LicenseDomain<BaseLicenseConfig>> Domains { get; set; }
// other properties...
}
public class LicenseDomain<T> where T : BaseLicenseConfig
{
// properties...
// Depending on where this value comes from, you could do this a variety of ways,
//but this is just one
public string LicenseConfigJson { get; set; }
public T LicenseConfig
{
get
{
if (string.IsNullOrWhiteSpace(LicenseConfigJson))
{
return null;
}
return JsonConvert.DeserializeObject<T>(LicenseConfigJson);
}
set
{
if (value == null)
{
LicenseConfigJson = null;
}
else
{
LicenseConfigJson = JsonConvert.SerializeObject(value);
}
}
}
}
public abstract class BaseLicenseConfig
{
}
public class LicConfig1 : BaseLicenseConfig
{
public int MaxValidUsers { get; set;}
}
public class LicConfig2 : BaseLicenseConfig
{
public string Property {get;set;}
public SubProp SubProperty {get;set;}
}
public class SubProp
{
public string Foo {get;set;}
}
In both cases, the BaseLicenseConfig class is strictly to enforce that everything in the domain list can come from a base class of some kind. If that's not important, you don't need the base class and can remove the where T : BaseLicenseConfig from LicenseDomain class.

Create game inventory system, without casting to derived

I am trying to implement a high-performance game inventory system. I have This abstract base class to store different type of items in Inventory, for example, Coin, Flashlight, Knife etc..
public abstract class ObtainableItem
{
public string Name { get; private set; }
public ObtainableItem(string name)
{
Name = name;
}
}
For example, I have a DoorKey which opens a door. DoorKey has a property KeyCode which will be used for opening a door.
public class DoorKey : ObtainableItem
{
public int KeyCode { get; private set; }
public DoorKey() : base("key")
{
KeyCode = 1234;
}
}
All ObtainableItem are stored in Inventory
public class Inventory
{
const int slotCount = 2;
ObtainableItem[] slots = new ObtainableItem[slotCount];
public Inventory()
{
slots[0] = new DoorKey();
}
}
Now imagine user drags DoorKey from his Inventory on a Door and triggers Open method
public class Door
{
public void Open(ObtainableItem key)
{
if (key is DoorKey)
{
DoorKey doorKey = (DoorKey)key;
if (doorKey.KeyCode == 1234)
{
// Open door
}
}
else
{
// "can't use this item on a door"
}
}
}
How to avoid cast from ObtainableItem to a DoorKey? I have read that using casting is bad practice and it points at a bad code oop design. Ideally, a Door class should look like this. Is there any pattern I should for my inventory system?
public class Door
{
public void Open(DoorKey key)
{
if (key.KeyCode == 1234)
{
// Open door
}
}
}
There are always exceptions that can be made for ease of implementation and readability. What you describe is common, if not typical.
An alternative would be to have the "control" logic in the class that calls Door.Open. This could be easily achieved with a touch of reflection:
public abstract class ObtainableItem
{
public string Name { get; private set; }
public ObtainableItem(string name)
{
Name = name;
}
}
public abstract class WorldItem
{
}
public interface IActsOn<in TWorldItem>
where TWorldItem : WorldItem
{
void ApplyTo(TWorldItem worldItem);
}
public class World
{
// If profiling shows that this is a performance issue, a cache keyed by tWorldItem, tInvItem
// should fix it. No expiry or invalidation should be needed.
private Action<ObtainableItem, WorldItem> GetApplyTo(Type tWorldItem, Type tInvItem)
{
var tActOn = typeof(IActsOn<>).MakeGenericType(tWorldItem);
if (!tActOn.IsAssignableFrom(tInvItem))
{
return null;
}
var methodInfo = tActOn.GetMethod(nameof(IActsOn<WorldItem>.ApplyTo));
return new Action<ObtainableItem, WorldItem>((invItem, worldItem) =>
{
methodInfo.Invoke(invItem, new object[] { worldItem });
});
}
public bool IsDropTarget(WorldItem worldItem, ObtainableItem item)
=> GetApplyTo(worldItem.GetType(), item.GetType()) != null;
public void ActOn(WorldItem worldItem, ObtainableItem item)
{
var actOn = GetApplyTo(worldItem.GetType(), item.GetType());
if (actOn == null)
{
throw new InvalidOperationException();
}
actOn(item, worldItem);
}
}
While this slightly complicates the implementation of World, it simplifies the implementation of various objects:
class Door : WorldItem
{
public void Unlock(string bitting)
{
if (bitting == "1234")
{
Console.WriteLine("Door Opened");
}
else
{
Console.WriteLine("Door could not unlock");
}
}
}
class DoorKey : ObtainableItem, IActsOn<Door>
{
private readonly string Bitting;
public DoorKey(string bitting)
: base("Key")
{
this.Bitting = bitting;
}
public void ApplyTo(Door worldItem)
{
worldItem.Unlock(this.Bitting);
}
}
class RubberChicken : ObtainableItem
{
public RubberChicken()
: base("Rubber chicken")
{
}
}
Example usage:
class Program
{
static void Main(string[] args)
{
var key1 = new DoorKey("1234");
var key2 = new DoorKey("4321");
var rubberChicken = new RubberChicken();
var door = new Door();
var world = new World();
Debug.Assert(!world.IsDropTarget(door, rubberChicken));
Debug.Assert(world.IsDropTarget(door, key1));
world.ActOn(door, key2);
world.ActOn(door, key1);
Console.ReadLine();
}
}

Constructors GetInfo

I am new to C# and am working on classes and understanding them. My problem is I am not understanding how to create a Get to retrieve the private variable _yourname and Set to set the private variable _yourname.
namespace WindowsFormsApplication1
{
class InputClass
{
private string _yourName;
public string _banner;
public virtual void GetInfo();
public InputClass(String _banner)
{
_banner = "Enter your name";
}
}
}
Maybe I am using the wrong function to GetInfo. But I am also wondering when I have the GetInfo if in the () I should write _yourname in it.
In C# there are properties, which have the function of public getter and setter methods in other languages:
class InputClass
{
private string _yourName;
public string _banner;
public InputClass(String _banner)
{
this._banner = _banner;
}
public string YourName
{
get { return _yourName; }
set { _yourName = value; }
}
}
But you can use auto properties, if you want:
class InputClass
{
public InputClass(String _banner)
{
Banner = _banner;
}
public string YourName
{
get; set;
}
public string Banner
{
get; set;
}
}
It sounds like you are trying to provide access to the _yourName field. If so then just use a property
class InputClass {
public string YourName {
get { return _yourName; }
set { _yourName = value; }
}
...
}
Now consumers of InputClass can access it as if it were a read only field.
InputClass ic = ...;
string yourName = ic.YourName;
ic.YourName = "hello";
Note: C# provides a special syntax for simple properties like this which are just meant to be wrappers over private fields. It's named auto-implemented properties
class InputClass {
public string YourName { get; set; }
}
You can override getters and settings using the get and set keywords. For example:
class InputClass
{
private string _yourName;
private string _banner;
public YourName
{
get { return _yourName; }
set { _yourName = value; }
}
public Banner
{
get { return _banner; }
set { _banner = value; }
}
public InputClass(String banner)
{
_banner = banner;
}
}
1.) Use properties instead of members, you get a free accessor (get) and mutator (set).
public string YourName { get; set; }
public string Banner { get; set; }
2.) You can take advantage of the default constructor, and declare it on the fly.
//the old way:
InputClass myClass = new InputClass();
myClass.YourName = "Bob";
myClass.Banner = "Test Banner";
//on the fly:
InputClass myClass = new InputClass()
{
YourName = "Bob",
Banner = "Test Banner"
}

MEF Import Scenario

Hi
I have some problems in import scenarios example:
[Export(typeof(IICon))]
public class WriteInputData : IICon
{
[Import(typeof(IIOWriter))]
public IIOWriter IOWriter { get; set; }
public object Input { get; set; }
public void Process()
{
IOWriter.Write(Input);
}
}
Then i hawe two classes that implement interface IIOWriter like :
[Export(typeof(IIOWriter))]
public class FileWriter : IIOWriter
{
public string FilePath { get; set; }
public void Write(object data)
{
if (string.IsNullOrEmpty(FilePath))
FilePath = #"c:\test.txt";
var fl = new StreamWriter(FilePath, true);
fl.Write((string)data);
fl.Flush();
fl.Close();
}
public string Name
{
get { return "FileWriter"; }
}
}
[Export(typeof(IIOWriter))]
public class ConsoleWrite : IIOWriter
{
public void Write(object data)
{
Console.WriteLine((string)data);
}
public string Name
{
get { return "ConsoleWrite"; }
}
}
How can i let that to user so he can change that in runtime, so example whene he type select in ListBox FileWriter than the IIOWriter in WriteInputData will be injected FileWriter end so one..
Sorry for my bad english.
You probably need to supply some metadata to the export, such like:
[Export(typeof(IIOWriter)),
ExportMetadata("Name", "ConsoleWriter")]
public class ConsoleWriter : IIOWriter
{
}
The reason you need to do this, is that you need to know ahead of time what the user selection will match to. Because of this, you may want to refactor your design to remove the dependency on the IOWriter property:
[Export(typeof(IICon))]
public class WriteInputData : IICon
{
public object Input { get; set; }
public void Process(IIOWriter writer)
{
}
}
If you define your Process method to take in an instance, we can resolve it using the CompositionContainer. Firstly, define a metadata interface that matches your ExportMetadata value:
public interface INamedMetadata
{
string Name { get; }
}
And then, we can resolve the instance:
public IIOWriter GetWriter(string name)
{
return container
.GetExports<IIOWriter, INamedMetadata>()
.Where(e => e.Metadata.Name.Equals(name, StringComparison.OrdinalIgnoreCase))
.Select(e => e.Value)
.FirstOrDefault();
}
Hope that points you in the right direction....

Categories