Convert to using Generics - c#

I've tried several times and I keep getting myself into a loop. How would you change this code to a generics based approach? This code follows the Gang of Four Composite Pattern.
public abstract class AdjacencyTreeBase
{
public AdjacencyTreeBase(int entityId, int entityTypeId)
{
EntityId = entityId;
EntityTypeId = entityTypeId;
}
public long? Id { get; set; }
public int? SystemId { get; set; }
public int EntityId { get; set; }
public int EntityTypeId { get; set; }
public bool? isActive { get; set; }
public long? lft { get; set; }
public long? rgt { get; set; }
public abstract void AddChild(AdjacencyTreeBase c);
public abstract void RemoveChild(AdjacencyTreeBase c);
public abstract List<AdjacencyTreeBase> ListChildren();
public abstract void AddChildren(List<AdjacencyTreeBase> c);
public abstract void ReplaceChildren(List<AdjacencyTreeBase> c);
}
public class AdjacencyTree : AdjacencyTreeBase
{
private List<AdjacencyTreeBase> _children = new List<AdjacencyTreeBase>();
public List<AdjacencyTreeBase> Children { get { return _children; } set { _children = value; } }
public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { }
public override void AddChild(AdjacencyTreeBase component)
{
_children.Add(component);
}
public override void AddChildren(List<AdjacencyTreeBase> c)
{
_children = c;
}
public override void ReplaceChildren(List<AdjacencyTreeBase> c)
{
_children = c;
}
public override void RemoveChild(AdjacencyTreeBase component)
{
_children.Remove(component);
}
public override List<AdjacencyTreeBase> ListChildren()
{
return _children;
}
}
public class AdjacencyAgency : AdjacencyTree
{
public string agency_name { get; set; }
public string customer_number { get; set; }
public string agency_type { get; set; }
public AdjacencyAgency(int entityId, int entityTypeId) : base(entityId, entityTypeId)
{
}
}
public class AdjacencyUser : AdjacencyTree
{
public string officer_number { get; set; }
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public AdjacencyUser(int entityId, int entityTypeId) : base(entityId, entityTypeId)
{
}
}
public class AdjacencyClient : AdjacencyTree
{
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public string ssn { get; set; }
public AdjacencyClient(int entityId, int entityTypeId) : base(entityId, entityTypeId)
{
}
}
A sample of instantiating this object map:
public List<AdjacencyTreeBase> CreateSample()
{
// build bottom of tree objects...
var client1 = new AdjacencyClient(1, 4)
{
first_name = "Pic'nic",
last_name = "Basket #1",
ssn = "123-45-6789"
};
var client2 = new AdjacencyClient(2, 4)
{
first_name = "Pic'nic",
last_name = "Basket #2",
ssn = "234-56-7890"
};
var client3 = new AdjacencyClient(3, 4)
{
first_name = "Bear",
last_name = "Cave",
ssn = "345-67-8901"
};
var client4 = new AdjacencyClient(4, 4)
{
first_name = "Picnic",
last_name = "Table",
ssn = "456-78-9012"
};
// build the next level up and add the children...
var officer1 = new AdjacencyUser(1, 3)
{
first_name = "Yogi",
last_name = "Bear",
officer_number = "YB123"
};
officer1.AddChild(client1);
officer1.AddChild(client2);
var officer2 = new AdjacencyUser(2, 3)
{
first_name = "Park",
last_name = "Ranger",
officer_number = "PR123"
};
officer2.AddChild(client3);
officer2.AddChild(client4);
// build the top of the tree and add the middle children...
var agencyThatAlreadyExists = new AdjacencyAgency(1, 2)
{
agency_name = "Jellystone",
agency_type = "Park",
};
agencyThatAlreadyExists.AddChild(officer1);
agencyThatAlreadyExists.AddChild(officer2);
return agencyThatAlreadyExists;
}
While my sample is pretty simple, our entity structure is not quite so simple. We currently have 7 different entities and pretty much any type of entity can be a child of any type of entity and its siblings could be various types as well.
TIA
EDIT:
To try to clarify: The children (and children of children) can be of any entity type (agency, user, officer, client, etc). While all entities have a base of properties in common, the rest of each object is different from one another. When pulling from the database, I may make a request for an agency and want the entire hierarchy underneath that one agency. Direct descendants could include all types, the each child could have descendants which include all types. Very messy, very flexible.

