i'm trying to generate some methods like below, add crm
and human resources
. But i want to write Crm,humanResources,SupplChain etc. (5-6 Modules) how to write below codes simple(Below codes is skeleton :))
i'd written without generics first and then i've written with generics. Second usage differs from first.
1) Clear reading also managed and expendable
2) give me some performance advantages. No boxing and unboxing process
CAN YOU teach me pros and cons of generics WITH compare first and second usage?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace App.GenericsBaseClass.Diff
{
// View :
class Program
{
static void Main(string[] args)
{
new Crm().Add("Yapı Kredi");
Console.Read();
new HumanResources().Add("yusuf karatoprak");
Console.Read();
}
}
// Business
public class Crm : Customer
{
public override void Add(object obj)
{
if (obj is String)
{
base.Add(obj);
}
else
{
throw new InvalidOperationException("Customer Name must be string Format...");
}
}
}
public class HumanResources : Staff
{
public override void Add(object obj)
{
if (obj is string)
{
base.Add(obj);
}
else
{
throw new InvalidOperationException("Stuff Name must be string Format...");
}
}
}
// Dal
public class Staff
{
public virtual void Add(object obj)
{
new Db.Staff().Save((string)obj);
}
}
public class Customer
{
public virtual void Add(object obj)
{
new Db.Customer().Save((string)obj);
}
}
// Model
public class Db
{
public class Customer
{
public void Save(string Name)
{
Console.WriteLine("Customer : {0} is saved",Name);
}
}
public class Staff
{
public void Save(string Name)
{
Console.WriteLine("Staff: {0} is saved", Name);
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace App.GenericsInstadofBaseMethod
{
class Program
{
static void Main(string[] args)
{
new Crm<string>().Add("Yapı Kredi");
Console.Read();
new HumanResources<string>().Add("yusuf karatoprak");
Console.Read();
}
}
// Business
public class Crm<T> : Customer<T>
{
public override void Add(T obj)
{
base.Add(obj);
}
}
public class HumanResources<T> : Staff<T>
{
public override void Add(T obj)
{
base.Add(obj);
}
}
// Dal
public class Staff<T>
{
public virtual void Add(T obj)
{
new Db<T>.Staff().Save(obj);
}
}
public class Customer<T>
{
public virtual void Add(T obj)
{
new Db<T>.Customer().Save(obj);
}
}
// Model
public class Db<T>
{
public class Customer
{
public void Save(T Name)
{
Console.WriteLine("Customer : {0} is saved", Name.ToString());
}
}
public class Staff
{
public void Save(T Name)
{
Console.WriteLine("Staff: {0} is saved", Name);
}
}
}
}
Aside from my post explaining some things about generics, your situation here doesn't look like it really requires them at all. You should instead add a property to your class.
public class Person {
public string Name { get; set; }
}
public class Customer : Person {
}
public class Staff : Person {
}
public class HumanResources : Staff {
}
public class Db {
public List<Customer> Customers { get; set; }
public List<Staff> Staff { get; set; }
}
public static void Main(){
var db = new Db {
Customers = new List<Customer> { },
Staff = new List<Staff> { }
};
Db.Customers.Add(new Customer { Name = "Primary Customer" } );
Db.Staff.Add(new Staff { Name = "Employee Prime" } );
Db.Staff.Add(new HumanResources { Name = "Human Resource Manager" });
}
The generics should be used to insist on the type that goes in, not to actually control the data. The example you have isn't using generics in its real intended fashion.
In this example, you create a base class and then three sub-types, one of which inherits from another sub-type. So you have Customer
and Staff
as your two primary types that the Db
, (assuming that is short for database) accepts.
The Db
can take a List of Customer
s and a list of Staff
, since HumanResources
is a type of Staff
you can store them together. You could also add a List<Person> People { get; set; }
and it would accept all of the types since they inevitably inherit from each other.
As for enforcing strings, that would be better done in your constructors.
public class Person {
public Person(string name) {
this.Name = name; }
}
public string Name { get; private set; }
}
In this example, we are setting up the Name
field so that it can be seen by everything, but only changed and set at creation (not really the best idea, but it demonstrates the point).
So now, you would add a new Customer
like this ..
Db.Customers.Add( new Customer ( "Ciel" ) );
or a new Staff... Db.Staff.Add( new Staff( "Darin" ) );
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With