Unity3D - Show a custom list on the Inspector - list

I have a simple EventList that I want to show on the Inspector.
public class EventList<T> : List<T>
{
public GenericEvent<T> OnAdd;
public new void Add(T item)
{
base.Add(item);
OnAdd.Invoke(item);
}
}
public class GenericEvent<T> : UnityEvent<T> { }
How do I show this on the Inspector similar to how a List is shown, using the simplest way possible, without the use of 3rd-party assets? Thanks!

Related

CMFCRibbonBar remove/hide QAT

Here's the thing - we have a C++ MFC Project including a ribbon. We managed to hide the Main Button. Now we want to do the same with the QAT because it won't be used - we have our own QAT category. Is this possible and how?
Solved it! I derived CMFCRibbonBar. I added a method delQAT() in which i call m_QAToolbar.RemoveAll(). In CMyAppMainFrame::OnCreate() I call this new method.
class CRibbonBar : public CMFCRibbonBar
{
public:
void delQAT()
{
m_QAToolbar.RemoveAll(); //m_QAToolbar is a protected member of CMFCRibbon
}
}
class CMyAppMainFrame(...)
{
//...
protected:
CRibbon m_wndRibbonBar;
//...
public:
void OnCreate()
{
//...
m_wndRibbonBar.delQAT();
//...
}
}
Hope it'll help other people with the same problem :)

Return a concrete implementation in a list

I know this doesnt compile but why shouldnt it?
public interface IReportService {
IList<IReport> GetAvailableReports();
IReport GetReport(int id);
}
public class ReportService : IReportService {
IList<IReport> GetAvailableReports() {
return new List<ConcreteReport>(); // This doesnt work
}
IReport GetReport(int id){
return new ConcreteReport(); // But this works
}
}
It's because of covariance. You can get it to work in .NET 4 (read the link).
Try change to this
IList<? extends IReport> GetAvailableReports()
I recently ran into this problem myself, and found that using IEnumerable instead of List solves the problem. It was quite a frustrating issue, but once I found the source of the problem, it made sense.
Here's the test code I used to find the solution:
using System.Collections.Generic;
namespace InheritList.Test
{
public interface IItem
{
string theItem;
}
public interface IList
{
IEnumerable<IItem> theItems; // previously has as list... didn't work.
// when I changed to IEnumerable, it worked.
public IItem returnTheItem();
public IEnumerable<IItem> returnTheItemsAsList();
}
public class Item : IItem
{
string theItem;
}
public class List : IList
{
public IEnumerable<IItem> theItems; // List here didn't work - changed to IEnumerable
public List()
{
this.theItems = returnTheItemsAsList();
}
public IItem returnTheItem()
{
return new Item();
}
public IEnumerable<IItem> returnTheItemsAsList()
{
var newList = new List<Item>();
return newList;
}
}
}

How to decouple process in business layer