This works to make your class hierarchy strongly-typed & comply with the CreateSample code:
public abstract class AdjacencyTreeBase<T> where T : AdjacencyTreeBase<T>
{
public AdjacencyTreeBase(int entityId, int entityTypeId)
{
EntityId = entityId;
EntityTypeId = entityTypeId;
}
public long? Id { get; set; }
public int? SystemId { get; set; }
public int EntityId { get; set; }
public int EntityTypeId { get; set; }
public bool? isActive { get; set; }
public long? lft { get; set; }
public long? rgt { get; set; }
public abstract void AddChild(T c);
public abstract void RemoveChild(T c);
public abstract List<T> ListChildren();
public abstract void AddChildren(List<T> c);
public abstract void ReplaceChildren(List<T> c);
}
public abstract class AdjacencyTree : AdjacencyTreeBase<AdjacencyTree>
{
private List<AdjacencyTree> _children = new List<AdjacencyTree>();
public List<AdjacencyTree> Children { get { return _children; } set { _children = value; } }
public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { }
public override void AddChild(AdjacencyTree component)
{
_children.Add(component);
}
public override void AddChildren(List<AdjacencyTree> c)
{
_children = c;
}
public override void ReplaceChildren(List<AdjacencyTree> c)
{
_children = c;
}
public override void RemoveChild(AdjacencyTree component)
{
_children.Remove(component);
}
public override List<AdjacencyTree> ListChildren()
{
return _children;
}
}
public class AdjacencyAgency : AdjacencyTree
{
public string agency_name { get; set; }
public string customer_number { get; set; }
public string agency_type { get; set; }
public AdjacencyAgency(int entityId, int entityTypeId) : base(entityId, entityTypeId)
{
}
}
public class AdjacencyUser : AdjacencyTree
{
public string officer_number { get; set; }
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public AdjacencyUser(int entityId, int entityTypeId) : base(entityId, entityTypeId)
{
}
}
public class AdjacencyClient : AdjacencyTree
{
public string last_name { get; set; }
public string first_name { get; set; }
public string middle_initial { get; set; }
public string ssn { get; set; }
public AdjacencyClient(int entityId, int entityTypeId) : base(entityId, entityTypeId)
{
}
}
Then CreateSample needs to be modified like this:
public List<AdjacencyTree> CreateSample()
{
// build bottom of tree objects...
var client1 = new AdjacencyClient(1, 4)
{
first_name = "Pic'nic",
last_name = "Basket #1",
ssn = "123-45-6789"
};
var client2 = new AdjacencyClient(2, 4)
{
first_name = "Pic'nic",
last_name = "Basket #2",
ssn = "234-56-7890"
};
var client3 = new AdjacencyClient(3, 4)
{
first_name = "Bear",
last_name = "Cave",
ssn = "345-67-8901"
};
var client4 = new AdjacencyClient(4, 4)
{
first_name = "Picnic",
last_name = "Table",
ssn = "456-78-9012"
};
// build the next level up and add the children...
var officer1 = new AdjacencyUser(1, 3)
{
first_name = "Yogi",
last_name = "Bear",
officer_number = "YB123"
};
officer1.AddChild(client1);
officer1.AddChild(client2);
var officer2 = new AdjacencyUser(2, 3)
{
first_name = "Park",
last_name = "Ranger",
officer_number = "PR123"
};
officer2.AddChild(client3);
officer2.AddChild(client4);
// build the top of the tree and add the middle children...
var agencyThatAlreadyExists = new AdjacencyAgency(1, 2)
{
agency_name = "Jellystone",
agency_type = "Park",
};
agencyThatAlreadyExists.AddChild(officer1);
agencyThatAlreadyExists.AddChild(officer2);
return new List<AdjacencyTree>() { agencyThatAlreadyExists };
}

I guess I'm not totally sure what you're hoping to accomplish, but I can tell you this much.
First off, C# doesn't offer a good way to do exactly what I understand you're looking for. You won't be able to replicate what you've got using generics, because you can't inherit from a generic type argument (which makes sense, from a conflict-avoidance perspective).
That said, what I would do, is something like this:
public class AdjacencyTree<T> : AdjacencyTree
{
public AdjacencyTree(int entityId, int entityTypeId) : base(entityId, entityTypeId) { }
public T Value { get; set; }
}
Of course, you're adding an extra layer of properties here. You could call that a good or a bad thing, but that's the best there is.
You'll, of course, then need to instantiate them like this:
var client1 = new AdjacencyTree<Client>(1, 4)
{
Value = new Client()
{
first_name = "Pic'nic",
last_name = "Basket #1",
ssn = "123-45-6789"
}
};
It's up to you whether this is better or worse, but it does have the distinct advantage that you can play with Client instances without touching the collection type.
If you want to be extra clever, you could add a method like this:
public class AdjacencyTree<T> : AdjacencyTree
{
// ...
public void AddChild<TChild>(int entityId, int entityTypeId, TChild child)
{
var child = new AdjacencyTree<TChild>(entityId, entityTypeId)
{
Value = child
};
this.AddChild(child);
}
}
But that's really up to whether it would be useful. Some might object to adding this, as it unnecessarily duplicates the constructor arguments. But that's, of course, up to you. This is just another example.
Beyond that, I'd be tempted to drop entityTypeId from everything, unless you absolutely need it. That's up to how you want to use it, but I left it here based on your implementation. It seems like you could infer it based on T, but I wasn't sure why you needed it in your implementation either.
Even without generics being included, by my understanding, your existing pass-through constructor could look like this:
public AdjacencyUser(int entityId) : base(entityId, 3)
Thereby removing some redundancy. Of course, this is dependent on the assumption that two "User" types will always have the same EntityTypeId, which may or may not be fair.
I'm not sure if I've actually answered your question here, but hopefully it leads you in a bit of the right direction!

Related

Project a name-value list into an object

I want to be able to save an arbitrary flat object into the name-value list.
public class NameValueListEntity
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[InverseProperty(nameof(NameValueListContentEntity.Entity))]
public ICollection<NameValueListContentEntity> Content { get; set; }
}
public class NameValueListContent
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("entity_fk")]
public NameValueListEntity Entity { get; set; }
public string Name { get; set; }
public string Value { get; set; }
}
public class ObjectToSave
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
}
I could use reflection to manually assemble/parse the list, but it will create a lot of overhead. Lots of NameValueListContent objects will be needlessly created both during the saving and the reading. Could it somehow be omitted? Especially during the reading, which is very performance-sensitive in my case.
Assume you have a AppDbContext class that holds your NameValueListContent class objects named as NVListContents. You can read and write the name-value list of objects by doing the following:
public class AppDbContext : DbContext
{
public DbSet<NameValueListContent> NVListContents { get; set; }
public AppDbContext()
: base()
{ }
}
public class SomeClass
{
private AppDbContext context { get; set; }
public SomeClass(AppDbContext _context)
{
context = _context;
}
public List<ObjectToSave> ReadObjects()
{
return context.NVListContents
.Select(nvlc => new ObjectToSave { Prop1 = nvlc.Name, Prop2 = nvlc.Value
}).ToList();
}
public bool WriteObjects(int id, string name, string value)
{
var query = context.NVListContents
.FirstOrDefault(nvlc => nvlc.Id == id);
if(query != null)
{
query.Name = name;
query.Value = value;
context.Update(query);
context.SaveChanges();
return true;
}
else
{
return false;
}
}
}
Hope, this answers to your question.