I am facing a problem that, for some business processes the sequence of invoking business objects and methods may change frequently. So I came up with something similar to the below:(Sorry somehow I can't post image..., I tried to express them in the below text)
Business Objects:
Object1, Object2
Methods: M1, M2, M3, M4
Processes: P1 (M1 > M2 > M3), P2 (M2 > M3 > if M3 return true then M4 else end)
In this case I am using .NET 3.5. I create some classes to represent processes, which contains those sequences I mentioned. It works. But the problem is I need to compile every time when process changed. It would be much better if I could configure it by some sort of XML.
I have heard about jBPM for Java, Workflow Foundation for .NET but not sure if they fit my needs, or would they be overkill. I even don't what keyword to search in Google. Could anyone advice what technology I should use to solve this issue? Or just point me to some websites or books? Thanks in advance.
A common way to decouple software layers is by using interfaces as stated by Dependency Inversion Principle. In you case you could abstract the process concept using an interface and implement the logic in the implementation of that interface.
when you need change the logic of the process you can create a new implementation of that interface. You can use any IoC framework to inject what implementation you want to use
below is showed just a simple way to do that:
public interface IMethod
{
void M1();
string M2();
void M3();
void M4();
}
public interface IProcess
{
IMethod Method { get; set; }
void P1();
void P2();
}
public class Process : IProcess
{
public IMethod Method
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public void P1()
{
Method.M1();
Method.M2();
}
public void P2()
{
if(Method.M2()==string.Empty)
{
Method.M3();
}
}
}
public class AnotherProcess : IProcess
{
public IMethod Method
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public void P1()
{
Method.M4();
}
public void P2()
{
Method.M2();
Method.M4();
}
}
public class UseProcess
{
private IProcess _process;
//you can inject the process dependency if you need use a different implementation
public UseProcess(IProcess process)
{
_process = process;
}
public void DoSomething()
{
_process.P1();
}
}

Testable design with COM objects

What is a good way to design for testing and extensibility when a component used to complete a task could either be a COM component or a .NET component? Does it make sense to wrap the COM component completely and extract an interface? Here is a simple, completely contrived, RCW interface on a COM component, where "abc" is the acronym for the component maker:
public interface IComRobot
{
void abcInitialize(object o);
void abcSet(string s, object o);
void abcBuild();
void abcExit();
}
To me, the fact that the provider of the component chose to prefix all methods with something indicating their company is somewhat irritating. The problem is, I want to define other Robot components that perform the same actions, but the underlying implementation is different. It would be completely confusing to Robot builders to have to implement "abcAnything".
How should I go about building a RobotFactory with a simple implementation that works like this?
public class RobotFactory
{
public static IRobot Create(int i)
{
// // problem because ComRobot implements IComRobot not IRobot
if (i == 0) return new ComRobot();
if (i == 1) return new AdvancedRobot();
return new SimpleRobot();
}
}
Should I bite the bullet and accept the abc prefix in my interface, thus confusing robot implementers? Should I force a dependency on the Robot consumer to know when they are using the COM robot? None of these seem ideal. I'm thinking about an additional level of abstraction (that can solve everything, right?). Something like so:
public interface IRobot : IDisposable
{
void Initialize(object o);
void Set(string s, object o);
void Build();
void Exit();
}
public class ComRobotWrapper: IRobot
{
private readonly IComRobot m_comRobot;
public ComRobotWrapper()
{
m_comRobot = ComRobotFactory.Create();
}
public void Initialize(object o)
{
m_comRobot.abcInitialize(o);
}
public void Set(string s, object o)
{
m_comRobot.abcSet(s, o);
}
public void Build()
{
m_comRobot.abcBuild();
}
public void Exit()
{
m_comRobot.abcExit();
}
public void Dispose()
{
//...RELEASE COM COMPONENT
}
}
public class ComRobotFactory
{
public static IComRobot Create()
{
return new ComRobot();
}
}
I would then alter and use the RobotFactory like so:
public class RobotFactory
{
public static IRobot Create(int i)
{
if (i == 0) return new ComRobotWrapper();
if (i == 1) return new AdvancedRobot();
return new SimpleRobot();
}
}
public class Tester
{
// local vars loaded somehow
public void Test()
{
using (IRobot robot = RobotFactory.Create(0))
{
robot.Initialize(m_configuration);
robot.Set(m_model, m_spec);
robot.Build();
robot.Exit();
}
}
}
I'm interested in opinions on this approach. Do you recommend another approach? I really don't want to take on a DI framework, so that is out of scope. Are the pitfalls in testability? I appreciate you taking the time to consider this lengthy issue.
That looks spot on to me. You are creating an interface that is right for your domain / application, and implementing it in terms of a thrid party component.

How to write a Mock Repository to test WCF RIA Domain Service build on top of Entity Framework Context

I need to write a test layer to Test my WCF RIA Domain Service layer which is build on top of Entity Framework context. I have come across some patterns which suggest to use a repository and then use the Domain Service factory to intilize the domain service with a repository instance to use. One of the sample which fits the requirement is explained here on Vijay's blog(http://blogs.msdn.com/vijayu/archive/2009/06/08/unit-testing-business-logic-in-net-ria-services.aspx). The problem with this implementation is that it initilize the repository only for a specific Domain Object e.g. Customer/Product but it provides no way to create a repository which can return any object which i would like to return.
Please suggest what is the right way of doing this and whether it is possible or not.
Thanks in advance,
Manoj
I got around this issue by extending the sample with a RepositoryCollection object, which automatically instantiates LinqToSqlRepositories as needed, and also allows the insertion of mock/stub repositories manually for unit testing.
public class RepositoryCollection : IDisposable
{
private Dictionary<Type, object> _repositories = new Dictionary<Type, object>();
private DataContext _context;
public RepositoryCollection() { }
public RepositoryCollection(DataContext context)
{
_context = context;
}
public IRepository<T> Get<T>() where T : class
{
if(!_repositories.ContainsKey(typeof(T)))
_repositories.Add(typeof(T), new LinqToSqlRepository<T>(_context));
return _repositories[typeof(T)] as IRepository<T>;
}
public RepositoryCollection Insert<T>(IRepository<T> repository) where T : class
{
_repositories[typeof(T)] = repository;
return this;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void SubmitChanges()
{
if (_context != null)
_context.SubmitChanges();
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if(_context != null)
_context.Dispose();
}
}
}
Then, in your domain service, you use it like so:
private RepositoryCollection _repositoryCollection;
public MyDomainService(RepositoryCollection repositoryCollection = null)
{
_repositoryCollection = repositoryCollection ?? new RepositoryCollection(new MyDataContext());
}
public IQueryable<Customer> GetCustomers()
{
return _repositoryCollection.Get<Customer>().Query();
}
public IQueryable<Product> GetProducts()
{
return _repositoryCollection.Get<Product>().Query();
}
.. other methods go here ...