How to access a property in another class?

I am working with a WPF .Net Core 3 project.
In my UnbalancedViewModel I need to access an ID from another class (TestRunDto.cs).
UnbalancedViewModel
public class UnbalancedViewModel : ViewModelBase, IUnbalancedViewModel
{
private TestRunApi _testRunApi;
public UnbalancedViewModel(TestRunApi testRunApi, INotificationManager notifications)
{
_testRunApi = testRunApi;
}
private void StartTestRunJobExecuted(object obj)
{
_testRunApi.StartTestRun(1); ////I need the Id from TestRunDto (TestRunDto.Id)
}
}
TestRunApi
public async Task<TestRunLiveValueDto> GetTestRunLiveValue(int jobRunId)
{
await using var dbContext = new AldebaDbContext(_connectionString);
return await TestRunInteractor.GetTestRunLiveValue(jobRunId, dbContext);
}
public async Task StartTestRun(int testRunId)
{
await using var dbContext = new AldebaDbContext(_connectionString);
await TestRunInteractor.StartTestRun(dbContext, testRunId);
}
TestRunLiveValueDto
public class TestRunLiveValueDto
{
public TestRunDto TestRun { get; }
public bool ShowInstantaneousValue { get; set; }
public bool EnableStart { get; set; }
public bool EnableStop { get; set; }
public bool EnableMeasure { get; set; }
public int RecipeRpm { get; }
public string ActualRecipeName { get; }
public int DefaultSetOfPlaneId { get; }
public ICollection<BalancePlaneDto> ListBalancePlane { get; }
public ICollection<SetOfPlaneDto> ListSetOfPlane { get; }
public ICollection<SensorVibrationDto> SensorVibrations { get; set; }
public ICollection<EstimationDto> InstantaneousValues { get; set; }
public ICollection<EstimationDto> EstimationsValues { get; set; }
private TestRunLiveValueDto(TestRunDto testRun, bool enableStart, bool enableStop, int recipeRpm, ICollection<SensorVibrationDto> sensorVibrations)
{
EnableStart = enableStart;
EnableStop = enableStop;
TestRun = testRun;
RecipeRpm = recipeRpm;
SensorVibrations = sensorVibrations;
}
public static TestRunLiveValueDto Create(TestRunDto testRun, bool enableStart, bool enableStop, int recipeRpm, ICollection<SensorVibrationDto> sensorVibrations)
=> new TestRunLiveValueDto(testRun, enableStart, enableStop, recipeRpm, sensorVibrations);
}
TestRunDto
public class TestRunDto
{
public int Id { get; set; }
public int JobRunId { get; set; }
public string Name { get; set; }
public int TestRunNumber { get; set; }
public RunState State { get; set; }
public ICollection<BalancePlaneDto> BalancePlanes { get; set; } // Todo remove
private TestRunDto(int id, int jobRunId, RunState state, string name, int testRunNumber)
{
Id = id;
JobRunId = jobRunId;
Name = name;
TestRunNumber = testRunNumber;
State = state;
}
public static TestRunDto Create(int id, int jobRunId, RunState state, string name, int testRunNumber)
=> new TestRunDto(id, jobRunId, state, name, testRunNumber);
}
I have been trying to understand this, but I can not get a hold of the proper method to do this. Do I first declare a new TestRunDto class in my viewmodel or am I supposed to access it some other way?
You need to ensure class A has a reference to an instance of class B to access the properties, for example one way of doing this is to pass class A to B in a method where you can manipulate or access properties.
public class FooA
{
public string PropertyA { get; set; }
}
public class FooB
{
public string PropertyB { get; set; }
public void CanAccessFooA(FooA a)
{
a.PropertyA = "See, I can access this here";
}
}
Another is to pass class A to B in the constructor (known as dependency-injection)
public class FooB
{
FooA _a;
public FooB(FooA a)
{
// Pass instance of FooA to constructor
// (inject dependency) and store as a member variable
this._a = a;
}
public string PropertB { get; set; }
public void CanAccessFooA()
{
if (this._a != null)
this._a.PropertyA = "See, I can access this here";
}
}
Exactly how to structure your code is up to you, but the principle remains the same: Class B can only access Class A if it has a reference to an instance of it.
Look into 'Dependency Injection' as there are many techniques to achieve this.
Edit
One such technique might be abstracting the code to provide the ID to both, like so
public class IdProvider
{
public int Id { get; set; }
}
public class FooA
{
private int _id;
public FooA(IdProvider idProvider)
{
_id = idProvider.Id;
}
}
public class FooB
{
private int _id;
public FooB(IdProvider idProvider)
{
_id = idProvider.Id;
}
}
Now both classes have the same ID;
StartTestRun takes the tesRunId as it's parameter.
public async Task StartTestRun(int testRunId)
{
I think you need to call StartTestRunJobExecuted with this testRunId.
You will to change
private void StartTestRunJobExecuted(object obj)
to
private void StartTestRunJobExecuted(int testRunIdn)
{
_testRunApi.StartTestRun(testRunId); ////I need the Id from TestRunDto (TestRunDto.Id)
}
(This based on me guessing).

How to get data out of sqlite database by using a function?

I am making a application in Xamarin forms but whatever I try I can't get the sqlite database working. I want to select all Categories where menu_ID = 1 how can I do this? I need this code inside a other page (CategoriePage.xaml.cs) can somewann help me with this?
Here is some code that I use:
(Tables.cs):
namespace AmsterdamTheMapV3
{
public class Categories
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public int Menu_ID { get; set; }
public string Name { get; set; }
public Categories()
{
}
}
public class Places
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public int Categorie_ID { get; set; }
public string Name { get; set; }
public Boolean Featured { get; set; }
public string OpenHours { get; set; }
public string Info { get; set; }
public string Images { get; set; }
public string Phone { get; set; }
public string Website { get; set; }
public string Adress { get; set; }
public Places()
{
}
}
public class Events
{
[PrimaryKey, AutoIncrement]
public int ID { get; set; }
public Events()
{
}
}
}
DB helper (TheMapDB.cs):
public class TheMapDB
{
private SQLiteConnection db;
public TheMapDB()
{
//Getting conection and Creating table
db = DependencyService.Get<ISQLite>().GetConnection();
db.CreateTable<Categories>();
db.CreateTable<Places>();
db.CreateTable<Events>();
var categories = new Categories()
{
ID = 1,
Menu_ID = 1,
Name = "test"
};
db.Insert(categories); // Insert the object in the database
}
public IEnumerable<Categories> GetCategories()
{
return (from t in db.Table<Categories>() select t).ToList();
}
//Get specific Categorie
public Categories GetCategorie(int id)
{
return db.Table<Categories>().FirstOrDefault(t => t.ID == id);
}
//Delete specific Categorie
public void DeleteCategorie(int id)
{
db.Delete<Categories>(id);
}
//Add new student to Categorie
public void AddCategorie(Categories categorie)
{
db.Insert(categorie);
}
}
}
CategoriePage.xaml.cs:
public partial class CategoriePage : ContentPage
{
static TheMapDB database;
TheMapDB categorie = new TheMapDB();
public CategoriePage(String txt)
{
InitializeComponent();
var layout = new StackLayout { Padding = new Thickness(5, 10) };
this.Content = layout;
if(txt.Equals("1"))
{
txt = "this text is number 1";
//needed code can't find soluction
}
var label = new Label { Text = txt, TextColor = Color.FromHex("#77d065"), FontSize = 20 };
layout.Children.Add(label);
}
}
thank you in advance,
I suggest you make a DAO class with a function like this: (or put this function in TheMapDB.cs)
public List<Category> GetCategoryByID(int menuID)
{
return db.Table<Category>().Where(x => x.menu_ID == menuID).ToList();
}
Then you can call this function in your DAO from everywhere you want. That seems the best solution to me.
When you put this function in the class TheMapDB.cs you can say in your CategoryPage.xaml.cs:
database.GetCategoryByID(menuID);

Reusing common mapping code across common DTO classes

I have 2 DTO classes that have multiple common properties, I'm trying to avoid having to repeat myself when writing mapping code for entity to DTO conversion, I'm wondering how I could achieve this, I have a feeling I need to probably use a Func or Action delegate to achieve this. For example I have 2 classes StudentDTO and EmployeeDTO:
public class StudentDTO : PersonDTO
{
public int CourseId { get; set; }
//other properties
}
public class EmployeeDTO : PersonDTO
{
public int OccupationId { get; set; }
//other properties
}
and both naturally inherit from PersonDTO:
public class PersonDTO
{
public int Id { get; set; }
public string FirstName { get; set; }
public string FamilyName { get; set; }
public int Age { get; set; }
}
How could I reuse the mapping code that maps the common properties? Thanks.
You probably can do something like this (very basic and not elegant):
(note Entity can off course be a DataReader, DataSet etc.)
public class Entity
{
public string FirstName { get; set; }
public string FamilyName { get; set; }
public int CourseId { get; set; }
public int OccupationId { get; set; }
}
public class BaseDto
{
}
public class PersonDto : BaseDto
{
public string FirstName { get; set; }
public string FamilyName { get; set; }
public static void Map(Entity entity, PersonDto personDto)
{
personDto.FirstName = entity.FirstName;
personDto.FamilyName = entity.FamilyName;
}
}
public class StudentDto : PersonDto
{
public int CourseId { get; set; }
public static StudentDto Map(Entity entity)
{
var studentDto = new StudentDto { CourseId = entity.CourseId };
// ..can call map to PersonDto if you want
return studentDto;
}
}
public class EmployeeDto : PersonDto
{
public int OccupationId { get; set; }
public static EmployeeDto Map(Entity entity)
{
var employeeDto = new EmployeeDto() { OccupationId = entity.OccupationId };
// ..can call map to PersonDto if you want
return employeeDto;
}
}
public class Mapper<TDto>
where TDto : BaseDto
{
private TDto _dto;
private readonly Entity _entity;
public Mapper(Entity entity)
{
_entity = entity;
}
public Mapper<TDto> Map(Func<Entity, TDto> map)
{
_dto = map(_entity);
return this;
}
public Mapper<TDto> Map<TBaseDto>(Action<Entity, TBaseDto> map)
where TBaseDto : BaseDto
{
map(_entity, _dto as TBaseDto);
return this;
}
public TDto Result
{
get { return _dto; }
}
}
class Program
{
static void Main(string[] args)
{
var studentEntity = new Entity() { FirstName = "John", FamilyName = "Doe", CourseId = 1 };
var studentDto = new Mapper<StudentDto>(studentEntity)
.Map(StudentDto.Map)
.Map<PersonDto>(PersonDto.Map)
.Result;
}
}
Use a library.. that's what they are there for!
Automapper
ValueInjecter
In Automapper, your above mapping becomes incredibly simple:
Mapper.CreateMap<EmployeeDTO, StudentDTO>();
Mapper.CreateMap<StudentDTO, EmployeeDTO>();
..then when you want to map:
var studentInstance = ...; // go get student instance
var employee = Mapper.Map<Employee>(studentInstance);

Set Properties within base function and call function in child function inheritance

I am trying to refactor some old code and wanted to create more logical inheritance.
We have struct Custom Class which we have separated into (3) levels:
AccountView > Details > Full with inheritance. We set the properties of each one as needed.
After looking at the setters, we wanted to combine them into a single class 'SetAccountProp' with methods that set the properties.
We have the 'CustomerBaseView' where we pass in Models ACCOUNT data which works.
Now for the CustomerDetailView pass the same Model ACCOUNT data, but we would like to fill the properties of 'CustomerBaseView' use function 'CustomerBaseView' then fill the details.
Also, for CustomerFullView pass the Model ACCOUNT data, and fill the properties of 'CustomerBaseView' THEN 'CustomerBaseView' and then the remaining fields for CustomerFullView.
How can I call and fill the 'CustomerBaseView' within the 'CustomerDetailView' function? Do I initialize new AccountsView(); in each function?
Not sure how to finish up the refactor without repeating the:
// -- CustomView <--- replace with func?
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
in the Details and Full functions.
CODE
namespace BLL.Presenters
{
public class AccountsView
{
public string Email { get; set; }
public bool Active { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
public class Details : AccountsView
{
public bool Administrator { get; set; }
public DateTime? LastLogin { get; set; }
}
public class Full : Details
{
public Guid ID { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string FullName { get; set; }
}
public class SetAccountProp
{
public static AccountsView CustomerBaseView(Account data)
{
var view = new AccountsView();
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
return view;
}
public static Details CustomerDetailView(Account data)
{
var view = new Details();
// -- CustomView <--- replace with func?
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
// -- Details
view.Administrator = data.Administrator;
view.LastLogin = data.LastLogin;
return view;
}
public static Full CustomerFullView(Account data)
{
var view = new Full();
// -- CustomView <--- replace with func?
view.Email = data.Email;
view.Active = data.Active;
view.FirstName = data.FirstName;
view.LastName = data.LastName;
// -- Details <--- replace with func?
view.Administrator = data.Administrator;
view.LastLogin = data.LastLogin;
// -- Full
view.ID = data.ID;
view.Created = data.Created;
view.Modified = data.Modified;
view.FullName = data.LastName + ", " + data.FirstName;
return view;
}
}
}
Using constructor chaining, you could have something like this:
Each constructor calls it's base class' constructor first, so you don't have to repeat code.
namespace BLL.Presenters
{
using System;
public class Account // dummy to make it compile
{
public string Email;
public bool Active;
public string FirstName;
public string LastName;
public bool Administrator;
public DateTime? LastLogin;
public Guid ID;
public DateTime Created;
public DateTime Modified;
}
public class AccountsView
{
public string Email { get; set; }
public bool Active { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public AccountsView(Account data)
{
this.Email = data.Email;
this.Active = data.Active;
this.FirstName = data.FirstName;
this.LastName = data.LastName;
}
}
public class Details : AccountsView
{
public bool Administrator { get; set; }
public DateTime? LastLogin { get; set; }
public Details(Account data) : base(data)
{
this.Administrator = data.Administrator;
this.LastLogin = data.LastLogin;
}
}
public class Full : Details
{
public Guid ID { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public string FullName { get; set; }
public Full(Account data) : base(data)
{
this.ID = data.ID;
this.Created = data.Created;
this.Modified = data.Modified;
this.FullName = data.LastName + ", " + data.FirstName;
}
}
class Program
{
static void Main()
{
Console.WriteLine();
Console.ReadLine();
}
}
}
Why not something like this:
public class CustomerBase
{
public string Email { get; private set; }
public bool Active { get; private set; }
public string FirstName { get; private set; }
public string LastName { get; private set; }
protected void SetAccountInfo(Account account)
{
this.Email = account.Email;
this.Active = account.Active;
this.FirstName = account.FirstName;
this.LastName = account.LastName;
}
}
public class CustomerA : CustomerBase
{
public string IsAdmin { get; private set; }
public DateTime? LastLogin { get; private set; }
public void SetAccountInfo(Account account)
{
base.SetAccountInfo(account);
this.IsAdmin = account.IsAdmin;
this.LastLogin = account.LastLogin;
}
}
public class Account
{
//your properties
public string Email { get; set; }
public bool Active { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string IsAdmin { get; set; }
public DateTime? LastLogin { get; set; }
}
Or let the SetAccountInfo() return this
public CustomerA SetAccountInfo(Account account)
{
base.SetAccountInfo(account);
this.IsAdmin = account.IsAdmin;
this.LastLogin = account.LastLogin;
return this;
}

Categories

Resources